文:阿蜜果
日期:2008-1-2——1-3
1. web.xml
web.xml文件對(duì)任何的Web項(xiàng)目都是一個(gè)必須的文件,使用Struts時(shí),還需要對(duì)該文件進(jìn)行一些必須的配置。
1.1 ActionServlet的配置
一般需要在該文件中配置Struts的Servlet,示例配置如下:
Eg1. 簡(jiǎn)單的Struts的ActionServlet的配置:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
對(duì)于復(fù)雜的應(yīng)用,一般需要配置多個(gè)struts-config.xml文件,可以通過(guò)添加另外的<init-param>來(lái)實(shí)現(xiàn),或者在多個(gè)配置文件中以為“,”隔開,如下所示:
Eg2. 配置多個(gè)struts-config.xml配置文件的ActionServlet的配置:
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>config/IVR</param-name>
<param-value>/WEB-INF/struts-config-IVR.xml</param-value>
</init-param>
<init-param>
<param-name>config/wap</param-name>
<param-value>
/WEB-INF/struts-config-wap.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
1.2 歡迎和錯(cuò)誤處理的配置
首先講述一下歡迎文件清單<welcome-file-list>的配置,該元素可包含多個(gè)<welcome-file>子元素,當(dāng)Web容器調(diào)用歡迎界面時(shí),將首先查看第一個(gè)<welcome-file>子元素中定義的文件是否存在,若存在,則將其返回給用戶,若不存在,繼續(xù)判斷第二個(gè)<welcome-file>子元素中定義的文件……,配置示例如下:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
接著講述一下在web.xml中如何配置錯(cuò)誤處理,這時(shí)需要使用<error-page>元素,該元素可以根據(jù)異常的類型來(lái)配置跳轉(zhuǎn)的頁(yè)面,還可以根據(jù)錯(cuò)誤碼來(lái)配置跳轉(zhuǎn)頁(yè)面,配置示例如下:
<!-- 根據(jù)錯(cuò)誤碼進(jìn)行跳轉(zhuǎn)-->
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 根據(jù)異常進(jìn)行跳轉(zhuǎn)-->
<error-page>
<exception-type>java.lang.NullException</exception-type>
<location>/error.jsp</location>
</error-page>
1.3 tld文件的配置
若Web工程沒有使用Struts的標(biāo)簽庫(kù),可以不在web.xml中使用Struts的標(biāo)簽庫(kù)信息。當(dāng)然若開發(fā)人員使用了struts的標(biāo)簽庫(kù),也可以直接在jsp頁(yè)面中引入標(biāo)簽庫(kù),例如通過(guò)如下方式引入:
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%>
在Struts中進(jìn)行配置的的好處是因?yàn)榭梢栽?/span>Struts中配置為tld文件配置一個(gè)簡(jiǎn)要的名稱或者更加易懂的名稱,例如在web.xml文件中增加如下配置:
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
其中<taglib-uri>元素指定標(biāo)簽庫(kù)的相對(duì)或者絕對(duì)URI地址,Web應(yīng)用將根據(jù)這一URI來(lái)訪問標(biāo)簽庫(kù);<taglib-location>元素指定標(biāo)簽庫(kù)描述文件在文件資源系統(tǒng)中的物理位置。
此時(shí)在jsp頁(yè)面通過(guò)如下方面引入標(biāo)簽庫(kù):
<%@ taglib uri="/tags/struts-bean " prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic " prefix="logic"%>
<%@ taglib uri="/tags/struts-nested " prefix="nested"%>
1.4 完整配置實(shí)例
下面舉一個(gè)使用Struts的Web項(xiàng)目的web.xml的簡(jiǎn)單配置實(shí)例(該實(shí)例開發(fā)人員也參考struts-1.2.8-bin.zip包的webapps目錄下的struts-mailreader.war),內(nèi)容如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>Struts Example Application</display-name>
<!-- Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml, /WEB-INF/struts-config-registration.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 歡迎列表-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 錯(cuò)誤處理 -->
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
</web-app>
2 .properties資源文件
默認(rèn)情況下,Struts默認(rèn)的資源文件為ApplicationResources.properties文件。
資源文件可為一個(gè)束,可針對(duì)不同的語(yǔ)言,寫不同的資源文件,開發(fā)人員可以定義英文、簡(jiǎn)體中文、繁體中文、日文等資源文件,資源文件的命名格式為:資源文件名_國(guó)別_語(yǔ)言.properties,常見的資源文件名稱為:
ApplicationResources.properties:默認(rèn)資源文件,或英文資源文件
ApplicationResources_zh_CN.properties:簡(jiǎn)體中文
ApplicationResources_zh_TW.properties:繁體中文
每一個(gè)資源文件都是“鍵-值”對(duì)的集合,例如可在src目錄下的資源文件ApplicationResources.properties中,編寫如下內(nèi)容:
UserForm.userName = USERNAME
UserForm.password = PASSWORD
同時(shí)在src目錄下建立ApplicationResources_zh_CN.properties.bak文件來(lái)編寫簡(jiǎn)體中文的原始資源文件,內(nèi)容如下:
UserForm.userName = 用戶名
UserForm.password = 密碼
開發(fā)人員還可以建立ApplicationResources_zh_TW.properties.bak文件編寫繁體中文的原始資源文件。其內(nèi)容如下:
UserForm.userName = ノめ?W
UserForm.password = ノめ?W
對(duì)于簡(jiǎn)體中文和繁體中文文件,開發(fā)人員還需要對(duì)其使用native2ascii工具對(duì)其進(jìn)行轉(zhuǎn)換,將非ASCII碼轉(zhuǎn)換為Unicode編碼,native2ascii工具在%JAVA_HOME%/bin目錄下的native2ascii.exe,因此若要進(jìn)行這種轉(zhuǎn)換,讀者首先需要安裝了該工具。安裝了該工具之后,進(jìn)入其所在目錄,運(yùn)行native2ascii…命令可進(jìn)行編碼轉(zhuǎn)換,為了能在命令行直接使用該命令,讀者還需要在系統(tǒng)變量PATH中添加路徑:%JAVA_HOME%"bin。
在實(shí)際項(xiàng)目中,一般編寫一個(gè)批處理文件(eg. code.bat)來(lái)處理資源文件的編碼,例如code.bat為如下內(nèi)容時(shí),可滿足要求將如上的ApplicationResources_zh_CN.properties.bak文件進(jìn)行編碼,并將編碼后的信息放入ApplicationResources_zh_CN.properties文件中。該批處理文件的內(nèi)容如下所示:
del ApplicationResources_zh_CN.properties
copy ApplicationResources_zh_CN.properties.bak ApplicationResources_zh_CN.properties.gbk
native2ascii -encoding GBK ApplicationResources_zh_CN.properties.gbk ApplicationResources_zh_CN.properties
del ApplicationResources_zh_CN.properties.gbk
del ApplicationResources_zh_TW.properties
copy ApplicationResources_zh_TW.properties.bak ApplicationResources_zh_TW.properties.big5
native2ascii -encoding Big5 ApplicationResources_zh_TW.properties.big5 ApplicationResources_zh_TW.properties
del ApplicationResources_zh_TW.properties.big5
del *.bak.bak
運(yùn)行code.bat之后,可看到目錄下多出了兩個(gè)資源文件,即:ApplicationResources_zh_CN.properties和ApplicationResources_zh_TW.properties。其中ApplicationResources_zh_CN.properties的內(nèi)容是經(jīng)過(guò)編碼的,內(nèi)容如下:
UserForm.userName = "u7528"u6237"u540d
UserForm.password = "u5bc6"u7801
在jsp頁(yè)面中,可以通過(guò)Struts的自定義標(biāo)簽來(lái)獲取資源文件的某個(gè)鍵(例如:UserForm.userName)的信息。示例如下:
<bean:message key="UserForm.userName"/>
參考文檔:
《為Struts應(yīng)用配置web.xml文件》
《Struts國(guó)際化處理》
posted on 2008-01-05 12:23
阿蜜果 閱讀(9680)
評(píng)論(2) 編輯 收藏 所屬分類:
Struts