程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Struts源碼研究-Action-Input屬性篇

Struts源碼研究-Action-Input屬性篇

編輯:JAVA編程入門知識

  初學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
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved