`

【struts】web.xml的配置

    博客分类:
  • SSH
阅读更多
1. web.xml

       web.xml文件对任何的Web项目都是一个必须的文件,使用Struts时,还需要对该文件进行一些必须的配置。
1.1 ActionServlet的配置

一般需要在该文件中配置Struts的Servlet,示例配置如下:

Eg1. 简单的Struts的ActionServlet的配置:

view plaincopy to clipboardprint?

    <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> 
     
        <init-param> 
     
          <param-name>debug</param-name> 
     
          <param-value>3</param-value> 
     
        </init-param> 
     
        <init-param> 
     
          <param-name>detail</param-name> 
     
          <param-value>3</param-value> 
     
        </init-param> 
     
        <load-on-startup>0</load-on-startup> 
     
     </servlet> 
     
     <servlet-mapping> 
     
        <servlet-name>action</servlet-name> 
     
        <url-pattern>*.do</url-pattern> 
     
     </servlet-mapping> 

  对于复杂的应用,一般需要配置多个struts-config.xml文件,可以通过添加另外的<init-param>来实现,或者在多个配置文件中以为“,”隔开,如下所示:

Eg2. 配置多个struts-config.xml配置文件的ActionServlet的配置:

view plaincopy to clipboardprint?

    <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> 
     
                       <init-param> 
     
                                <param-name>config/IVR</param-name> 
     
                                <param-value>/WEB-INF/struts-config-IVR.xml</param-value> 
     
                       </init-param> 
     
                       <init-param> 
     
                       <param-name>config/wap</param-name> 
     
                                <param-value> 
     
                                         /WEB-INF/struts-config-wap.xml 
     
                                </param-value> 
     
                       </init-param> 
     
                       <init-param> 
     
                                <param-name>debug</param-name> 
     
                                <param-value>3</param-value> 
     
                       </init-param> 
     
                       <init-param> 
     
                                <param-name>detail</param-name> 
     
                                <param-value>3</param-value> 
     
                       </init-param> 
     
                       <load-on-startup>0</load-on-startup> 
     
             </servlet> 
     
    <servlet-mapping> 
     
                   <servlet-name>action</servlet-name> 
     
                   <url-pattern>*.do</url-pattern> 
     
          </servlet-mapping> 

1.2 欢迎和错误处理的配置

       首先讲述一下欢迎文件清单<welcome-file-list>的配置,该元素可包含多个<welcome-file>子元素,当Web容器调用欢迎界面时,将首先查看第一个<welcome-file>子元素中定义的文件是否存在,若存在,则将其返回给用户,若不存在,继续判断第二个<welcome-file>子元素中定义的文件……,配置示例如下:

view plaincopy to clipboardprint?

    <welcome-file-list> 
     
                       <welcome-file>index.html</welcome-file> 
     
                       <welcome-file>index.jsp</welcome-file> 
     
                       <welcome-file>default.jsp</welcome-file> 
     
             </welcome-file-list> 

接着讲述一下在web.xml中如何配置错误处理,这时需要使用<error-page>元素,该元素可以根据异常的类型来配置跳转的页面,还可以根据错误码来配置跳转页面,配置示例如下:

view plaincopy to clipboardprint?

    <!-- 根据错误码进行跳转--> 
     
    <error-page>  
     
    <error-code>500</error-code>  
     
    <location>/error.jsp</location>  
     
    </error-page> 
     
    <!-- 根据异常进行跳转--> 
     
    <error-page>  
     
    <exception-type>java.lang.NullException</exception-type>  
     
    <location>/error.jsp</location>  
     
    </error-page> 

1.3 tld文件的配置

       若Web工程没有使用Struts的标签库,可以不在web.xml中使用Struts的标签库信息。当然若开发人员使用了struts的标签库,也可以直接在jsp页面中引入标签库,例如通过如下方式引入:
view plaincopy to clipboardprint?

    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> 
     
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> 
     
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%> 
     
    <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%> 

在Struts中进行配置的的好处是因为可以在Struts中配置为tld文件配置一个简要的名称或者更加易懂的名称,例如在web.xml文件中增加如下配置:
view plaincopy to clipboardprint?

    <taglib> 
     
        <taglib-uri>/tags/struts-bean</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> 
     
     </taglib> 
     
     <taglib> 
     
        <taglib-uri>/tags/struts-html</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-html.tld</taglib-location> 
     
     </taglib> 
     
     <taglib> 
     
        <taglib-uri>/tags/struts-logic</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> 
     
     </taglib> 
     
     <taglib> 
     
        <taglib-uri>/tags/struts-nested</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-nested.tld</taglib-location> 
     
     </taglib> 

  

    其中<taglib-uri>元素指定标签库的相对或者绝对URI地址,Web应用将根据这一URI来访问标签库;<taglib-location>元素指定标签库描述文件在文件资源系统中的物理位置。

此时在jsp页面通过如下方面引入标签库:

view plaincopy to clipboardprint?

    <%@ taglib uri="/tags/struts-bean " prefix="bean"%> 
     
    <%@ taglib uri="/tags/struts-html" prefix="html"%> 
     
    <%@ taglib uri="/tags/struts-logic " prefix="logic"%> 
     
    <%@ taglib uri="/tags/struts-nested " prefix="nested"%> 

1.4 完整配置实例

       下面举一个使用Struts的Web项目的web.xml的简单配置实例(该实例开发人员也参考struts-1.2.8-bin.zip包的webapps目录下的struts-mailreader.war),内容如下所示:
view plaincopy to clipboardprint?

    <?xml version="1.0" encoding="ISO-8859-1"?> 
     
    <!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>Struts Example Application</display-name> 
     
     <!-- 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, /WEB-INF/struts-config-registration.xml</param-value> 
     
        </init-param> 
     
        <load-on-startup>1</load-on-startup> 
     
     </servlet> 
     
     <!-- Action Servlet Mapping --> 
     
     <servlet-mapping> 
     
        <servlet-name>action</servlet-name> 
     
        <url-pattern>*.do</url-pattern> 
     
     </servlet-mapping> 
     
     <!-- 欢迎列表--> 
     
     <welcome-file-list> 
     
        <welcome-file>index.jsp</welcome-file> 
     
     </welcome-file-list> 
     
     <!-- 错误处理 --> 
     
     <error-page> 
     
       <exception-type>java.lang.Exception</exception-type> 
     
       <location>/error.jsp</location> 
     
     </error-page> 
     
     <taglib> 
     
        <taglib-uri>/tags/struts-bean</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> 
     
     </taglib> 
     
     <taglib> 
     
        <taglib-uri>/tags/struts-html</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-html.tld</taglib-location> 
     
     </taglib> 
     
     <taglib> 
     
        <taglib-uri>/tags/struts-logic</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> 
     
     </taglib> 
     
     <taglib> 
     
        <taglib-uri>/tags/struts-nested</taglib-uri> 
     
        <taglib-location>/WEB-INF/struts-nested.tld</taglib-location> 
     
     </taglib> 
     
    </web-app> 
分享到:
评论

相关推荐

    struts.xml和applicationContext.xml、web.xml的配置

    在Java Web开发中,`struts.xml`, `applicationContext.xml` 和 `web.xml` 是三个至关重要的配置文件,它们各自负责不同的职责,并协同工作来构建一个完整的应用框架。以下是关于这三个配置文件的详细说明。 首先,...

    spring在web.xml中和在struts中的不同配置..pdf

    在本文中,我们将探讨Spring在`web.xml`中的配置与在Struts中的配置差异,以及这两种配置方式背后的基本原理。 首先,Spring的核心是ApplicationContext,它是一个管理Bean的容器,可以看作是应用程序的上下文环境...

    struts1中web.xml配置详解

    struts1 中 web.xml 配置详解 struts1 框架是一种基于 Java 语言的 Web 应用程序开发框架,它提供了一个灵活的、可扩展的框架来开发基于 Web 的应用程序。在 struts1 框架中,web.xml 文件是必不可少的配置文件之一...

    struts2.5 web.xml配置

    ### Struts2.5 Web.xml配置概述 #### 1. **XML声明和命名空间** - **XML声明**:`&lt;?xml version="1.0" encoding="UTF-8"?&gt;` 定义了文档类型为XML,并指定了编码方式。 - **根元素**:`&lt;web-app&gt;` 元素是`web.xml`...

    sruts2.0 web.xml 配置文件

    6. **Action的映射(标签)**:在Struts2.0中,`web.xml`里不直接配置Action,而是通过配置文件(如struts-default.xml, struts-plugin.xml, struts.xml等)来定义Action的配置。 除了上述基本配置外,`web.xml`还...

    在web.xml中配置action或.do

    在早期的Struts框架中(Struts 1),开发者经常需要在`web.xml`中配置action映射来处理HTTP请求。随着技术的发展,虽然现代Web框架如Spring MVC等提供了更为灵活和强大的机制来管理这些操作,但了解如何在`web.xml`...

    struts.xml和struts.properties配置详解

    在Struts中,`struts.xml`和`struts.properties`文件是两个核心的配置文件,它们分别负责定义应用的行为和设置全局属性。 **`struts.xml`配置详解** `struts.xml`是Struts 2框架的核心配置文件,用于定义动作映射...

    struts2的web.xml配置文件

    以前和struts2一起使用的配置文件,struts2的核心控制器

    Struts框架中struts-config.xml文件配置小结

    ### Struts框架中struts-config.xml文件配置详解 #### 一、引言 在Java Web开发领域,Struts是一个非常重要的MVC(Model-View-Controller)框架,它极大地简化了Web应用程序的开发过程。而在Struts框架中,`struts...

    Struts-config.xml配置详解

    Struts的配置文件通常命名为struts-config.xml,它是整个Struts应用的核心配置文件,通过定义一系列的XML元素来设定框架的不同功能和行为。下面将详细介绍struts-config.xml中8个主要配置元素的功能和使用方法。 1....

    struts.xml文件详解.doc

    struts.properties中的属性也可以在web.xml或struts.xml中进行配置。在web.xml中,你可以使用"init-param"标签,而在struts.xml中,你可以使用"constant"标签来设置这些属性。这样做提供了更大的灵活性,让开发者...

    Struts struts-config.xml配置

    ### Struts struts-config.xml配置详解 #### 一、引言 在Java Web开发领域,Struts框架一直是构建MVC架构应用的重要工具之一。而`struts-config.xml`配置文件则是Struts应用的核心配置文件,它负责管理Struts应用中...

    flex4,struts2.3兼容配置web.xml

    flex4,struts2.3兼容配置web.xml

    javaweb项目中web.xml的作用

    web.xml是javaweb项目中一个非常重要的配置文件,它是每一个javaWeb工程都必需的配置文件。web.xml文件的主要作用是用于初始化工程配置信息,例如welcome页面、filter、listener、servlet、servlet-mapping、启动...

    struts-config.xml

    `struts-config.xml`是Struts框架的核心配置文件,它定义了应用的各个组件及其交互方式。下面将详细介绍这个配置文件的主要元素和子元素。 ### 主要元素 1. **`&lt;data-sources&gt;`**: 这个元素用于配置数据源,通常...

    Struts2.5版本struts.xml与web.xml配置的更改方法

    struts.xml配置文件头部信息的DTD改为最新** Struts2.5版本的`struts.xml`配置文件需要使用最新的DTD(Document Type Definition),以确保解析正确。将头部信息的DTD链接更改为: ```xml &lt;?xml version="1.0" ...

    struts、applicationContext配置文件移动后web.xml配置示例

    使用myeclipse8.5搭建SSH后,将struts.xml和applicationContext.xml移动到别的地方,示例中为webroot下的config文件夹中,web.xml中需要做的修改示例。其中对于返回上一层方式不同的myeclipse可能不同,如有的用../...

    struts2 使用注解现在零配置不需要在使用struts.xml配置文件,可以直接跑

    Struts2是一款强大的Java Web...通过合理利用这些注解,你可以创建出结构清晰、易于维护的Struts2项目,而无需担心过多的XML配置带来的困扰。在实际开发中,结合Spring等框架,还能进一步提升应用的灵活性和可扩展性。

    struts-config.xml配置文件详解

    Struts-config.xml 配置文件详解 Struts-config.xml 是 Struts 框架的主要配置文件,用于配置 Struts 应用程序的...通过了解 Struts-config.xml 文件的结构和配置方式,可以更好地使用 Struts 框架开发 Web 应用程序。

    struts.xml配置详解

    struts.xml文件的配置对于整个Web应用来说是基础性的,它定义了应用的执行流程和模块划分。正确和合理地配置struts.xml文件,可以使得Web应用结构更加清晰,也更易于后续的维护和升级。在实际的开发过程中,掌握...

Global site tag (gtag.js) - Google Analytics