• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

创建Web应用和Struts框架的配置文件

servlet/jsp 搞代码 7年前 (2018-06-18) 176次浏览 已收录 0个评论

    创建web应用的配置文件

    对于Struts应用,它的配置文件web.xml应该对ActionServlet类进行配置,此外,还应该声明Web应用所使用的Struts标签库,本例中声明使用了三个标签库: Struts Bean、Struts html和Struts Logic标签库。例程2-7为web.xml的源代码。

例程2-7  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>

http://www.gaodaima.com/?p=44571创建Web应用和Struts框架的配置文件

    创建Struts框架的配置文件

    正如前面提及的,Struts框架允许把应用划分成多个组件,提高开发速度。而Struts框架的配置文件struts-config.xml可以把这些组件组装起来,决定如何使用它们。例程2-8是helloapp应用的struts-config.xml文件的源代码。

例程2-8  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和消息资源文件进行了配置,首先通过<form-bean>元素配置了一个ActionForm Bean,名叫HelloForm,它对应的类为hello.HelloForm:
<form-bean name=”HelloForm” type=”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>

    <action>元素的path属性指定请求访问Action的路径,type属性指定Action的完整类名,name属性指定需要传递给Action的ActionForm Bean,scope属性指定ActionForm Bean的存放范围,validate属性指定是否执行表单验证,input属性指定当表单验证失败时的转发路径。<action>元素还包含一个<forward>子元素,它定义了一个请求转发路径。

    本例中的 <action>元素配置了HelloAction组件,对应的类为hello.HelloAction,请求访问路径为”HelloWorld”,当Action类被调用时,Struts框架应该把已经包含表单数据的HelloForm Bean传给它。HelloForm Bean存放在request范围内,并且在调用Action类之前,应该进行表单验证。如果表单验证失败,请求将被转发到接收用户输入的网页hello.jsp,让用户纠正错误。

    struts-config.xml文件最后通过元素定义了一个Resource Bundle: <message-resources parameter=”hello.application”/>

    <message-resources>元素的parameter属性指定Resource Bundle使用的消息资源文件。本例中parameter属性为”hello.application”,表明消息资源文件名为”application.properties”,它的存放路径为WEB-INF/classes/hello/application.properties。

本文选自飞思图书《精通Struts:基于MVC的java Web设计与开发》

欢迎大家阅读《创建Web应用和Struts框架的配置文件》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:创建Web应用和Struts框架的配置文件

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址