`
lekihuo
  • 浏览: 25240 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

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  

 

分享到:
评论
1 楼 289296126 2010-12-13  
我的ActionSupport中没有实现了Action接口,也没有实现了Validateable接口,只有一个object,可能是什么原因呢

相关推荐

    struts2入门知识

    struts2入门知识学习总结

    Struts2入门教程(全新完整版)

    十二、总结 本教程对struts2的基本知识进行了一些说明,关于struts2的更多详细内容应参看struts2的官方文档及提供的app实例。 下面对struts2的基本执行流程作一简要说明,此流程说明可以结合官方提供的struts2结构图...

    Struts2入门教程

    Struts2其实并不是一个陌生的Web框架,Struts2是以Webwork的设计思想为核心,吸收了Struts1的优点,因此,可以认为Struts2是Struts1和Webwork结合的产物。 Struts2的使用人群逐渐增多,它在逐步吸引开发者和用户的...

    struts2一些知识点总结

    关于struts2的入门知识点,以及拦截器方面的知识

    收集的常见的专业问题解决办法.rar

    2009-02-24 08:39 1423088 975149 常见的专业问题解决办法\Java中多态变量的讨论和总结.mht 2009-02-24 08:52 156256 34316 常见的专业问题解决办法\Java中的强制类型转换_Believe ┭┮ YourSelf.mht 2009-02-24 08:...

    Java语言基础下载

    第三十章:struts入门 623 学习目标 623 Struts简介 624 什么是应用框架 624 WEB框架所要解决的问题 625 建立简单的Struts应用 627 内容总结 636 独立实践 636 第三十一章:Struts基础 637 学习目标 637 MVC 638 ...

    iuhyiuhkjh908u0980

    本章我们将会开发基于Struts2框架的Hello World.我们的Hello World程序是你开发基于Struts2框架程序的第一步.这里我们会提供循序渐进的教程来开发基于Struts2框架的Hello World程序. 教程包含基本的步骤像创建目录...

    java面试题及技巧3

    │ │ │ 275test-2.txt │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini ...

    java面试题以及技巧

    │ │ │ 275test-2.txt │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini ...

    java面试题目与技巧1

    │ │ │ 275test-2.txt │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini ...

    java面试题及技巧4

    │ │ │ 275test-2.txt │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini ...

    java面试题以及技巧6

    │ │ │ 275test-2.txt │ │ │ 275test-3.txt │ │ │ 275test-4.txt │ │ │ 275test.txt │ │ │ answer-1.txt │ │ │ answer-2.txt │ │ │ answer-3.txt │ │ │ answer-4.txt │ │ │ Desktop_.ini ...

    ssh集成开发宝典

    本书籍涵盖了ssh框架集成开发的知识点,是前人总结的开发经验,从入门到精通,适合初学者和想深入研究ssh集成开发的人士。

    spring学习笔记

    .................................................................................................................................5 1.3 Spring 的入门的程序:................................................

Global site tag (gtag.js) - Google Analytics