在前边的几篇中,我们已经看了Action的执行,
实际上 Action 的任务已经基本完成了,只剩下一个收尾的工作 : Action的跳转。
让我们看一下 Xwork 是如何做的。
相关的方法是我们前边提到过的 ActionInvocation 的 invoke 方法,
// now execute the result, if we're supposed to
if (proxy.getExecuteResult()) {
executeResult();
}
/**
* Uses getResult to get the final Result and executes it
*
* @throws ConfigurationException If not result can be found with the returned code
*/
private void executeResult() throws Exception {
result = createResult();
String timerKey = "executeResult: "+getResultCode();
try {
UtilTimerStack.push(timerKey);
if (result != null) {
result.execute(this);
} else if (resultCode != null && !Action.NONE.equals(resultCode)) {
throw new ConfigurationException("No result defined for action " + getAction().getClass().getName()
+ " and result " + getResultCode(), proxy.getConfig());
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("No result returned for action "+getAction().getClass().getName()+" at "+proxy.getConfig().getLocation());
}
}
} finally {
UtilTimerStack.pop(timerKey);
}
}
result.execute(this);
调用result 的 相应方法,
ServletDispatcherResult 是 xwork 中,负责转跳页面的Result,
我们以他做例子,看一下 xwork 的Result 是怎么做的。
public class ServletDispatcherResult extends WebWorkResultSupport {
private static final Log log = LogFactory.getLog(ServletDispatcherResult.class);
/**
* Dispatches to the given location. Does its forward via a RequestDispatcher. If the
* dispatch fails a 404 error will be sent back in the http response.
*
* @param finalLocation the location to dispatch to.
* @param invocation the execution state of the action
* @throws Exception if an error occurs. If the dispatch fails the error will go back via the
* HTTP request.
*/
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Forwarding to location " + finalLocation);
}
PageContext pageContext = ServletActionContext.getPageContext();
if (pageContext != null) {
pageContext.include(finalLocation);
} else {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);
// if the view doesn't exist, let's do a 404
if (dispatcher == null) {
response.sendError(404, "result '" + finalLocation + "' not found");
return;
}
// If we're included, then include the view
// Otherwise do forward
// This allow the page to, for example, set content type
if (!response.isCommitted() && (request.getAttribute("javax.servlet.include.servlet_path") == null)) {
request.setAttribute("webwork.view_uri", finalLocation);
request.setAttribute("webwork.request_uri", request.getRequestURI());
dispatcher.forward(request, response);
} else {
dispatcher.include(request, response);
}
}
}
}
doExecute 方法,是Result 的主要逻辑,
我们可以看到
RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);
dispatcher.forward(request, response);
这两句实现了页面的转跳功能,将执行完毕的Acion 跳到适当的页面。
这是我们会最常用到的一个 Result,
其他还有很多,可以生成word,pdf,或转跳到另一个Action 等,都于此类似,
Xwork中, Action的调用,到此也就基本完成了。
分享到:
相关推荐
源码学习,Xwork2 源码阅读.pdf webwork源码, strtus2 源码
NULL 博文链接:https://gutou9.iteye.com/blog/268794
xwork-2.1.2-all.zip,xwork2.2.1源码,github上下载,适合不会SVN下载的初学者
xwork2.1.6源码及其之前版本的源码
struts2下的xwork源码 学习源代码,掌握struts2原理
xwork-2.0.4源码 xwork2源码
struts2-core-2.3.1.2.jar 和xwork-core-2.3.1.2.jar的源码;
XWork源码+docs+showcase全,在运用struts2时可通过eclipse或myeclipse查看源码
Struts2中xwork源码,希望对你们有所帮助。
这个压缩包包含struts-xwork-core的src源码,导入eclipse即可。
xwork源码 xwork src Eclipse关联xwork源码 这个目录里面xwork-2.0.7\src\java
xwork源码,xwork源码,xwork源码
xwork源码及文档,xwork源码及文档,xwork源码及文档
Xwork的源码,struts2中action的内核
xwork 2.8的源码,最新版本欢迎下载!
在网上找到的好东西,拿来大家共享一下。希望支持
xwork2.3.3 源码,解压后的文件就是源码,用exlipse加载方法,在xwork2.3.3.jar上右键--属性--java source Attachment,选择external folder把解压后的文件目录定位到Java即可,我的路径是F:/struts-2.3.3-src/src/...
xwork-2.0.4 源码 xwork-2.0.4 源码
使用SVN下载的xwork的源代码包,包含docs、src、core、plugins、assembly等文件,以及xwork.jar札包。
xwork 源码, xwork and struts2 的最佳学习资料, 百分百的源码, 学习过各中可以不断的学习, 通过阅读源码, 可以大大的提升你的学习进度, 达到事半功倍的效果. 其中给包括有源码, 示例, 介绍文档.