• Struts1.2总结,web.xml、.properties资源文件的配置


    1. web.xml

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

    1.1 ActionServlet的配置

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

    Eg1. 简单的StrutsActionServlet的配置:

    <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的配置:

    <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>子元素中定义的文件……,配置示例如下:

    <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>元素,该元素可以根据异常的类型来配置跳转的页面,还可以根据错误码来配置跳转页面,配置示例如下:

    <!-- 根据错误码进行跳转-->

    <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页面中引入标签库,例如通过如下方式引入:

    <%@ 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文件中增加如下配置:

    <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页面通过如下方面引入标签库:

    <%@ 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 完整配置实例

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

    <?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>

    2 .properties资源文件

           默认情况下,Struts默认的资源文件为ApplicationResources.properties文件。

    资源文件可为一个束,可针对不同的语言,写不同的资源文件,开发人员可以定义英文、简体中文、繁体中文、日文等资源文件,资源文件的命名格式为:资源文件名_国别_语言.properties,常见的资源文件名称为:

           ApplicationResources.properties:默认资源文件,或英文资源文件

           ApplicationResources_zh_CN.properties:简体中文

           ApplicationResources_zh_TW.properties:繁体中文

           每一个资源文件都是“键-值”对的集合,例如可在src目录下的资源文件ApplicationResources.properties中,编写如下内容:

    UserForm.userName = USERNAME

    UserForm.password = PASSWORD

           同时在src目录下建立ApplicationResources_zh_CN.properties.bak文件来编写简体中文的原始资源文件,内容如下:

    UserForm.userName = 用户名

    UserForm.password = 密码

           开发人员还可以建立ApplicationResources_zh_TW.properties.bak文件编写繁体中文的原始资源文件。其内容如下:

    UserForm.userName = ノめ?W

    UserForm.password = ノめ?W

           对于简体中文和繁体中文文件,开发人员还需要对其使用native2ascii工具对其进行转换,将非ASCII码转换为Unicode编码,native2ascii工具在%JAVA_HOME%/bin目录下的native2ascii.exe,因此若要进行这种转换,读者首先需要安装了该工具。安装了该工具之后,进入其所在目录,运行native2ascii…命令可进行编码转换,为了能在命令行直接使用该命令,读者还需要在系统变量PATH中添加路径:%JAVA_HOME%"bin

           在实际项目中,一般编写一个批处理文件(eg. code.bat)来处理资源文件的编码,例如code.bat为如下内容时,可满足要求将如上的ApplicationResources_zh_CN.properties.bak文件进行编码,并将编码后的信息放入ApplicationResources_zh_CN.properties文件中。该批处理文件的内容如下所示:

    del ApplicationResources_zh_CN.properties

    copy ApplicationResources_zh_CN.properties.bak ApplicationResources_zh_CN.properties.gbk

    native2ascii -encoding GBK ApplicationResources_zh_CN.properties.gbk ApplicationResources_zh_CN.properties

    del ApplicationResources_zh_CN.properties.gbk

    del ApplicationResources_zh_TW.properties

    copy ApplicationResources_zh_TW.properties.bak ApplicationResources_zh_TW.properties.big5

    native2ascii -encoding Big5 ApplicationResources_zh_TW.properties.big5 ApplicationResources_zh_TW.properties

    del ApplicationResources_zh_TW.properties.big5

    del *.bak.bak

           运行code.bat之后,可看到目录下多出了两个资源文件,即:ApplicationResources_zh_CN.propertiesApplicationResources_zh_TW.properties。其中ApplicationResources_zh_CN.properties的内容是经过编码的,内容如下:

    UserForm.userName = "u7528"u6237"u540d

    UserForm.password = "u5bc6"u7801

           jsp页面中,可以通过Struts的自定义标签来获取资源文件的某个键(例如:UserForm.userName)的信息。示例如下:

    <bean:message key="UserForm.userName"/>

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    C# IL语法
    设计模式学习笔记(1)之单例模式
    设计模式学习笔记(3)之策略设计模式(Strategy)
    ORACLE与SQL SERVER语法区别
    浅谈测试驱动开发(TDD)
    面向对象的5条基本设计原则
    干法读后感磨练灵魂 提升心志
    Linux系统管理之硬盘管理
    Linux硬件信息采集
    Linux iptables
  • 原文地址:https://www.cnblogs.com/shipeng22022/p/4614102.html
Copyright © 2020-2023  润新知