我在使用struts 1 / x中的tile时遇到了问题。
例如我有下一个文件
为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>
JS for ajax
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
请给我解释或链接好文章。