這時web.xml中相應的地方就改為:
<filter>
???????<filter-name>struts</filter-name>
???????<filter-class>
???????????com.filter.NewFilterDispatcher
???????</filter-class>
???????<init-param>
????????????<param-name>encoding</param-name>
????????????<param-value>GB2312</param-value>
????????</init-param>
????</filter>
????
????<filter-mapping>
???????<filter-name>struts</filter-name>
???????<url-pattern>/*</url-pattern>
????</filter-mapping>
說明:
(1)該類是FilterDispatcher類的子類。
(2)該類有個成員變量,名為encoding,默認是“GB<?xml:namespace prefix = st1 />2312”。
(3)注意在web.xml中,<filter>標簽里多了<init-param>標簽,顧名思義,它的作用是初始化一個參數,里面定義了參數名和參數值。因此,在子類中,需要重寫init方法,其中:
String encodingParam = filterConfig.getInitParameter("encoding");
???就是從web.xml中讀出了參數名為encoding的值,然后賦給子類中的encoding成員。
(4)重寫dofilter方法,加上:
??request.setCharacterEncoding(encoding);
然后再調用父類的dofilter方法,這樣就完成了編碼的轉換。
(5)如果需要使用其它編碼(如“UTF-8”等),只要改變<param-value>中的值即可。
這樣就把struts2.0加入到工程中了。
三、Struts2.0的配置文件
除了在web.xml中配置以外,struts2.0還有幾個自己的配置文件,其中最重要的兩個是struts.properties和struts.xml,都要放到src目錄下。
Struts.properties的原文件可以在struts-core-2.0.x.jar中找到,原名叫default.properties,將其解壓出來,并改名為struts.properties,放到工程的src目錄下,然后還需要修改里面的值。比如:
struts.locale=zh_CN
struts.i18n.encoding=GB2312
修改以后,這樣struts才能認識中文。
再比如:
struts.action.extension=action
這是個默認值,意思說,struts的每個action的后綴都是.action。
修改后的struts.properties文件在ftp上。
Struts.xml文件用于配置所有的action,在后文有詳細的配置方法。
四、創建JavaBean
創建com.bean包,確定需要輸入的個人信息有“姓名”、“性別”、“地址”、“電話”,因此先定義一個JavaBean,名為Person.java,放到com.bean包中,它包括四個成員以及相應的get、set方法:(待續)
posted on 2007-09-24 14:48
jadmin 閱讀(89)
評論(0) 編輯 收藏