国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

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

2019-11-17 06:19:42
字體:
供稿:網(wǎng)友

  初學(xué)Struts,寫了一個很簡單的應(yīng)用,主要功能和頁面如下:
  
  1、首頁顯示一個“添加新用戶”的鏈接,點(diǎn)擊該鏈接出發(fā)一個forward動作,頁面導(dǎo)向到添加用戶的jsp頁面
  2、添加用戶的jsp頁面中,可供用戶輸入“用戶名”和“用戶描述”兩項(xiàng)
  3、用戶輸入完畢,將做輸入數(shù)據(jù)合法性檢查,檢查通過,將輸入信息保存進(jìn)入文件(使用了PRoperties類),然后返回首頁;檢查失敗返回添加用戶頁面
  4、數(shù)據(jù)合法性檢查分成兩塊,第一部分檢查條件使用Struts的Validator,檢查條件配置在Validator.xml中;第二部分檢查放在ActionForm中,
  檢查失敗將錯誤信息置入ActionErrors中,然后返回到添加用戶的頁面并顯示錯誤信息。
  
  JSP頁面、ActionForm和Action類的代碼書寫都參照了struts-example應(yīng)用,所以這里代碼不再列舉,請看附件中的代碼包這里值得一提的是,在開發(fā)過程中,碰到了一個小問題,正是由于該問題,才導(dǎo)致查看Struts源碼,刨根問底的查找錯誤原因的過程該錯誤發(fā)生在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>
  
  首先描述一下系統(tǒng)的出錯背景:
  1、從首頁點(diǎn)擊鏈接來到添加用戶的頁面 正常
  2、在添加用戶頁面中輸入Vlidator.xml文件中定義的錯誤數(shù)據(jù),彈出javascript對話框,提示出錯 正常
  3、在添加用戶頁面中輸入合法數(shù)據(jù),數(shù)據(jù)保存進(jìn)入文件并重定向到首頁 正常
  4、在添加用戶頁面中輸入ActionForm中定義的非法數(shù)據(jù),系統(tǒng)應(yīng)返回到添加用戶的頁面 出錯!!!
  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驗(yàn)證出錯,Struts應(yīng)該為我們重定向到input域所定義的uri,即/jsp/createuser.jsp
  看起來應(yīng)該沒有問題,再來看看出錯信息,如下:
  
  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應(yīng)該以“/”字符開頭
  為定位這個錯誤,從以上錯誤信息,開始打開Struts的源碼RequestProcessor.java進(jìn)行研究,首先來到這一段:
  
  public class RequestProcessor {
  
  protected boolean processValidate(H

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 二手房| 黑龙江省| 西城区| 张家口市| 大邑县| 台前县| 北京市| 呼图壁县| 乐至县| 苍山县| 河东区| 昌图县| 平远县| 宁化县| 泰来县| 青海省| 宣威市| 揭西县| 鄂州市| 勃利县| 雅安市| 铜梁县| 白朗县| 太白县| 南昌县| 岚皋县| 金溪县| 罗山县| 万宁市| 蓝田县| 安塞县| 屏南县| 绥化市| 张家港市| 旅游| 诏安县| 德兴市| 保定市| 日土县| 阿合奇县| 稻城县|