快速业务通道

Struts源码研究 - Action-Input属性篇 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-15
了javax.servlet.ServletContext的方法getRequestDispatcher这个方法

既然出错信息中是路径出了问题,那么看来这个参数uri非常的重要,极有可能就是这个uri发生了错误导致了出错OK,开始剖析这个uri,从头开始看,这个uri是这样被赋值的:uri = moduleConfig.getPrefix() + uri

1、moduleConfig.getPrefix()这个方法返回的应该是""(这个请看ActionServlet的Init方法,如果在web.xml文件中定义ActionServlet的时候,给定了一些init-params,那么这个prefix就有可能不为空,这里不再列举了)

2、代码右边的这个uri是从processValidate这个方法中定义的input,如下:

String input = mapping.getInput();这个input应该是struts-config.xml文件中定义的那个action的input,也就是“createuser”,如果Struts将其做了进一步的解析,那么这个input应该进一步被转化成为“/jsp/createuser.jsp”

好,到此为止,可以看到,这个uri不是“createuser”,那就是“/jsp/createuser.jsp”,再来看getRequestDispatcher这个方法的定义,翻开Servlet的API文档,可以看到如下一段话:

public RequestDispatchergetRequestDispatcher(java.lang.String path)Returns a RequestDispatcher object that acts as a wrapperfor the resource located at the given path. A RequestDispatcher object can be used to forward a requestto the resource or to include the resource in a response. The resource can be dynamic or static.The pathname must begin with a "/" and is interpreted as relative to the current context root.Use getContext to obtain a RequestDispatcher for resources in foreign contexts. This method returns nullif the ServletContext cannot return a RequestDispatcher.

终于有拨云见日的感觉了,因为这段话和出错信息实在是太一致了!由上面这段话,我们可以断定,uri这个变量的值肯定是“createuser”,而不是我们所希望的“/jsp/createuser.jsp”。为什么会这样呢?显然是struts-config.xml中配置有些还是不对,或是缺了点什么。想到这里,很自然的就联想到上面所提到的InputForward这个配置项了,因为从字面意思上看来,这个配置项的用处就应该是将input的值解析成forward中对应的值,而且在ControllerConfig中,这个变量默认值是false,所以猜测将其改成true是不是就可以了呢?

为了寻找答案,再次翻开struts-example(因为这个例子中的action也定义了input),终于找到了答案,和之前猜测的果然十分吻合,如下:

<controller><set-property property="inputForward" value="true"/></controller>

至此,问题解决,正确的action配置可以是如下两种:

1、不使用inputForward

<actionpath="/docreateuser"type="com.zchome.CreateUserAction"name="CreateUserForm"scope="request"input="/jsp/createuser.jsp"><forward name="createusersuccess" path="/jsp/Welcome.jsp"/></action>

Struts源码研究 - Action-Input属性篇(6)

时间:2011-08-18

2、使用InputForward

<actionpath="/docreateuser"type="com.zchome.CreateUserAction"name="CreateUserForm"scope="request"input="createuser"><forward name="createusersuccess" path="/jsp/Welcome.jsp"/><forward name=&

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号