创建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<?xml version="1.0" encoding="iso-8859-1" ?><!doctype struts-config public"-//apache software foundation//dtd struts configuration 1.1//en""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"><!--this is the struts configuration file |
以上代码对helloapp应用的helloform、helloaction和消息资源文件进行了配置,首先通过
接着通过元素配置了一个action组件:
<action path = "/helloworld" type = "hello.helloaction" name = "helloform" scope = "request" validate = "true" input = "/hello.jsp"><forward name="sayhello" path="/hello.jsp" /></action> |
本例中的
struts-config.xml文件最后通过元素定义了一个resource bundle:元素的parameter属性指定resource bundle使用的消息资源文件。本例中parameter属性为"hello.application",表明消息资源文件名为"application.properties",它的存放路径为web-inf/classes/hello/application.properties。
闽公网安备 35060202000074号