专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » struts框架:创建Web应用和Struts框架配置文件例子 »正文

struts框架:创建Web应用和Struts框架配置文件例子

来源: 发布时间:星期四, 2009年1月15日 浏览:29次 评论:0
  创建Web应用配置文件

  对于Struts应用配置文件web.xml应该对ActionServlet类进行配置此外还应该声明Web应用所使用Struts标签库本例中声明使用了 3个标签库: 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->org.apache.struts.action.ActionServlet</servlet->
<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 for the "Hello!" sample application
-->
<struts-config>
<!-- Form Bean Definitions -->
  <form-beans>
    <form-bean name="HelloForm" type="hello.HelloForm"/>
  </form-beans>
<!-- Action Mapping Definitions = -->
 <action-mappings>
  <!-- Say Hello! -->
  <action  path   = "/HelloWorld"
        type   = "hello.HelloAction"
        name   = "HelloForm"
        scope   = "request"
        validate = "true"
        input   = "/hello.jsp"
   >
    <forward name="SayHello" path="/hello.jsp" />
  </action>
 </action-mappings>
 <!-- Message Resources Definitions -->
 <message-resources parameter="hello.application"/>
</struts-config>


  以上代码对helloapp应用HelloForm、HelloAction和消息资源文件进行了配置首先通过元素配置了个ActionForm Bean名叫HelloForm它对应类为hello.HelloForm:

  接着通过元素配置了个Action组件: 

<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属性指定需要传递给ActionActionForm Beanscope属性指定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/es/hello/application.properties

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: