struts2入门(二)-知识总结(3)

三、关于struts2中Action的实现和ActionSupport
     1,Action的实现
       在编程中,action是用来处理用户请求的,理论上Struts 2的Action无须实现任何接口或继承任何类型,普通的POJO类就可以用做Action类,但是,我们为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重载(Override)此类里的String execute()方法,因为ActionSupport已经实现了Action接口,还实现了Validateable接口,提供了数据校验功能。通过继承该ActionSupport类,可以简化Struts 2的Action开发。
      另外,处理Action过程中调用的方法("execute"方法)或自定义方法是不带参数的,但Struts 2允许使用ActionContext和ServletActionContext类来取得系统的Session、Request、Response、Application等对象。Action类的设计之所以这么简单,其目的是能让Action和框架完全解耦。
       其次,在struts2中的Action是每一个请求对应一个实例,是线程安全的。

      2,struts2 中 Actionsupport 的作用
        这部分转自:
http://xumiao900.iteye.com/blog/469760
        Action 跟 Actionsupport 的区别
       当我们在写action的时候,可以实现Action接口,也可以继承Actionsupport这个类.到底这两个有什么区别呢?

Action接口有:
public static final java.lang.String SUCCESS = "success";
public static final java.lang.String NONE = "none";
public static final java.lang.String ERROR = "error";
public static final java.lang.String INPUT = "input";
public static final java.lang.String LOGIN = "login";

public abstract java.lang.String execute() throws java.lang.Exception;

而Actionsupport这个工具类在实现了Action接口的基础上还定义了一个validate()方法,重写该方法,它会在execute()方法之前执行,如校验失败,会转入input处,必须在配置该Action时配置input属性。

另外,Actionsupport还提供了一个getText(String key)方法还实现国际化,该方法从资源文件上获取国际化信息.

这样在自定义标签时可以定义一个变量为new actionsupport对象实现国际化。


      ActionSupport类的作用
       struts2不要求我们自己设计的action类继承任何的struts基类或struts接口,但是我们为了方便实现我们自己的action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类,并重写此类里的public String execute() throws Exception方法。因为此类中实现了很多的实用借口,提供了很多默认方法,这些默认方法包括国际化信息的方法、默认的处理用户请求的方法等,这样可以大大的简化Acion的开发。
      Struts2中通常直接使用Action来封装HTTP请求参数,因此,Action类里还应该包含与请求参数对应的属性,并且为属性提供对应的getter和setter方法。

 

四,struts.xml和struts.properties配置常量参数

Struts2中常量可以在多个配置文件中配,建议在struts.xml中配置,方式如下:
<struts>
    <constant name=”struts.action.extension” value=”do”/>
</struts>
因为常量可以在多个配置文件中进行定义,所以我们需要了解struts2加载常量的搜索顺序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml

如果在多个文件中配置了同一个常量,则后一个配置文件中的常量值将会覆盖前面文件中配置的常量值

附:常见配置参数详解

转自:http://blog.csdn.net/firetaker/archive/2010/03/30/5432133.aspx

### 指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration   
### 开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件。   
# struts.configuration=org.apache.struts2.config.DefaultConfiguration   
  
### 设置默认的locale和字符编码   
# struts.locale=en_US  
struts.i18n.encoding=UTF-8   
  
  
### 指定struts的工厂类   
# struts.objectFactory = spring  
  
### 指定spring框架的装配模式   
### 装配方式有: name, type, auto, and constructor (name 是默认装配模式)   
struts.objectFactory.spring.autoWire = name  
  
### 该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true.   
struts.objectFactory.spring.useClassCache = true  
  
### 指定类型检查   
#struts.objectTypeDeterminer = tiger  
#struts.objectTypeDeterminer = notiger  
  
### 该属性指定处理 MIME-type multipart/form-data,文件上传   
# struts.multipart.parser=cos  
# struts.multipart.parser=pell  
struts.multipart.parser=jakarta  
# 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir    
strutsstruts.multipart.saveDir=   
struts.multipart.maxSize=2097152  
  
### 加载自定义属性文件 (不要改写struts.properties!)   
# struts.custom.properties=application,org/apache/struts2/extension/custom   
  
### 指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper   
#struts.mapper.class=org.apache.struts2.dispatcher.mapper.DefaultActionMapper   
  
### 指定action的后缀,默认为action,可以加多个后缀,用‘,’号隔开
struts.action.extension=action   
  
### 被 FilterDispatcher使用   
### 如果为 true 则通过jar文件提供静态内容服务.    
### 如果为 false 则静态内容必须位于 <context_path>/struts   
struts.serve.static=true  
  
### 被 FilterDispatcher使用   
### 指定浏览器是否缓存静态内容,测试阶段设置为false,发布阶段设置为true.   
struts.serve.static.browserCache=true  
  
### 设置是否支持动态方法调用,true为支持,false不支持.   
struts.enable.DynamicMethodInvocation = true  
  
### 设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true.   
struts.enable.SlashesInActionNames = false  
  
### 是否允许使用表达式语法,默认为true.   
struts.tag.altSyntax=true  
  
  
### 设置当struts.xml文件改动时,是否重新加载.   
### - struts.configuration.xml.reload = true  
### 设置struts是否为开发模式,默认为false,测试阶段一般设为true.  可以获取更多调试信息
struts.devMode = false  
  
### 设置是否每次请求,都重新加载资源文件,默认值为false.   
struts.i18n.reload=false  
  
###标准的UI主题   
### 默认的UI主题为xhtml,可以为simple,xhtml或ajax   
struts.ui.theme=xhtml  
###模板目录   
struts.ui.templateDir=template   
#设置模板类型. 可以为 ftl, vm, or jsp   
struts.ui.templateSuffix=ftl  
  
###定位velocity.properties 文件.  默认 velocity.properties   
struts.velocity.configfile = velocity.properties   
  
### 设置velocity的context.   
struts.velocity.contexts =   
  
### 定位toolbox.   
struts.velocity.toolboxlocation=   
  
### 指定web应用的端口.   
struts.url.http.port = 80  
### 指定加密端口   
struts.url.https.port = 443  
### 设置生成url时,是否包含参数.值可以为: none, get or all   
struts.url.includeParams = get  
  
### 设置要加载的国际化资源文件,以逗号分隔.   
# struts.custom.i18n.resources=testmessages,testmessages2   
  
### 对于一些web应用服务器不能处理HttpServletRequest.getParameterMap()   
### 像 WebLogic, Orion, and OC4J等,须设置成true,默认为false.   
struts.dispatcher.parametersWorkaround = false  
  
### 指定freemarker管理器   
#struts.freemarker.manager.classname=org.apache.struts2.views.freemarker.FreemarkerManager   
  
### 设置是否对freemarker的模板设置缓存   
### 效果相当于把template拷贝到 WEB_APP/templates.   
struts.freemarker.templatesCache=false  
  
### 通常不需要修改此属性.   
struts.freemarker.wrapper.altMap=true  
  
### 指定xslt result是否使用样式表缓存.开发阶段设为true,发布阶段设为false.   
struts.xslt.nocache=false  
  
### 设置struts自动加载的文件列表.   
strutsstruts.configuration.files=struts-default.xml,struts-plugin.xml,struts.xml   
  
### 设定是否一直在最后一个slash之前的任何位置选定namespace.   
struts.mapper.alwaysSelectFullNamespace=false  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值