struts2 配置

计应134(实验班)   韩凯丽

一.Struts2配置

  Struts2框架中涉及两个重要的文件:

   1).struts.xml文件

   2).web.xml文件

1.Struts2的主要组件:

组  

作    

 Filterispatcher

起中央控制器作用的过滤器

Action

处于Model层的Action,调用JavaBean实现业务逻辑

Struts.xml

核心配置文件,配置有Action、Result等

2.Struts2框架配置文件:

文件名

文件路径

作     

是否必须

Web.xml

/WEB-INF/

描述Web部署,包括所有必须的框架组件,由开发人员编写

Struts.xml

/WEB-INF/classe(一般直接在src中定义)

核心配置文件,包括Result映射、Action映射、拦截器配置等,由开发人员编写

Struts-default.xml

/WEB-INF/lib/struts2-core.jar

Struts2提供的默认配置,有框架提供

Struts-pugin.xml

/WEB-INF/lib/struts2-xxx-plugin.jar

Struts2框架的插件所用的配置,由插件提供

 

3.web.xml配置:

核心代码如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- 配置struts2框架的核心Filter -->

    <filter>

        <!-- 配置struts2核心的Filter的名字 -->

        <filter-name>struts2</filter-name>

        <!-- 配置Struts2核心的Filter的实现类 -->

        <filter-class>

    org.apache.struts2.dispatcher.FilterDispatcher

        </filter-class>

    </filter>

    <!-- 配置Filter的映射 -->

   <filter-mapping>

       <!-- 指定Filter的名称 -->

      <filter-name>struts2</filter-name>

       <!-- 指定对应Filter的访问路径 -->

       <url-pattern>/ *</url-pattern>

   </filter-mapping>

   </web-app>

2.struts.xm配置文件:

在struts2中一些配置(比如常量)可以同时在struts-default.xml(只读性),strtus-plguin.xml(只读性),struts.xml,struts.properties和web.xml文件中配置,它们的优先级逐步升高,即是说后面的配置会覆盖掉前面相同的配置。

2.配置形式

下面以对struts.i18n.encoding=UTF-8的配置为例进行说明:

在struts.xml配置形式如下:

[html] view plaincopy

  1. <constant name="struts.i18n.encoding" value="gbk"></constant>  

在struts.properties的配置形式如下:

[html] view plaincopy

  1. struts.i18n.encoding=UTF-8  

在web.xml中配置如下:

[html] view plaincopy

  1. <filter>  
  2.   
  3. <filter-name>struts2</filter-name>  
  4.   
  5. <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  6.   
  7. </filter-class>  
  8.   
  9. <init-param>  
  10. 10.   

11. <param-name>struts.i18n.encoding</param-name>  

  1. 12.   

13. <param-value>UTF-8</param-value>  

  1. 14.   

15. </init-param>  

  1. 16.   

17. </filter>  

说明:官方声称配置了此常量可以解决中文乱码问题,但实事上并不能达到目的,在前面的三个项目中,如果我们在表单中输入中文,其结果是会出现乱码。解决此问题参看[一.7的注意]。这是struts2.1.6中的一bug,它的下一版2.1.8已解决此问题。

3.package配置相关

属性名

是否必须

  

Name

Package的唯一标识,不允许同名

Extends 

指定要继承的包

Namespace

指定名称空间

Abstract

声明包为抽象否

一常量配置

1. 在struts2中配置常量的方式有三种:

       X 在struts.xml文件中配置 
       X 在web.xml文件中配置  
       X 在sturts.propreties文件中配置

     注意: 1.之所以使用struts.propreties文件配置,是因为为了保持与WebWork的向后兼容

            2.在实际开发中,在web.xml中配置常量相比其他两种,需要更多的代码量,会降低了web.xml的可读性

            3.通常推荐在struts.xml文件中配置struts2的常量,而且便于集中管理  

2. 在sturt2中搜索加载常量的顺序是:

     struts-default.xml      (在struts2-core-2.0.6.jar文件中)
     struts-plugin.xml      (在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中)
     struts.xml             (Web应用默认的Struts2的配置文件)
     sturts.propreties       (Web应用默认的Struts2的配置文件)
     web.xml                 (Web应用下的配置文件)

    注意:1.若在不同的配置文件中同时配置了相同的Struts2常量,则后一个配置文件的常量值覆盖前一个配置的常量值

3. 配置文件的属性:name和value

         name:指定属性的常量名
   value:指定属性的值

   注意:1.在不同的配置文件中配置常量,虽然方式各异,但是都必须指定name、value这两个属性

Xml代码  
  1. <!-指定struts2的SilterDispatcher的Filter-!>  
  2. <filter>  
  3.   <!-指定Struts2的核心Filter-!>  
  4.     
  5.     <filter-name>strurs2</filter-name>  
  6.     <filter-class>org.apache.struts2.dispatcher.FilterDispaycher  
  7.     </filter-class>  
  8.   
  9.     <!-通过init-param元素配置struts2常量-!>  
  10.   
  11.     <init-param>  
  12.         <param-name>struts.custom.il8n.resources</param-name>  
  13.         <param-va>

   常量详解

Xml代码  " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->  
  8.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  9.   
  10.     <!--  
  11.         该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。  
  12.         如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。  
  13.     -->  
  14.     <constant name="struts.action.extension" value="do" />  
  15.   
  16.     <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->  
  17.     <constant name="struts.serve.static.browserCache" value="false" />  
  18.   
  19.     <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->  
  20.     <constant name="struts.configuration.xml.reload" value="true" />  
  21.   
  22.     <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->  
  23.     <constant name="struts.devMode" value="true" />  
  24.   
  25.     <!-- 默认的视图主题 -->  
  26.     <constant name="struts.ui.theme" value="simple" />  
  27.   
  28.     <!-- spring 托管 -->  
  29.     <constant name="struts.objectFactory" value="spring" />  
  30.   
  31.     <!--  
  32.         指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration  
  33.         开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。  
  34.     -->  
  35.     <constant name="struts.configuration"  
  36.         value="org.apache.struts2.config.DefaultConfiguration" />  
  37.   
  38.     <!-- 设置默认的locale和字符编码 -->  
  39.     <constant name="struts.locale" value="zh_CN" />  
  40.     <constant name="struts.i18n.encoding" value="GBK" />  
  41.   
  42.     <!-- 指定Struts的工厂类 -->  
  43.     <constant name="struts.objectFactory" value="spring"></constant>  
  44.   
  45.     <!--  
  46.         指定spring框架的装配模式,装配方式有: name, type, auto, and constructor (name  
  47.         是默认装配模式)  
  48.     -->  
  49.     <constant name="struts.objectFactory.spring.autoWire" value="name" />  
  50.   
  51.     <!-- 该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true -->  
  52.     <cosntant name="struts.objectFactory.spring.useClassCache" />  
  53.   
  54.     <!-- 指定类型检查,包含tiger和notiger -->  
  55.     <cosntant name="struts.objectTypeDeterminer" value="tiger" />  
  56.   
  57.     <!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 -->  
  58.     <constant name="struts.multipart.parser" value="cos" />  
  59.     <constant name="struts.multipart.parser" value="pell" />  
  60.     <constant name="struts.multipart.parser" value="jakarta" />  
  61.   
  62.     <!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir -->  
  63.     <constant name="struts.multipart.saveDir" value="/tmpuploadfiles" />  
  64.   
  65.     <!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 -->  
  66.     <constant name="struts.multipart.maxSize" value="2097152" />  
  67.   
  68.     <!--  
  69.         该属性指定Struts2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖  
  70.         struts.properties文件中指定的属性。如果需要加载多个自定义属性文件,多个自定义属性文  
  71.         件的文件名以英文逗号(,)隔开。(也就是说不要改写struts.properties!)  
  72.     -->  
  73.     <constant name="struts.custom.properties"  
  74.         value="application,org/apache/struts2/extension/custom" />  
  75.   
  76.     <!-- 
  77.         指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper 
  78.     -->  
  79.     <constant name="struts.mapper.class"  
  80.         value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />  
  81.   
  82.     <!-- 指定action的后缀,默认为action -->  
  83.     <constant name="struts.action.extension" value="do" />  
  84.   
  85.     <!-- 被 FilterDispatcher使用指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true. -->  
  86.     <constant name="struts.serve.static.browserCache" value="true" />  
  87.   
  88.     <!-- 设置是否支持动态方法调用,true为支持,false不支持. -->  
  89.     <constant name="struts.enable.DynamicMethodInvocation" value="true" />  
  90.   
  91.     <!-- 设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true. -->  
  92.     <constant name="struts.enable.SlashesInActionNames" value="true" />  
  93.   
  94.     <!-- 是否允许使用表达式语法,默认为true. -->  
  95.     <constant name="struts.tag.altSyntax" value="true" />  
  96.   
  97.     <!-- 设置当struts.xml文件改动时,是否重新加载 -->  
  98.     <cosntant name="struts.configuration.xml.reload" value="true" />  
  99.   
  100.     <!-- 设置struts是否为开发模式,默认为false,测试阶段一般设为true. -->  
  101.     <cosntant name="struts.devMode" value="true" />  
  102.   
  103.     <!-- 设置是否每次请求,都重新加载资源文件,默认值为false. -->  
  104.     <cosntant name="struts.i18n.reload" value="false" />  
  105.   
  106.     <!-- 标准的UI主题,默认的UI主题为xhtml,可以为simple,xhtml或ajax -->  
  107.     <cosntant name="struts.ui.theme" value="xhtml" />  
  108.   
  109.     <!-- 模板目录 -->  
  110.     <cosntant name="struts.ui.templateDir" value="template" />  
  111.   
  112.     <!-- 设置模板类型. 可以为 ftl, vm, or jsp -->  
  113.     <cosntant name="struts.ui.templateSuffix" value="ftl" />  
  114.   
  115.     <!-- 定位velocity.properties 文件. 默认velocity.properties -->  
  116.     <cosntant name="struts.velocity.configfile" value="velocity.properties" />  
  117.   
  118.     <!-- 设置velocity的context. -->  
  119.     <cosntant name="struts.velocity.contexts" value="...." />  
  120.   
  121.     <!-- 定位toolbox -->  
  122.     <cosntant name="struts.velocity.toolboxlocation" value="...." />  
  123.   
  124.     <!-- 指定web应用的端口 -->  
  125.     <cosntant name="struts.url.http.port" value="80" />  
  126.   
  127.     <!-- 指定加密端口 -->  
  128.     <cosntant name="struts.url.https.port" value="443" />  
  129.   
  130.     <!-- 设置生成url时,是否包含参数.值可以为: none,get or all -->  
  131.     <cosntant name="struts.url.includeParams" value="get" />  
  132.   
  133.     <!-- 设置要加载的国际化资源文件,以逗号分隔. -->  
  134.     <cosntant name="struts.custom.i18n.resources" value="application" />  
  135.   
  136.     <!--  
  137.         对于一些web应用服务器不能处理HttpServletRequest.getParameterMap(), 像  
  138.         WebLogic,Orion, and OC4J等,须设置成true,默认为false.  
  139.     -->  
  140.     <cosntant name="struts.dispatcher.parametersWorkaround" value="false" />  
  141.   
  142.     <!-- 指定freemarker管理器 -->  
  143.     <cosntant name="struts.freemarker.manager.classname"  
  144.         value="org.apache.struts2.views.freemarker.FreemarkerManager" />  
  145.   
  146.     <!-- 设置是否对freemarker的模板设置缓存,效果相当于把template拷贝到 WEB_APP/templates. -->  
  147.     <cosntant name="struts.freemarker.templatesCache" value="false" />  
  148.   
  149.     <!-- 通常不需要修改此属性. -->  
  150.     <cosntant name="struts.freemarker.wrapper.altMap" value="true" />  
  151.   
  152.     <!-- 指定xslt result是否使用样式表缓存.开发阶段设为true,发布阶段设为false. -->  
  153.     <cosntant name="struts.xslt.nocache" value="false" />  
  154.   
  155.     <!-- 设置struts自动加载的文件列表. -->  
  156.     <cosntant name="struts.configuration.files"  
  157.         value="struts-default.xml,struts-plugin.xml,struts.xml" />  
  158.   
  159.     <!-- 设定是否一直在最后一个slash之前的任何位置选定namespace. -->  
  160.     <cosntant name="struts.mapper.alwaysSelectFullNamespace"  
  161.         value="false" />  
  162. </struts>  

二、包配置

1. 图片解释:

     1.struts2框架的核心组件是 Action和拦截器等,struts2框架使用包来管理Action和拦截器等的

     2.每个包就是多个Action、多个拦截器、多个拦截器引用的集合

2. 抽象包:除了正常的包之外,struts2还提供了抽象包

         定义:不包含action定义的包

         区分:该包的package元素增加abstract="true"

3. 包配置:

      在struts.xml中,使用package元素配置包的信息,每个package元素定义一个包的配置

      package元素可以定义的属性:

         name:必填属性,指定该包的名字,是引用该包的key

         extends:可选属性,指定该包是否可以继承其他的包:可以继承一个或多个父包的中Action定义、烂机器定义、拦截器栈等配置

         namespace:可选属性,定义该包的命名空间

         abstract:可选属性,指定该包是个抽象包,包中不能有Action的定义

    注意:Struts2的配置文件是从上到下处理的,所以父包应该在子包的前面~!(其实所有xml文件都是自上往下顺序执行的)

4. 理解示例:

Xml代码  showcase.jsp /date.jsp /empmanager/editSkill.jsp /empmanager/editSkill.jsp edit.action?skillName=${currentSkill.name} " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> 收藏代码
  1. <struts>  
  2.     <!-- 配置第一个包,该包名为defalut,继承struts-defalut -->  
  3.     <package name="default" extends="struts-defalut">  
  4.   
  5.         <!-- 下面定义了拦截器部分-->  
  6.         <interceptors>  
  7.   
  8.             <!--定义拦截器栈-->  
  9.             <interceptor-stack name="crudStack">  
  10.                 <interceptor-ref name="params" />  
  11.                 <interceptor-ref name="defalutStack" />  
  12.             </interceptor-stack>  
  13.         </interceptors>  
  14.   
  15.         <default-action-ref name="showcase" />  
  16.   
  17.         <!--定义一个Action,该Action直接映射到showcase.jsp页面-->  
  18.         <action name="Showcase">  
  19.             <result> showcase.jsp</result>  
  20.         </action>  
  21.   
  22.         <!--定义一个Action,该Action类为com.opensymphony.webwork.showcase.DateAction-->  
  23.         <action name="Date" class="lee.DateAction">  
  24.             <result name="success">/date.jsp </result>  
  25.         </action>  
  26.     </package>  
  27.   
  28.     <!--配置第二个包,该包名为skill,该包继承default包-->  
  29.     <package name="skill" extends="defalut" name="/skill">  
  30.   
  31.         <!-- 定义默认你的拦截器引用-->   
  32.                 <defalut-interceptor-ref name="crudStack"/>  
  33.   
  34.         <!--定义名为Edit的Action,该类对应Action对应的处理类为lee.SkillAction 
  35. -->  
  36.         <action name="Edit" class="lee.SkillAction">  
  37.             <result>/empmanager/editSkill.jsp</result>  
  38.             <intercepor-ref name="params" />  
  39.             <interceptor-ref name="basicStack" />  
  40.   
  41.         </action>  
  42.   
  43.         <!--定义名为Save的Action,该Action对应的处理类为lee.SkillAction,使用save方法作为处理方法-->  
  44.         <action name="Save" class="lee.SkillAction" method="save">  
  45.             <result name="input">/empmanager/editSkill.jsp</result>  
  46.             <result type="redirect">edit.action?skillName=${currentSkill.name}  
  47.             </result>  
  48.         </action>  
  49.     </package>  
  50. </struts>    

三、命名空间配置

1.不使用命名空间的方式:

struts配置:

Xml代码  /success.jsp /index.jsp /index.jsp " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> 收藏代码
  1. <struts>  
  2.     <include file="struts-default.xml" />  
  3.     <package name="com.casc.manager" extends="struts-default"   
  4.         <action name="xxn" class="com.casc.manager.XxnAction">  
  5.             <result name="success">/success.jsp</result>  
  6.              <result name="error">/index.jsp</result>  
  7.              <result name ="input" >/index.jsp</result >   
  8.         </action>  
  9.           
  10.     </package>  
  11. </struts>  

 

Html代码 
<s:text name=<"/> " quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> 
  1. <form action="xxn.action" method="post">  
  2.         <s:text name="user.name"></s:text><input type="text" name="name"><br>  
  3.           <s:text name="user.password"></s:text><input type="password" name="password"><br>  
  4.           <input type="submit" value="<s:text name="user.submit"/>"/>  
  5. </form>  
 
原文地址:https://www.cnblogs.com/1225hkl/p/4566089.html