你的位置:首页 > Java教程

[Java教程]Struts2核心技术简介


Struts2核心技术简介

      使用Struts2框架,只要注重以下三大元素:配置文件、映射文件和Action:

          全局属性文件struts.properties:保存系统运行的一些参数变量,整个系统只有一个属性文件;

          映射文件struts.

          业务控制器Action:可以使用POJO(类似JavaBean)类,也可以集成ActionSupport,在该类中不仅可以取得表单数据,还可以取得上下文变量。以下是详细说明:

1、全局属性文件struts.properties

     struts.properties文件主要定义系统的属性,请看以下示例代码:

 

### Struts default properties###(can be overridden by a struts.properties file in the root of the classpath)######指定Struts2的配置类,默认为下面的配置,也可以通过继承### Specifies the Configuration used to configure Struts ### one could extend org.apache.struts2.config.Configuration### to build one's customize way of getting the configurations parameters into Struts# struts.configuration=org.apache.struts2.config.DefaultConfiguration###设置默认的国际化地区信息和国际化信息内码### This can be used to set your default locale and encoding scheme# struts.locale=en_USstruts.i18n.encoding=UTF-8###指定对象工厂类,也可以实现自己的工厂类### if specified, the default object factory can be overridden here### Note: short-hand notation is supported in some cases, such as "spring"###    Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here # struts.objectFactory = spring###当使用Spring的工厂类时,指定自动植入autoWrite的机制### specifies the autoWiring logic when using the SpringObjectFactory.### valid values are: name, type, auto, and constructor (name is the default)struts.objectFactory.spring.autoWire = name###使用Spring集成时,是否使用类缓存,可选值有:true和false### indicates to the struts-spring integration if Class instances should be cached### this should, until a future Spring release makes it possible, be left as true### unless you know exactly what you are doing!### valid values are: true, false (true is the default)struts.objectFactory.spring.useClassCache = true###指定对象类型检查器,可用值有“tiger”和“notiger”,也可以使用自己的类### if specified, the default object type determiner can be overridden here### Note: short-hand notation is supported in some cases, such as "tiger" or "notiger"###    Alternatively, you can provide a com.opensymphony.xwork2.util.ObjectTypeDeterminer implementation name here### Note: By default, com.opensymphony.xwork2.util.DefaultObjectTypeDeterminer is used which handles type detection###    using generics. com.opensymphony.xwork2.util.GenericsObjectTypeDeterminer was deprecated since XWork 2, it's###    functions are integrated in DefaultObjectTypeDeterminer now.###    To disable tiger support use the "notiger" property value here.#struts.objectTypeDeterminer = tiger#struts.objectTypeDeterminer = notiger###指定使用MIME-type multipart/form-data时的解析器### Parser to handle HTTP POST requests, encoded using the MIME-type multipart/form-data# struts.multipart.parser=cos# struts.multipart.parser=pellstruts.multipart.parser=jakarta# uses javax.servlet.context.tempdir by defaultstruts.multipart.saveDir=struts.multipart.maxSize=2097152###指定自定义的属性文件### Load custom property files (does not override struts.properties!)# struts.custom.properties=application,org/apache/struts2/extension/custom###URL与Action映射处理器### How request URLs are mapped to and from actions#struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper###指定URL的扩展名,可以指定多个扩展名,用逗号分隔,例如action、jnlp、do### Used by the DefaultActionMapper### You may provide a comma separated list, e.g. struts.action.extension=action,jnlp,dostruts.action.extension=do###被FilterDispatcher所使用,true表示Struts serves static content来自于内部jar### Used by FilterDispatcher### If true then Struts serves static content from inside its jar. ### If false then the static content must be available at <context_path>/strutsstruts.serve.static=true###是否Struts过滤器中提供的静态内容应该被浏览器缓存到头部属性中### Used by FilterDispatcher### This is good for development where one wants changes to the static content be### fetch on each request. ### NOTE: This will only have effect if struts.serve.static=true### If true -> Struts will write out header for static contents such that they will###       be cached by web browsers (using Date, Cache-Content, Pragma, Expires)###       headers).### If false -> Struts will write out header for static contents such that they are###      NOT to be cached by web browser (using Cache-Content, Pragma, Expires###      headers)struts.serve.static.browserCache=true###是否允许动态方法调用,如果为true,则可以配置如下的<action>参数映射### mappings, such as <action name="*/*" method="{2}" class="actions.{1}">### Set this to false if you wish to disable implicit dynamic method invocation### via the URL request. This includes URLs like foo!bar.action, as well as params### like method:bar (but not action:foo). ### An alternative to implicit dynamic method invocation is to use wildcard ### mappings, such as <action name="*/*" method="{2}" class="actions.{1}">struts.enable.DynamicMethodInvocation = true###是否在action名称中允许“/”### Set this to true if you wish to allow slashes in your action names. If false,### Actions names cannot have slashes, and will be accessible via any directory### prefix. This is the traditional behavior expected of WebWork applications.### Setting to true is useful when you want to use wildcards and store values### in the URL, to be extracted by wildcard patterns, such as ### <action name="*/*" method="{2}" class="actions.{1}"> to match "/foo/edit" or ### "/foo/save".struts.enable.SlashesInActionNames = false###是否可以用替代的语法%{}代替tags### use alternative syntax that requires %{} in most places### to evaluate expressions for String attributes for tagsstruts.tag.altSyntax=true###是否为Struts开发模式### when set to true, Struts will act much more friendly for developers. This### includes:### - struts.i18n.reload = true### - struts.configuration.

View Code

 

2、映射文件struts.

     Struts2框架的核心配置文件时Struts.

(1)属性定义元素<constant>

     可以使用<constant>标签来定义一个属性,该属性与struts.properties中属性重名,并覆盖struts.properties中该属性的值。这样做的好处是,如果使用了多个映射文件时,允许每一个文件都使用不同的属性。

(2)导入子文件元素<include>

     使用<include>标签来导入一个子文件,子文件与struts.

(3)包定义元素<package>

     <package>定义了一组<action>和拦截器元素,可以定义多个<action>元素,name表示包的名字,extends属性表示继承自struts-default.

(4)Action映射配置元素<action>

     Action mapping是框架中的基本工作单元,框架通过对请求路径进行映射来决定由哪个Action来处理请求。

(5)Result配置元素<result>

     Action类处理完一个请求后会返回一个字符串,这个字符串将被用来选择一个<result>元素。通常一个action mapping会有多个<result>,代表各个可能不同的结果。

(6)配置全局映射元素<global-results>

      定义在<action>元素里面的<result>我们可以称之为局部<result>,除此之外我们还可以定义全局的<result>,这些全局的<result>会悲哀多个<action>所共享。框架会首先寻找嵌套在<action>元素中的<result>,如果没有匹配的就去全局<result>中去寻找。

(7)配置拦截器<interceptors>

      通过使用拦截器,我们可以在Action中的方法执行之前先执行一些我们事先定义好的方法,也可以在Action中的方法执行之后立即执行一些我们事先定义好的方法。

   下面请看一个struts.文件代码示例:

 

<??><!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>  <constant name="struts.enable.DynamicMethodInvocation" value="false" />  <constant name="struts.devMode" value="false" />  <include file="example./>  <!-- Add packages here -->  <package name="main" extends="struts-default">    <action name="test" class="com.demo.struts2.actions.TestAction">      <result name="success">success.jsp</result>      <result name="input">input.jsp</result>    </action>  </package></struts>

View Code

 

3、业务控制器Action

     使用Struts2框架,不需要开发ActionForm类,主要注意一下三点:

       Action可以是任意的POJO(类似JavaBean)类

       一般继承ActionSupport类

       可以在Action中访问上下文变量