熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

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

2013-11-23 20:33:19  來源: Java開源技術 

  初學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
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28659.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.