Struts2核心技术简介

时间:2023-01-12 11:28:38

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&lpar;1&rpar;-简介及入门

    简介 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学习&lpar;11&rpar;struts2验证框架1&period;验证简介、内置验证

    一.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. TJI读书笔记17-字符串

    TJI读书笔记17-字符串 不可变的String 重载”+”和StringBuilder toString()方法的一个坑 String上的操作 格式化输出 Formatter类 字符串操作可能是计算 ...

  2. 在server 2008&sol;2003中 取消对网站的安全检查&sol;去除添加信任网站

    新安装好Windows   Server   2003操作系统后,打开浏览器来查询网上信息时,发现IE总是“不厌其烦”地提示我们,是否需要将当前访问的网站添加到自己信任的站点中去:要是不信任的话,就无 ...

  3. ubuntu下python3安装类库

    ubuntu是默认安装了python2的,所以直接使用 pip install XXX 是默认安装到python2的,安装到python3 的指令是 pip3 install XXXX 或者 pyth ...

  4. MySql服务基础

           MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 ...

  5. Oracle导出空表(从来都没有用过的表)

    Oracle11g默认对空表不分配segment,故使用exp导出Oracle11g数据库时,空表不会导出! .设置deferred_segment_creation参数为FALSE后,无论是空表还是 ...

  6. JS滑动门&comma;JQuery滑动门

    <a href="#" id="one1" onmouseover="setTab('one',1,2)" class="h ...

  7. mysql用root用户启动后其他用户无法启动不问题

    问题描述:用root账户启动mysql后,在用mysql用户或其他非root账户启动不了mysql问题解决:通过看mysql的err日志,发现 Failed to open log (robert-b ...

  8. zoj - 3209 - Treasure Map(精确覆盖DLX)

    题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...

  9. Android项目中的各个模块框架设计

    作为Android开发,现对项目开发中的各个模块搭建,梳理如下: Android UI框架,开发人员需要达到专家级 网络框架 浏览框架 图片加载框架 图片裁剪压缩工具类 客户端并发框架 线程池设计 ( ...

  10. C&num; 三种打印方式含代码

    一:C#代码直接打印pdf文件(打印质保书pdf文件) 引用: 代码注释很详细了. private void btn_pdf_Click(object sender, RoutedEventArgs ...