zoukankan      html  css  js  c++  java
  • Struts2核心技术简介

    Struts2核心技术简介

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

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

              映射文件struts.xml:用于配置请求映射的Action和拦截器,可以有多个映射文件;

              业务控制器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_US
    struts.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=pell
    struts.multipart.parser=jakarta
    # uses javax.servlet.context.tempdir by default
    struts.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,do
    struts.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>/struts
    struts.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 tags
    struts.tag.altSyntax=true
    
    ###是否为Struts开发模式
    ### when set to true, Struts will act much more friendly for developers. This
    ### includes:
    ### - struts.i18n.reload = true
    ### - struts.configuration.xml.reload = true
    ### - raising various debug or ignorable problems to errors
    ###   For example: normally a request to foo.action?someUnknownField=true should
    ###                be ignored (given that any value can come from the web and it
    ###                should not be trusted). However, during development, it may be
    ###                useful to know when these errors are happening and be told of
    ###                them right away.
    struts.devMode = false
    
    ###是否国际化信息自动加载
    ### when set to true, resource bundles will be reloaded on _every_ request.
    ### this is good during development, but should never be used in production
    struts.i18n.reload=false
    
    
    ### Standard UI theme
    ### Change this to reflect which path should be used for JSP control tag templates by default
    struts.ui.theme=xhtml
    struts.ui.templateDir=template
    #sets the default template type. Either ftl, vm, or jsp
    struts.ui.templateSuffix=ftl
    
    ###在struts.xml 文件更改时,是否允许重载
    ### Configuration reloading
    ### This will cause the configuration to reload struts.xml when it is changed
    struts.configuration.xml.reload=false
    
    ###velocity配置文件路径,默认为velocity.properties
    ### Location of velocity.properties file.  defaults to velocity.properties
    struts.velocity.configfile = velocity.properties
    
    ###Cvelocity的context列表
    ### Comma separated list of VelocityContext classnames to chain to the StrutsVelocityContext
    struts.velocity.contexts =
    
    ###velocity工具盒的位置
    ### Location of the velocity toolbox
    struts.velocity.toolboxlocation=
    
    ### used to build URLs, such as the UrlTag
    struts.url.http.port = 80
    struts.url.https.port = 443
    ### possible values are: none, get or all
    struts.url.includeParams = get
    
    ###加载附加的国际化属性文件(不包含.properties)
    ### Load custom default resource bundles
    struts.custom.i18n.resources=com.demo.struts2.resources.ApplicationResources
    
    ### workaround for some app servers that don't handle HttpServletRequest.getParameterMap()
    ### often used for WebLogic, Orion, and OC4J
    struts.dispatcher.parametersWorkaround = false
    
    ### configure the Freemarker Manager class to be used
    ### Allows user to plug-in customised Freemarker Manager if necessary
    ### MUST extends off org.apache.struts2.views.freemarker.FreemarkerManager
    #struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager
    
    ### Enables caching of FreeMarker templates
    ### Has the same effect as copying the templates under WEB_APP/templates
    struts.freemarker.templatesCache=false
    
    ### Enables caching of models on the BeanWrapper
    struts.freemarker.beanwrapperCache=false
    
    ### See the StrutsBeanWrapper javadocs for more information
    struts.freemarker.wrapper.altMap=true
    
    ### configure the XSLTResult class to use stylesheet caching.
    ### Set to true for developers and false for production.
    struts.xslt.nocache=false
    
    ###struts自动加载的配置文件列表
    ### A list of configuration files automatically loaded by Struts
    struts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml
    
    ### Whether to always select the namespace to be everything before the last slash or not
    struts.mapper.alwaysSelectFullNamespace=false
    View Code

    2、映射文件struts.xml

         Struts2框架的核心配置文件时Struts.xml,该文件主要负责管理Struts框架的业务控制器Action。该文件包含唯一的根元素<struts>,可以在该元素中添加子标签来实现自己的配置。一般开发主要用到如下的七个配置功能:

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

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

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

         使用<include>标签来导入一个子文件,子文件与struts.xml有同样的结构,使用相同的配置语法。通过这种方式提供了一种模块化的方式来管理struts.xml文件。

    (3)包定义元素<package>

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

    (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.xml文件代码示例:

     

    <?xml version="1.0" encoding="UTF-8" ?>
    <!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.xml"/>
    
        <!-- 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中访问上下文变量

  • 相关阅读:
    Global Citizenship
    Eng Stu
    说说
    C#编程远程控制机械手臂
    切割系统
    C#编码 画图控件
    编程Sourceforge
    C#编程线程
    空间点的几何关系
    一台普通电脑通过设置连接到公司网络
  • 原文地址:https://www.cnblogs.com/liuzhen1995/p/5779343.html
Copyright © 2011-2022 走看看