初學Struts
寫了一個很簡單的應用
主要功能和頁面如下
首頁顯示一個
添加新用戶
的鏈接
點擊該鏈接出發一個forward動作
頁面導向到添加用戶的jsp頁面
添加用戶的jsp頁面中
可供用戶輸入
用戶名
和
用戶描述
兩項
用戶輸入完畢
將做輸入數據合法性檢查
檢查通過
將輸入信息保存進入文件(使用了Properties類)
然後返回首頁
檢查失敗返回添加用戶頁面
數據合法性檢查分成兩塊
第一部分檢查條件使用Struts的Validator
檢查條件配置在Validator
xml中
第二部分檢查放在ActionForm中
檢查失敗將錯誤信息置入ActionErrors中
然後返回到添加用戶的頁面並顯示錯誤信息
JSP頁面
ActionForm和Action類的代碼書寫都參照了struts
example應用
所以這裡代碼不再列舉
請看附件中的代碼包這裡值得一提的是
在開發過程中
碰到了一個小問題
正是由於該問題
才導致查看Struts源碼
刨根問底的查找錯誤原因的過程該錯誤發生在Struts的配置文件中
首先將錯誤的配置文件列出如下
<?xml version=
encoding=
ISO
?>
<!DOCTYPE struts
config PUBLIC
//Apache Software Foundation//DTD Struts Configuration
//EN
config_
_
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
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
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 struts
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 struts
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>
首先描述一下系統的出錯背景
從首頁點擊鏈接來到添加用戶的頁面 正常
在添加用戶頁面中輸入Vlidator
xml文件中定義的錯誤數據
彈出Javascript對話框
提示出錯 正常
在添加用戶頁面中輸入合法數據
數據保存進入文件並重定向到首頁 正常
在添加用戶頁面中輸入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 orgre
ApplicationContext
getRequestDispatcher(ApplicationContext
java:
)
at orgre
ApplicationContextFacade
getRequestDispatcher(ApplicationContextFacade
java:
)
at org
apache
struts
action
RequestProcessor
doForward(RequestProcessor
java:
)
at org
apache
struts
tiles
TilesRequestProcessor
doForward(TilesRequestProcessor
java:
)
at org
apache
struts
action
RequestProcessor
internalModuleRelativeForward(RequestProcessor
java:
)
at org
apache
struts
tiles
TilesRequestProcessor
internalModuleRelativeForward(TilesRequestProcessor
java:
)
at org
apache
struts
action
RequestProcessor
processValidate(RequestProcessor
java:
)
at org
apache
struts
action
RequestProcessor
process(RequestProcessor
java:
)
at org
apache
struts
action
ActionServlet
process(ActionServlet
java:
)
at org
apache
struts
action
ActionServlet
doPost(ActionServlet
java:
)
出錯信息清楚的說明
createuser
這個path應該以
/
字符開頭
為定位這個錯誤
從以上錯誤信息
開始打開Struts的源碼RequestProcessor
java進行研究
首先來到這一段
public class RequestProcessor {
protected boolean processValidate(H
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28659.html