創建Web應用的配置文件
對于Struts應用,它的配置文件web.xml應該對ActionServlet類進行配置,此外,還應該聲明Web應用所使用的Struts標簽庫,本例中聲明使用了三個標簽庫: Struts Bean、Struts HTML和Struts Logic標簽庫。例程1為web.xml的源代碼。
例程1 web.xml <?xml version="1.0" encoding="UTF-8"?> <!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 > HelloApp Struts Application </display-name > <!-- Standard 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 </param-value > </init-param > < load-on-startup>2</load-on-startup > </servlet> <!-- Standard Action Servlet Mapping --> < servlet-mapping> < servlet-name > action</servlet-name> < url-pattern>*.do</url-pattern> </servlet-mapping> <!-- The Usual Welcome File List --> < welcome-file-list> < welcome-file > hello.jsp</welcome-file> </welcome-file-list> <!-- Struts Tag Library Descriptors --> < taglib > < taglib-uri > /WEB-INF/struts-bean.tld </taglib-uri> < taglib-location> /WEB-INF/struts-bean.tld </taglib-location> </taglib> < taglib > < taglib-uri > /WEB-INF/struts-html.tld </taglib-uri > < taglib-location > /WEB-INF/struts-html.tld </taglib-location > </taglib > < taglib > < taglib-uri > /WEB-INF/struts-logic.tld </taglib-uri > < taglib-location > /WEB-INF/struts-logic.tld </taglib-location > </taglib > </web-app > |
創建Struts框架的配置文件
正如前面提及的,Struts框架允許把應用劃分成多個組件,提高開發速度。而Struts框架的配置文件struts-config.xml可以把這些組件組裝起來,決定如何使用它們。例程2是helloapp應用的struts-config.xml文件的源代碼。
例程2 struts-config.xml |
以上代碼對helloapp應用的HelloForm、HelloAction和消息資源文件進行了配置,首先通過
接著通過元素配置了一個Action組件:
< action |
本例中的
struts-config.xml文件最后通過元素定義了一個Resource Bundle:元素的parameter屬性指定Resource Bundle使用的消息資源文件。本例中parameter屬性為"hello.application",表明消息資源文件名為"application.properties",它的存放路徑為WEB-INF/classes/hello/application.properties。