Struts 1/x でタイルを使用する際に問題が発生しました。
たとえば、次のファイルがあります
なのでtiles-def.xml
<definition name="list.page" extends="main.layout">
<put name="title" value="List Page" />
<put name="body" value="/pages/tiles/body/list.jsp" />
</definition>
<definition name="view.page" extends="main.layout">
<put name="title" value="View Page" />
<put name="body" value="/pages/tiles/body/ViewItem.jsp" />
</definition>
<definition name="add.page" extends="main.layout">
<put name="title" value="Add and Edit Page" />
<put name="body" value="/pages/tiles/body/AddItem.jsp"></put>
</definition>
じぶんの struts-config.xml
<action-mappings>
<action path="/items" name="itemsForm"
type="org.springframework.web.struts.DelegatingActionProxy"
parameter="method" validate="false">
<forward name="list" path="list.page" />
<forward name="view" path="view.page" />
<forward name="add" path="add.page" />
<forward name="delete" path="/index.jsp" />
<forward name="edit" path="add.page" />
</action>
///
</action-mapping>
たとえば、私のlist.jspに次のようにリンクします
<a href="#" onclick="SendRequest('GET', 'news.do', 'method=view&newsMessage.id=${news.id}', null)"><bean:message key="link.view" /></a>
そしてajaxのJS
function CreateRequest() {
var Request = false;
if (window.XMLHttpRequest) {
// Gecko-compatible browsers
Request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// IE
try {
Request = new ActiveXObject("Msxml2.XMLHTTP"); //Newer IE
} catch (CatchException) {
Request = new ActiveXObject("Microsoft.XMLHTTP"); //Older IE
}
}
if (!Request) {
alert("Impossible create XMLHTTPREquest");
}
return Request;
}
-
/**
* Function sending request to file on server
*
* r_method - type request GET or POST r_path - path to file r_args - args as
* a1&a2 r_handler - function handler response from server
*
*/
function SendRequest(r_method, r_path, r_args, r_handler) {
// Create Request
var Request = CreateRequest();
// Check existing request
if (!Request) {
return;
}
// Set user handler
Request.onreadystatechange = function() {
// If exchange data was finished
if (Request.readyState == 4) {
if (Request.status == 200) {
// Give management user handler
//r_handler(Request);
document.getElementById("content_container").innerHTML = Request.responseText;
}
}
};
// Check if need to create GET-request
if (r_method.toLowerCase() == "get" && r_args.length > 0) {
r_path += "?" + r_args;
}
// Initialize connection
Request.open(r_method, r_path, true);
if (r_method.toLowerCase() == "post") {
// If this is POST
// Set header
Request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
//Send request
Request.send(r_args);
}
else {
//If it's GET
//Send null-request
Request.send(null);
}
}
そしてサーバー上でのアクション
public ActionForward view(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType(RESP_CONTENT_TYPE);
//some operationss
if (itemsForView.getId() == null) {
//return mapping.findForward(ERROR_ACTION);
return new ActionForward("/pages/error.jsp");
} else {
itemsForm.setItems(itemsForView);
//return mapping.findForward(VIEW_ACTION);
return new ActionForward("/pages/tiles/body/View.jsp");
}
}
次の例Using AJAX With Struts and Tilesを見つけたので混乱しました
良い記事の説明やリンクを教えてください。