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

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>

3、业务控制器Action

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

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

一般继承ActionSupport类

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

Struts2核心技术简介的更多相关文章

  1. Struts核心技术简介

    Struts核心技术简介 1.Struts内部机制   Struts是一种基于MVC经典设计模式的开发源代码的应用框架,它通过把Servlet.JSP.JavaBean.自定义标签和信息资源整合到一个 ...

  2. Hibernate核心技术简介

    Hibernate核心技术简介 1.Hibernate映射文件开发     Hibernate映射文件就是项目中*.hbm.xml文件,其主要是完成各元素的配置,包括根元素.类元素.定义主键.设置主键 ...

  3. Struts2标签简介

    Struts2标签简介 Struts2标签的优势 标签库简化了用户对标签的使用 结合OGNL使用,对于集合.对象的访问功能非常强大 提供可扩展的主题.模板支持,极大简化了视图页面的编写 不依赖任何表现 ...

  4. java框架之Struts2(1)-简介及入门

    简介 Struts2 是一个基于 MVC 设计模式的 Web 应用框架,它本质上相当于一个 servlet,在 MVC 设计模式中,Struts2 作为控制器 (Controller) 来建立模型与视 ...

  5. Struts2框架简介和示例

    struts2框架 Struts2是java web的框架,在Java Web开发中,表示层框架,其核心是通过扩展Servlet来帮助处理http请求. Struct2的基本流程 Struct2的框架 ...

  6. 【docker】docker基础原理,核心技术简介

    关于docker的核心技术,就是以下的三大技术: 1.namespaces [命名空间] 使用linux的命名空间实现的进程间隔离.Docker 容器内部的任意进程都对宿主机器的进程一无所知. 除了进 ...

  7. 【Struts2】简介及入门

    一.概述 二.Struts2 快速入门程序 2.1 开发流程比较 2.2 引入依赖 2.2 创建jsp页面 2.3 在web.xml中配置前端控制器 2.4 创建struts.xml配置文件 2.4 ...

  8. struts2学习(11)struts2验证框架1.验证简介、内置验证

    一.Struts2验证简介: 二.struts2内置验证: 下面例子,需求是:为用户注册进行验证: com.cy.model.User.java: package com.cy.model; publ ...

  9. Struts2配置文件

    Struts2配置文件 简介: 与Struts2相关的配置文件有好几个,常用的有 struts.properties , web.xml, struts.xml等.web.xml中配置Struts2的 ...

随机推荐

  1. 部署.NET开发环境

    昨晚把家里的电脑重新部署.NET开发环境.从晚上21点安装到今天凌晨3点多才完成,还算顺利,但是耗时最漫长莫过于在安装Visual Studio 2015 Update3...... 第一,全新安装W ...

  2. EFCore执行Sql语句的方法:FromSql与ExecuteSqlCommand

    前言 在EFCore中执行Sql语句的方法为:FromSql与ExecuteSqlCommand:在EF6中的为SqlQuery与ExecuteSqlCommand,而FromSql和SqlQuery ...

  3. MVC5 DBContext.Database.SqlQuery获取对象集合到ViewModel集合中(可以利用这个方法给作为前台视图页cshtml页面的@model 源)

    首先我们已经有了一个Model类: using System;using System.Data.Entity;using System.ComponentModel.DataAnnotations; ...

  4. Linux-网络连接-(VMware与CentOS)

    VMware虚拟机中安装CentOS,进行网络连接,分为两步,内网连接,与外网连接. 前提: 当你正确安装VMware后,网络适配器会增加2个新的网卡:(可在设备管理器->网络适配器中查看) 第 ...

  5. 高性能 TCP & UDP 通信框架 HP-Socket v3.2.3

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  6. SQL复制表结构或表数据

    需求: 软件开发过程中,一般会部署两个数据库:一个测试数据库提供给开发和测试过程使用:一个运维数据库提供上线使用.当需求变化需增加表时,会遇到数据库表结构或表数据同步的问题,这时就要复制表结构或表数据 ...

  7. 基于HTML5 Canvas实现的图片马赛克模糊特效

    效果请点击下面网址: http://hovertree.com/texiao/html5/1.htm 一.开门见山受美国肖像画家Chuck Close的启发,此脚本通过使用HTML5 canvas元素 ...

  8. C#变量、输入、数据类型的自动转换

  9. javascript中concat方法深入理解

    最近在恶补js知识的时候,总是会因为js强大的语法而感到震撼.因为以前对前端方面的疏忽,导致了一些理解的错误.因此痛改前非,下定决心,不管做什么事情,都要有专研的精神. 在介绍前,抛出一个问题:如何将 ...

  10. iOS开发-UI 从入门到精通(三)

    iOS开发-UI 从入门到精通(三)是对 iOS开发-UI 从入门到精通(一)知识点的综合练习,搭建一个简单地登陆界面,增强实战经验,为以后做开发打下坚实的基础! ※在这里我们还要强调一下,开发环境和 ...