初学Struts写了一个很简单的应用主要功能和页面如下
首页显示一个添加新用户的链接点击该链接出发一个forward动作页面导向到添加用户的jsp页面
添加用户的jsp页面中可供用户输入用户名和用户描述两项
用户输入完毕将做输入数据合法性检查检查通过将输入信息保存进入文件(使用了Properties类)然后返回首页检查失败返回添加用户页面
数据合法性检查分成两块第一部分检查条件使用Struts的Validator检查条件配置在Validatorxml中第二部分检查放在ActionForm中
检查失败将错误信息置入ActionErrors中然后返回到添加用户的页面并显示错误信息
JSP页面ActionForm和Action类的代码书写都参照了strutsexample应用所以这里代码不再列举请看附件中的代码包这里值得一提的是在开发过程中碰到了一个小问题正是由于该问题才导致查看Struts源码刨根问底的查找错误原因的过程该错误发生在Struts的配置文件中首先将错误的配置文件列出如下
<?xml version= encoding=ISO ?>
<!DOCTYPE strutsconfig PUBLIC
//Apache Software Foundation//DTD Struts Configuration //EN
config__dtd>
<strutsconfig>
<! ======================================== Form Bean Definitions >
<formbeans>
<formbean
name=CreateUserForm
type=comzchomeCreateUserForm/>
</formbeans>
<! ================================= Global Exception Definitions >
<globalexceptions>
</globalexceptions>
<! =================================== Global Forward Definitions >
<globalforwards>
<! Default forward to Welcome action >
<! Demonstrates using indexjsp to forward >
<forward name=welcome path=/Welcomedo/>
</globalforwards>
<! =================================== Action Mapping Definitions >
<actionmappings>
<! Default Welcome action >
<! Forwards to Welcomejsp >
<action
path=/Welcome
type=orgapachestrutsactionsForwardAction
parameter=/jsp/Welcomejsp/>
<action path=/createuserpage forward=/jsp/createuserjsp>
</action>
<action
path=/docreateuser
type=comzchomeCreateUserAction
name=CreateUserForm
scope=request
input=createuser>
<forward name=createusersuccess path=/jsp/Welcomejsp/>
<forward name=createuser path=/jsp/createuserjsp/>
</action>
</actionmappings>
<! ===================================== Controller Configuration >
<controller>
<setproperty property=processorClass value=orgapachestrutstilesTilesRequestProcessor/>
</controller>
<! ================================ Message Resources Definitions >
<messageresources parameter=resourcesapplication/>
<! ======================================= Plug Ins Configuration >
<! ========== Tiles plugin =================== >
<! >
<!
This plugin initialize Tiles definition factory This later can takes some
parameters explained here after The plugin first read parameters from webxml then
overload them with parameters defined here All parameters are optional
The plugin should be declared in each strutsconfig file
definitionsconfig: (optional)
Specify configuration file names There can be several comma
separated file names (default: ?? )
moduleAware: (optional struts)
Specify if the Tiles definition factory is module aware If true (default)
there will be one factory for each Struts module
If false there will be one common factory for all module In this later case
it is still needed to declare one plugin per module The factory will be
initialized with parameters found in the first initialized plugin (generally the
one associated with the default module)
true : One factory per module (default)
false : one single shared factory for all modules
definitionsparservalidate: (optional)
Specify if xml parser should validate the Tiles configuration file
true : validate DTD should be specified in file header (default)
false : no validation
Paths found in Tiles definitions are relative to the main context
>
<! comment following if strutsx >
<plugin className=orgapachestrutstilesTilesPlugin >
<setproperty property=definitionsconfig
value=/WEBINF/tilesdefsxml />
<setproperty property=moduleAware value=true />
<setproperty property=definitionsparservalidate value=true />
</plugin>
<! end comment if strutsx >
<plugin className=orgapachestrutsvalidatorValidatorPlugIn>
<setproperty
property=pathnames
value=/WEBINF/validatorrulesxml/WEBINF/validationxml/>
</plugin>
</strutsconfig>
首先描述一下系统的出错背景
从首页点击链接来到添加用户的页面 正常
在添加用户页面中输入Vlidatorxml文件中定义的错误数据弹出Javascript对话框提示出错 正常
在添加用户页面中输入合法数据数据保存进入文件并重定向到首页 正常
在添加用户页面中输入ActionForm中定义的非法数据系统应返回到添加用户的页面 出错!!!
OK来着重看这个添加动作的定义如下
<action
path=/docreateuser
type=comzchomeCreateUserAction
name=CreateUserForm
scope=request
input=createuser>
<forward name=createusersuccess path=/jsp/Welcomejsp/>
<forward name=createuser path=/jsp/createuserjsp/>
</action>
从以上的定义可以看出如果Validate验证出错Struts应该为我们重定向到input域所定义的uri即/jsp/createuserjsp
看起来应该没有问题再来看看出错信息如下
javalangIllegalArgumentException: Path createuser does not start with a / character
at orgreApplicationContextgetRequestDispatcher(ApplicationContextjava:)
at orgreApplicationContextFacadegetRequestDispatcher(ApplicationContextFacadejava:)
at orgapachestrutsactionRequestProcessordoForward(RequestProcessorjava:)
at orgapachestrutstilesTilesRequestProcessordoForward(TilesRequestProcessorjava:)
at orgapachestrutsactionRequestProcessorinternalModuleRelativeForward(RequestProcessorjava:)
at orgapachestrutstilesTilesRequestProcessorinternalModuleRelativeForward(TilesRequestProcessorjava:)
at orgapachestrutsactionRequestProcessorprocessValidate(RequestProcessorjava:)
at orgapachestrutsactionRequestProcessorprocess(RequestProcessorjava:)
at orgapachestrutsactionActionServletprocess(ActionServletjava:)
at orgapachestrutsactionActionServletdoPost(ActionServletjava:)
出错信息清楚的说明createuser这个path应该以/字符开头
为定位这个错误从以上错误信息开始打开Struts的源码RequestProcessorjava进行研究首先来到这一段
public class RequestProcessor {
protected boolean processValidate(H