创建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-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 |
<action path = "/helloworld" type = "hello.helloaction" name = "helloform" scope = "request" validate = "true" input = "/hello.jsp"><forward name="sayhello" path="/hello.jsp" /></action> |
元素的path属性指定请求访问action的路径,type属性指定action的完整类名,name属性指定需要传递给action的actionform bean,scope属性指定actionform bean的存放范围,validate属性指定是否执行表单验证,input属性指定当表单验证失败时的转发路径。元素还包含一个
本例中的 元素配置了helloaction组件,对应的类为hello.helloaction,请求访问路径为"helloworld",当action类被调用时,struts框架应该把已经包含表单数据的helloform bean传给它。helloform bean存放在request范围内,并且在调用action类之前,应该进行表单验证。如果表单验证失败,请求将被转发到接收用户输入的网页hello.jsp,让用户纠正错误。
struts-config.xml文件最后通过元素定义了一个resource bundle:元素的parameter属性指定resource bundle使用的消息资源文件。本例中parameter属性为"hello.application",表明消息资源文件名为"application.properties",它的存放路径为web-inf/classes/hello/application.properties。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器