Strut2配置详解

struts2配置说明

?
struts2配置说明: 2010 - 12 - 31 16 : 38 : 54 |  分类: struts2 |  标签:struts2配置说明     字号:大中小 订阅 .
  
通常struts2加载struts2常量的顺序如下:
  
   
  
1 . struts- default .xml:该文件保存在struts2-core- 2.0 . 6 .jar文件中。
2 . struts-plugin.xml:该文件保存在struts2-Xxx- 2.0 . 6 .jar等Struts2插件JAR文件中。
3 . struts.xml:该文件是Web应用默认的Struts2配置文件。
4 . struts.properties:该文件是Web应用默认的Struts2配置文件。
5 . web.xml:该文件是Web应用的配置文件。
  
如果在多个文件中配置了同一个Struts2常量,则后一个文件中的配置的常量值会覆盖前面文件中配置的常量值。
在不同文件中配置常量的方式是不一样的,但不管哪个文件中,配置Struts2常量都要指定两个属性:常量name和常量value。
  
推荐在struts.xml文件中配置Struts2常量。 
  
此处只加载了前三个配置文件,这是在常量struts.configuration.files中配置的。该属性指定Struts 2 框架默认加载的配置文件,如果需要指定默认加载多个配置文件,则多个配置文件的文件名之间以英文逗号(,)隔开。该属性的默认值为struts- default .xml,struts-plugin.xml,struts.xml,这就是上图中加载的三个配置文件。  
  
Struts2常量的具体用法实例
  
   
  
Xml代码
<?xml version= "1.0" encoding= "UTF-8" ?>   
<!DOCTYPE struts PUBLIC    
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
     
<struts>   
     <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->   
     <constant name= "struts.i18n.encoding" value= "UTF-8" />   
     
     <!--    
         该属性指定需要Struts 2 处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。    
         如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。    
     -->   
     <constant name= "struts.action.extension" value= "do" />   
     
     <!-- 设置浏览器是否缓存静态内容,默认值为 true (生产环境下使用),开发阶段最好关闭 -->   
     <constant name= "struts.serve.static.browserCache" value= "false" />   
     
     <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为 false (生产环境下使用),开发阶段最好打开 -->   
     <constant name= "struts.configuration.xml.reload" value= "true" />   
     
     <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->   
     <constant name= "struts.devMode" value= "true" />   
     
     <!-- 默认的视图主题 -->   
     <constant name= "struts.ui.theme" value= "simple" />   
     
     <!-- spring 托管 -->   
     <constant name= "struts.objectFactory" value= "spring" />   
     
     <!--    
         指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration    
         开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。    
     -->   
     <constant name= "struts.configuration"   
         value= "org.apache.struts2.config.DefaultConfiguration" />   
     
     <!-- 设置默认的locale和字符编码 -->   
     <constant name= "struts.locale" value= "zh_CN" />   
     <constant name= "struts.i18n.encoding" value= "GBK" />   
     
     <!-- 指定Struts的工厂类 -->   
     <constant name= "struts.objectFactory" value= "spring" ></constant>   
     
     <!--    
         指定spring框架的装配模式,装配方式有: name, type, auto, and constructor (name    
         是默认装配模式)    
     -->   
     <constant name= "struts.objectFactory.spring.autoWire" value= "name" />   
     
     <!-- 该属性指定整合spring时,是否对bean进行缓存,值为 true or false ,默认为 true -->   
     <cosntant name= "struts.objectFactory.spring.useClassCache" />   
     
     <!-- 指定类型检查,包含tiger和notiger -->   
     <cosntant name= "struts.objectTypeDeterminer" value= "tiger" />   
     
     <!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 -->   
     <constant name= "struts.multipart.parser" value= "cos" />   
     <constant name= "struts.multipart.parser" value= "pell" />   
     <constant name= "struts.multipart.parser" value= "jakarta" />   
     
     <!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir -->   
     <constant name= "struts.multipart.saveDir" value= "/tmpuploadfiles" />   
     
     <!-- 该属性指定Struts 2 文件上传中整个请求内容允许的最大字节数 -->   
     <constant name= "struts.multipart.maxSize" value= "2097152" />   
     
     <!--    
         该属性指定Struts2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖    
         struts.properties文件中指定的属性。如果需要加载多个自定义属性文件,多个自定义属性文    
         件的文件名以英文逗号(,)隔开。(也就是说不要改写struts.properties!)    
     -->   
     <constant name= "struts.custom.properties"   
         value= "application,org/apache/struts2/extension/custom" />   
              
     <!-- 指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper -->   
     <constant name= "struts.mapper.class" value= "org.apache.struts2.dispatcher.mapper.DefaultActionMapper" />   
     
     <!-- 指定action的后缀,默认为action -->   
     <constant name= "struts.action.extension" value= "do" />   
          
     <!-- 被 FilterDispatcher使用指定浏览器是否缓存静态内容,测试阶段设置为 false ,发布阶段设置为 true . -->   
     <constant name= "struts.serve.static.browserCache" value= "true" />   
     
     <!-- 设置是否支持动态方法调用, true 为支持, false 不支持. -->   
     <constant name= "struts.enable.DynamicMethodInvocation" value= "true" />   
              
     <!-- 设置是否可以在action中使用斜线,默认为 false 不可以,想使用需设置为 true . -->   
     <constant name= "struts.enable.SlashesInActionNames" value= "true" />   
          
     <!-- 是否允许使用表达式语法,默认为 true . -->   
     <constant name= "struts.tag.altSyntax" value= "true" />   
     
     <!-- 设置当struts.xml文件改动时,是否重新加载 -->   
     <cosntant name= "struts.configuration.xml.reload" value= "true" />   
          
     <!-- 设置struts是否为开发模式,默认为 false ,测试阶段一般设为 true . -->   
     <cosntant name= "struts.devMode" value= "true" />   
     
     <!-- 设置是否每次请求,都重新加载资源文件,默认值为 false . -->   
     <cosntant name= "struts.i18n.reload" value= "false" />   
     
     <!-- 标准的UI主题,默认的UI主题为xhtml,可以为simple,xhtml或ajax -->   
     <cosntant name= "struts.ui.theme" value= "xhtml" />   
              
     <!-- 模板目录 -->   
     <cosntant name= "struts.ui.templateDir" value= "template" />   
     
     <!-- 设置模板类型. 可以为 ftl, vm, or jsp -->   
     <cosntant name= "struts.ui.templateSuffix" value= "ftl" />   
     
     <!-- 定位velocity.properties 文件. 默认velocity.properties -->   
     <cosntant name= "struts.velocity.configfile" value= "velocity.properties" />   
          
     <!-- 设置velocity的context. -->   
     <cosntant name= "struts.velocity.contexts" value= "...." />   
          
     <!-- 定位toolbox -->   
     <cosntant name= "struts.velocity.toolboxlocation" value= "...." />   
     
     <!-- 指定web应用的端口 -->   
     <cosntant name= "struts.url.http.port" value= "80" />   
          
     <!-- 指定加密端口 -->           
     <cosntant name= "struts.url.https.port" value= "443" />   
     
     <!-- 设置生成url时,是否包含参数.值可以为: none,get or all -->   
     <cosntant name= "struts.url.includeParams" value= "get" />   
     
     <!-- 设置要加载的国际化资源文件,以逗号分隔. -->   
     <cosntant name= "struts.custom.i18n.resources" value= "application" />   
          
     <!-- 对于一些web应用服务器不能处理HttpServletRequest.getParameterMap(),    
         像 WebLogic,Orion, and OC4J等,须设置成 true ,默认为 false . -->   
     <cosntant name= "struts.dispatcher.parametersWorkaround" value= "false" />      
     
     <!-- 指定freemarker管理器 -->   
     <cosntant name= "struts.freemarker.manager.classname" value= "org.apache.struts2.views.freemarker.FreemarkerManager" />     
     
     <!-- 设置是否对freemarker的模板设置缓存,效果相当于把template拷贝到 WEB_APP/templates. -->   
     <cosntant name= "struts.freemarker.templatesCache" value= "false" />        
     
     <!-- 通常不需要修改此属性. -->   
     <cosntant name= "struts.freemarker.wrapper.altMap" value= "true" />     
          
     <!-- 指定xslt result是否使用样式表缓存.开发阶段设为 true ,发布阶段设为 false . -->   
     <cosntant name= "struts.xslt.nocache" value= "false" />     
          
     <!-- 设置struts自动加载的文件列表. -->   
     <cosntant name= "struts.configuration.files" value= "struts-default.xml,struts-plugin.xml,struts.xml" />   
              
     <!-- 设定是否一直在最后一个slash之前的任何位置选定namespace. -->   
     <cosntant name= "struts.mapper.alwaysSelectFullNamespace" value= "false" />   
</struts> 
  
   
  
   
  
   
  
   
  
<struts>
  
     <!-- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 -->
     <include file= "struts-default.xml" ></include>
      
      
     <!-- package 提供了将多个Action组织为一个模块的方式
         package 的名字必须是唯一的 package 可以扩展 当一个 package 扩展自
         另一个 package 时该 package 会在本身配置的基础上加入扩展的 package
         的配置 父 package 必须在子 package 前配置 
         name: package 名称
         extends :继承的父 package 名称
         abstract :设置 package 的属性为抽象的 抽象的 package 不能定义action 值 true : false
         namespace:定义 package 命名空间 该命名空间影响到url的地址,例如此命名空间为/test那么访问是的地址为http: //localhost:8080/struts2/test/XX.action
      -->
     < package name= "com.kay.struts2" extends = "struts-default" namespace= "/test" >
         <interceptors>
             <!-- 定义拦截器 
                 name:拦截器名称
                 class :拦截器类路径
              -->
             <interceptor name= "timer" class = "com.kay.timer" ></interceptor>
             <interceptor name= "logger" class = "com.kay.logger" ></interceptor>
             <!-- 定义拦截器栈 -->
             <interceptor-stack name= "mystack" >
                 <interceptor-ref name= "timer" ></interceptor-ref>
                 <interceptor-ref name= "logger" ></interceptor-ref>
             </interceptor-stack>
         </interceptors>
          
         <!-- 定义默认的拦截器 每个Action都会自动引用
          如果Action中引用了其它的拦截器 默认的拦截器将无效 -->
         < default -interceptor-ref name= "mystack" ></ default -interceptor-ref>
          
          
         <!-- 全局results配置 -->
         <global-results>
             <result name= "input" >/error.jsp</result>
         </global-results>
          
         <!-- Action配置 一个Action可以被多次映射(只要action配置中的name不同)
              name:action名称
              class : 对应的类的路径
              method: 调用Action中的方法名
         -->
         <action name= "hello" class = "com.kay.struts2.Action.LoginAction" >
             <!-- 引用拦截器
                 name:拦截器名称或拦截器栈名称
              -->
             <interceptor-ref name= "timer" ></interceptor-ref>
          
             <!-- 节点配置
                 name : result名称 和Action中返回的值相同
                 type : result类型 不写则选用superpackage的type struts- default .xml中的默认为dispatcher
              -->
          <result name= "success" type= "dispatcher" >/talk.jsp</result>
          <!-- 参数设置 
              name:对应Action中的get/set方法 
          -->
          <param name= "url" >http: //www.sina.com</param>
         </action>
     </ package >
</struts>
  
   
  
   
  
   
  
   
  
   
  
   
  
  
二、Struts2配置元素说明<转载>
Struts2核心的配置文件是缺省的struts.xml。
必要的时候,缺省的配置文件可以包含其它的配置文件;struts文件可以放入jar中,并自动插入应用程序,这样每个模块可以包含自己的配置文件并自动配置。在Freemarker和Velocity模块中,模板也能从classpath中加载,所以整个模块可以作为一个简单的jar文件被包含。
Struts.xml配置文件可以包含Interceptor、Action类和Results。
Struts.xml配置元素说明:
1 、Packages
Packages:packages把actions、results、results types、interceptors
和interceptor-stacks组装到一个逻辑单元中,从概念上讲,packages就像一个对象,可以被其它子包从写,而且可以拥有自己独立的部分。
Name属性是packages的必填元素,它作为一个关键字被后边的包引用; extends 元素是可选的,它允许包扩展一个和多个前边定义的包。注意,
struts.xml文件是至上而下处理的,所有被扩展的包,需要在扩展包前定义。
Abstract元素是可选的,它可以申明一个不包含actions的配置文件。
2 、Namespace
Namespace元素把actions细分到逻辑模块,每一个namespace都有自己的
前缀(prefix),namespace避免了action之间的名字冲突,当前缀出现在URI中时,这些标签都是名字空间感知的(“namespace aware”),所以这些namespace prefix不必嵌入到表单或连接中。
Default的namespace是一个空字符串“”,如果在指定的配置文件中,没有找到action,缺省的namespace也会被查找。Local/global策略允许应用程序在action “ extends ”元素层次结构之外,有全局的action配置。缺省的namespace也可以不在 package 中申明。
Namespace prefix可以注册为java的申明式安全,以确保授权的用户才能访问namespace的资源。
Root namespace(“/”)也被支持,root就是当直接请求context path的时候的namespace。
[个人理解:namespace的用法就像struts1.x中的path一样,只不过它在 package 中什么路径,而在action中申明action名子罢了]
3 、Include
Include元素使得框架能应用“divide and conquer”来配置文件。被include
的每个配置文件必须和struts.xml有一样的格式,一个大的项目可以采用这样方式来组织程序模块。
Include元素也可以和 package 交替出现,框架将按照顺序加载配置文件。
4 、Interceptor configuration
Interceptor允许应用程序在Action方法执行前后定义执行代码,
Interceptor在应用程序开发中十分重要,对于Interceptor有许多用例,如validation, property population, security, logging, 和profiling。
Interceptor被定义为一个Java类,Interceptor也可以组装成Interceptor-stack,他们将按照定义的顺序执行。
在struts- default .xml中定义了一些缺省的Interceptor-stack,以便框架能很好地运行。
5 、Action
Action是框架的“工作单元”。Action可以指定一个Interceptor-stack、
一序列的 return type和一序列的异常处理,但只有name属性是必须的。
  
   
  
   
  
   
  
   
  
  
一:配置struts2。
  
  首先在web.xml文件中配置filter
  
  Xml代码 
  
  <filter> 
  
  <filter-name>struts2</filter-name> 
  
  <filter- class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter- class
  
  </filter> 
  
  <filter-mapping> 
  
  <filter-name>struts2</filter-name> 
  
  <url-pattern>*.action</url-pattern> 
  
  </filter-mapping> 
  
  <filter>
  
  <filter-name>struts2</filter-name>
  
  <filter- class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter- class >
  
  </filter>
  
  <filter-mapping>
  
  <filter-name>struts2</filter-name>
  
  <url-pattern>*.action</url-pattern>
  
  </filter-mapping>
  
  然后在classpath中创建struts.xml配置文件。
  
  Xml代码 
  
  <?xml version= "1.0" encoding= "UTF-8" ?> 
  
  <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
  
  
  <struts> 
  
  <constant name= "struts.devMode" value=http: //blog.soso.com/qz.q/"true" /> 
  
  <constant name= "struts.convention.default.parent.package" value=http: //blog.soso.com/qz.q/"default-package" /> 
  
  <constant name= "struts.convention.package.locators" value=http: //blog.soso.com/qz.q/"action" /> 
  
  < package name= "default-package" extends = "convention-default"
  
  < default -action-ref name= "index" /> 
  
  <action name= "index"
  
  <result>/WEB-INF/content/index.jsp</result> 
  
  </action> 
  
  </ package
  
  </struts> 
  
  <?xml version= "1.0" encoding= "UTF-8" ?>
  
  <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
  
  
  <struts>
  
  <constant name= "struts.devMode" value=http: //blog.soso.com/qz.q/"true" />
  
  <constant name= "struts.convention.default.parent.package" value=http: //blog.soso.com/qz.q/"default-package" />
  
  <constant name= "struts.convention.package.locators" value=http: //blog.soso.com/qz.q/"action" />
  
  < package name= "default-package" extends = "convention-default" >
  
  < default -action-ref name= "index" />
  
  <action name= "index" >
  
  <result>/WEB-INF/content/index.jsp</result>
  
  </action>
  
  </ package >
  
  </struts>
  
  struts.devMode属性,配置启用调试,将有更多的错误信息输出,便于排错。struts.convention. default .parent. package 属性,指定使用注解标
  
  注的控制器的默认包。可以在这个默认包中配置全局信息。
  
  struts.convention. package .locators属性,为查找控制器包路径的关键字。如com.mycompany.action,com.mycompany.action.user,都会被
  
  struts2扫描。里面有继承至Action的类,或类名以Action结尾的类,都会做为Action处理。
  
  < default -action-ref name= "index" />指定了默认action,如果指定的action不存在则访问该action。
  
  把struts2-spring-plugin- 2.1 . 6 .jar添加到classpath中,struts2会自动扫描struts-plugin.xml文件,该文件自动注册了
  
  com.opensymphony.xwork2.ObjectFactory,完成和spring的集成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值