网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  struts源码研究 - action-input属性篇     
  文章作者:未知  文章来源:水木森林  
  查看:54次  录入:管理员--2007-11-17  
 
  初学struts,写了一个很简单的应用,主要功能和页面如下:
  
  1、首页显示一个“添加新用户”的链接,点击该链接出发一个forward动作,页面导向到添加用户的jsp页面
  2、添加用户的jsp页面中,可供用户输入“用户名”和“用户描述”两项
  3、用户输入完毕,将做输入数据合法性检查,检查通过,将输入信息保存进入文件(使用了properties类),然后返回首页;检查失败返回添加用户页面
  4、数据合法性检查分成两块,第一部分检查条件使用struts的validator,检查条件配置在validator.xml中;第二部分检查放在actionform中,
  检查失败将错误信息置入actionerrors中,然后返回到添加用户的页面并显示错误信息。
  
  jsp页面、actionform和action类的代码书写都参照了struts-example应用,所以这里代码不再列举,请看附件中的代码包这里值得一提的是,在开发过程中,碰到了一个小问题,正是由于该问题,才导致查看struts源码,刨根问底的查找错误原因的过程该错误发生在struts的配置文件中,首先将错误的配置文件列出如下:
  
  <?xml version="1.0" encoding="iso-8859-1" ?>
  
  <!doctype struts-config public
  "-//apache software foundation//dtd struts configuration 1.1//en"
  "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
  
  <struts-config>
  
  <!-- ======================================== form bean definitions -->
  
  <form-beans>
  
  <form-bean
  
  name="createuserform"
  
  type="com.zchome.createuserform"/>
  
  
  </form-beans>
  
  <!-- ================================= global exception definitions -->
  
  <global-exceptions>
  
  </global-exceptions>
  
  <!-- =================================== global forward definitions -->
  
  <global-forwards>
  
  <!-- default forward to "welcome" action -->
  
  <!-- demonstrates using index.jsp to forward -->
  
  <forward name="welcome" path="/welcome.do"/>
  
  </global-forwards>
  
  <!-- =================================== action mapping definitions -->
  
  <action-mappings>
  
  <!-- default "welcome" action -->
  
  <!-- forwards to welcome.jsp -->
  
  <action
  
  path="/welcome"
  
  type="org.apache.struts.actions.forwardaction"
  
  parameter="/jsp/welcome.jsp"/>
  
  
  <action path="/createuserpage" forward="/jsp/createuser.jsp">
  </action>
  
  
  <action
  
  path="/docreateuser"
  
  type="com.zchome.createuseraction"
  
  name="createuserform"
  
  scope="request"
  
  input="createuser">
  
  <forward name="createusersuccess" path="/jsp/welcome.jsp"/>
  
  <forward name="createuser" path="/jsp/createuser.jsp"/>
  
  </action>
  
  
  </action-mappings>
  
  
  <!-- ===================================== controller configuration -->
  
  <controller>
  <set-property property="processorclass" value="org.apache.struts.tiles.tilesrequestprocessor"/>
  </controller>
  
  <!-- ================================ message resources definitions -->
  
  <message-resources parameter="resources.application"/>
  
  <!-- ======================================= 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 web.xml, then
  overload them with parameters defined here. all parameters are optional.
  the plugin should be declared in each struts-config file.
  
  - definitions-config: (optional)
  specify configuration file names. there can be several comma
  separated file names (default: ?? )
  - moduleaware: (optional - struts1.1)
  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
  - definitions-parser-validate: (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 struts1.0.x -->
  <plug-in classname="org.apache.struts.tiles.tilesplugin" >
  <set-property property="definitions-config"
  value="/web-inf/tiles-defs.xml" />
  <set-property property="moduleaware" value="true" />
  <set-property property="definitions-parser-validate" value="true" />
  </plug-in>
  
  <!-- end comment if struts1.0.x -->
  
  <plug-in classname="org.apache.struts.validator.validatorplugin">
  <set-property
  property="pathnames"
  value="/web-inf/validator-rules.xml,/web-inf/validation.xml"/>
  </plug-in>
  
  </struts-config>
  
  首先描述一下系统的出错背景:
  1、从首页点击链接来到添加用户的页面 正常
  2、在添加用户页面中输入vlidator.xml文件中定义的错误数据,弹出javascript对话框,提示出错 正常
  3、在添加用户页面中输入合法数据,数据保存进入文件并重定向到首页 正常
  4、在添加用户页面中输入actionform中定义的非法数据,系统应返回到添加用户的页面 出错!!!
  ok,来着重看这个添加动作的定义,如下:
  
  <action
  
  path="/docreateuser"
  
  type="com.zchome.createuseraction"
  
  name="createuserform"
  
  scope="request"
  
  input="createuser">
  
  <forward name="createusersuccess" path="/jsp/welcome.jsp"/>
  
  <forward name="createuser" path="/jsp/createuser.jsp"/>
  
  </action>
  
  从以上的定义可以看出,如果validate验证出错,struts应该为我们重定向到input域所定义的uri,即/jsp/createuser.jsp
  看起来应该没有问题,再来看看出错信息,如下:
  
  java.lang.illegalargumentexception: path createuser does not start with a "/" character
  at org.apache.catalina.core.applicationcontext.getrequestdispatcher(applicationcontext.java:1179)
  at org.apache.catalina.core.applicationcontextfacade.getrequestdispatcher(applicationcontextfacade.java:174)
  at org.apache.struts.action.requestprocessor.doforward(requestprocessor.java:1062)
  at org.apache.struts.tiles.tilesrequestprocessor.doforward(tilesrequestprocessor.java:274)
  at org.apache.struts.action.requestprocessor.internalmodulerelativeforward(requestprocessor.java:1012)
  at org.apache.struts.tiles.tilesrequestprocessor.internalmodulerelativeforward(tilesrequestprocessor.java:345)
  at org.apache.struts.action.requestprocessor.processvalidate(requestprocessor.java:980)
  at org.apache.struts.action.requestprocessor.process(requestprocessor.java:255)
  at org.apache.struts.action.actionservlet.process(actionservlet.java:1482)
  at org.apache.struts.action.actionservlet.dopost(actionservlet.java:525)
  
  出错信息清楚的说明,“createuser”这个path应该以“/”字符开头
  为定位这个错误,从以上错误信息,开始打开struts的源码requestprocessor.java进行研究,首先来到这一段:
  
  public class requestprocessor {
  
  protected boolean processvalidate(h
 
 
上一篇: struts源码研究 - bean-message标签篇    下一篇: struts源码研究 - logic-iterator标签篇
  相关文档
java对象持久化技术之hibernate入门之二 11-17
java小程序源文件的组成 11-17
java之旅(8)复用类 (续) 11-17
struts构建文件上传(7) 11-17
java语言深入:java类完整构造执行顺序 02-25
基于tomcat5.5的数据库连接池环境设置 11-17
ajax框架zk 1.0发布 11-17
管理人员如何编制性能计划 11-17
java语言中的链表和双向链表的实现方法 11-16
grails + ejb domain models 11-17
使用struts,ibaits和jstl开发简便通用的文件上传系统(1) 11-17
netscape服务器端编程技术 11-17
精典文萃:java编程中异常处理的优劣观 11-16
java线程的缺陷 11-17
初学者编程十大好习惯 11-17
最佳实践:避免或最小化 servlet 中的同步 11-17
j2se1.4的i/o新特性 11-17
java面试中的陷阱 11-17
jdbc连接sqlserver2000 11-17
让java程序带着jre一起上路 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息