`
peihong-ph
  • 浏览: 21253 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2的整理

阅读更多
1、任何表现层框架都是用来帮我们实现MVC
model1(jsp)  -- >  model2 (jsp + servlet)  -- > MVC
2. Struts1 怎样实现MVC?
2.1 核心控制器
2.1.1 核心控制器  ActionServlet
2.1.2 业务逻辑控制器 Action
2.2 提供了标签,简化V的编写
2.3 表现层框架对M没有支持
3. Struts1 有什么样的缺点?  对servlet的简单封装
3.1 支持的表现层技术单一。
3.2 与servlet api严重耦合 execute()
3.3 代码严重依赖于struts1 的API,属于侵入式设计
4. webwork:opensymphony-- > XWork   AOP
5. 如何在项目中使用Struts2
5.1 jar (7 + 2)
5.2 log4j.properties
5.3 提供好配置文件 struts.xml src
5.4 web.xml
5.5 编写业务逻辑控制器
5.6 配置在struts.xml
6.Struts2和Struts1的主要区别:
6.1 设计理念不同,Struts1是对servlet的封装,而Struts2中体现的是AOP的思想
6.2 Struts2的Action非单例
7. 获得表单参数
7.1 属性驱动
7.2 模型驱动, 提供一个特定的JavaBean,用它来收集表单参数以及传递给下个页面的信息
7.2.1 让Action implements ModelDriven
7.2.2 自己创建模型的实例
7.2.3 getModel()  返回创建的模型的实例
7.2.4 结果页面访问信息:model.uname   uname
7.3 扩展的属性驱动:本质上还是属性驱动, 属性不是简单属性,而是JavaBean
7.3.1 不需要自己实例化 private LogInfo info;
7.3.2 提供对应的get/set方法
7.3.3 提交页面<input name="info.email">
7.3.4 显示页面: ${info.uname }
8. 如何传递给下个页面信息
8.1 通过实例变量进行传递
8.2 通过Servlet API
9. 在页面如何访问Action传入的信息 , ActionContext <s:debug/>
9.1 Value Stack
9.1.1 el requestScope ${info} ${requestScope.info}
9.1.2 struts2的标签 <s:property />
9.2 Stack Context
9.2.1 el 是哪个就用哪个作用域
9.2.2 struts2的标签 <s:property value="#"/>
10. Configuration Files
10.1 web.xml(可以提供参数,但不会这么来做)
    10.2 struts.xml
    10.3 struts.properties  -- > 用来覆盖defuault.properties(一般将其配置到 struts.xml中的constant里)
    10.4 struts-default.xml (package : 4)
    10.5 velocity.properties
    10.6 struts-default.vm
11. Action
11.1 POJO
11.2 implements Action
11.3 extends ActionSupport(提供了校验国际化的方法,可以简化Action的编写) (*)   
12. Package
12.1 解决命名冲突 & 便于管理
12.2 name给包起一个名字,在继承的时候用的上
12.3 extends 继承哪一个, 可以使自定义也可以说struts-default
12.4 namespace: 可以不指明,默认值是namespace="",被称为默认的查找空间
如果指明,必须以/开头,可以只是/,这种情况被称为根命名空间;
一旦为package指明了namespace,则namespace的值必须作为本报内Action访问路径的一部分
12.5 abstract:true表示内部没有配置action
13. 如何在一个Action中包含多个处理单元   public String  ()
13.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). 
13.2 把一个物理上的Action映射为多个逻辑上的Action<action method="">
13.3 通配符
### An alternative to implicit dynamic method invocation is to use wildcard
### mappings, such as <action name="*/*" method="{2}" class="actions.{1}">
13.3.1 约定优先 Action method jsp
13.3.2 先找严格匹配的,没有严格匹配的,才考虑通配
13.3.3 只要是通配,按照从上到下的顺序,第一个可以通配成功的被使用
13.3.4 如果有<action name="*" >,必须放到最后面
13.3.5 <default-action-ref name="index" />在包内找不到特定的Action的时候,会使用,
如果通配成功,也认为找到了,default-action-ref无意义。
14. 访问Servlet API
14.1 拿到Map的Servlet API(不包括response)对象
14.1.1 依赖于特定API (*)
ActionContext.getContext().getSession()
(Map<String, Object>) ActionContext.getContext().get("request")) 基本用不上
ActionContext.getContext().getApplication()
14.1.2 依赖于注入(拦截器)
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
14.2 拿到真实类型的Servlet API  -- > 最好用el
14.2.1 依赖于特定API (*)
ServletActionContext

14.2.2 依赖于注入(拦截器)
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
15. OGNL: Object Graph Navigation Language 类似于el ,应用在Struts2的标签里面 <s:property value="ognl"/>
16. 简单的校验(——)
if(GenericValidator.isBlankOrNull(uname)||!uname.matches("\\w(4,10)")){
addFieldError("uname", "用户名的长度必须在4到10之间");
flag=true;
}
用户名:<input name="uname"/><s:property value="fieldErrors.uname[0]"/><br>

17. Result
17.1 范围: result(本Action使用)  vs   global-results(本包的所有Action使用) ,局部优先
17.2 类型: 用于指明当前转向什么样的资源
17.2.1 默认的类型
     <result-type type="chain" class="com.opensymphony.xwork2.ActionChainResult"/> Action的链式调


            <result-type type="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult"

default="true"/>
            <result-type type="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type type="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            两个Action之间如何传递信息:
            chain:ActionContext.getContext().put("uid", id);
            redirectAction:Session 或者 url :<result type="redirectAction">emailAction?uid=%{id}</result>(在

action中要写id的set及get方法)
            17.2.2 可以扩展:<result-type name="chart"

class="org.apache.struts2.dispatcher.ChartResult">
            17.2.3 跳转到其它包的Action
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
               

<param name="namespace">/example</param>
            </result>
            17.3 定义自己的result-type
17.3.1 extends StrutsResultSupport
17.3.2 重写doExecute(String finalLocation, ActionInvocation invocation),配置的路径
18. 声明式异常处理 dao -- > service  - > Action (统一异常处理)  try { }catch() {}catch() {}
<exception-mapping result="result的name" exception="异常的类型"></exception-mapping>
18.1 异常是有继承关系: 先找精确的
18.2 既有局部(本Action使用)又有global-exception-mappings(本包所有Action共享)
     先找精确,同等级别下,局部优先
18.3 global-exception-mappings最好转向到global-results
18.4 应该怎么去用: global-exception-mappings: <exception-mapping result="error"

exception="java.lang.Exception"></exception-mapping>
Action所特定关心的一些:往往和用户的输入相关, 局部 
19. 19.1 ActionContext  19.2 拦截器的顺序问题
20. 编写自己的拦截器
20.1 extends AbstractInterceptor
20.2 implements Interceptor
20.3 配置 struts.xml  <interceptors></interceptors>
21. 文件上传(如何给拦截器配置参数)
       21.1 method="post" enctype="multipart/form-data"
       21.2 Action private File repo;private String repoFileName;private String repoContentType;
       21.3 struts.multipart.maxSize=20971520  一次请求最大的大小,如果不满足这个条件,错误信息来自于common-

fileupload
       21.4 <!--  fileUpload必须放到前面 ,不放到前面无法转向到input-->
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedExtensions">jpg,bmp</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
22. token(如何使用额外的拦截器)
22.1提交页面<form><s:token></s:token></form>
22.2<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="invalid.token">

23. 对现有拦截器进行修改

24. 标签
24.1 Generic Tags
24.1.1  Control Tags
24.1.2  Data Tags
24.2 UI Tags(主题 xhtml)-->界面元素怎样显示
24.2.1 Form Tags
24.2.2 Non-Form UI Tags
24.2.3 Ajax Tags(dojo)
type: object 默认理解为ognl  如果希望强制按照字符串理解‘’
      String 默认理解为字符串, 如果希望强制按照ognl理解 %{ }
24.3 修改现有主题 src  -- > template  -- > simple -- > 哪个不适合,单独提供
24.4 定义新的主题 src  -- > template ---> abc(主题的名字)通过 theme.properties指明继承simple   
25. 校验: 在真正执行处理单元之前,对用户提交的信息进行基本校验,
   如果满足业务要求,执行处理单元,否则转向到由input指向的页面。
   25.1 客户端校验(防止用户的误操作) vs 服务器端校验(防止恶意攻击)
   25.2 服务器端校验: 手工校验(validate  validateXxx)  配置校验(xml-->描述业务规则)
   25.3 手工校验:  在Action中重写 validate(不管处理哪个处理单元,都会执行),  validateXxx
            注:
   validateExecute会在execute执行之前来执行
   可以在validateExecute去编写业务规则
   addFieldError--> 表达error的Map去添加信息
   当该方法执行结束以后,如果map不为空,说明有校验错误,不再执行execute
   转向到由input指向的页面

任何一个处理单元在执行之前都会调用validate,先判断有无validateXxx方法,有就调用
不管有没有问题出现,都会检查有没有validate,有则执行,当validate执行完后,检查Map

   25.4  配置校验: 提供xml-->描述校验规则(1、字段优先  2、规则优先)
   ActionClassName-validation.xml  < -- > validate
   ActionClassName-ActionName-validation.xml  <--- >   validateXxx(注:此处的ActionName必须要

是.xml配置文件中的action的name)

<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
    <validator name="requiredstring"

class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
    <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
    <validator name="double"

class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
    <validator name="fieldexpression"

class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
    <validator name="conversion"

class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
    <validator name="stringlength"

class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
    <validator name="conditionalvisitor"

class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>

26. 国际化
26.1 资源文件的命名  baseName_语言_国家.properties   baseName.properties
26.2 ResourceBundle bundle = ResourceBundle.getBundle("msg", Locale.US);
     String message =  bundle.getString("key1");
     System.out.println(MessageFormat.format(message, "小张"));
26.3 Jsp: <s:property value="%{getText("")}"/> ,但不建议使用;
          <s:i18n name="com/pk/conf/user"><s:text/></s:i18n>(在页面中直接申明使用的配置文件)
26.4 Action: getText("key")   使用的资源文件最好是Action级别的或者是package级别
26.5 资源文件有级别
26.5.1 全局资源文件 <constant name="struts.custom.i18n.resources"

value="msg"></constant>
26.5.2 package级别: baseName必须是package,可以被本包及子包所有Action所共享
26.5.3 Action级别:baseName必须和类名一致,同时放在同一个目录下,可以被本Action及子类使用
26.5.4 查找顺序:Action级别  -- 》 package级别  --》 全局资源文件 
27. 类型转换
XWork will automatically handle the most common type conversion for you. This includes support for converting

to and from Strings for each of the following:

String
boolean / Boolean
char / Character
int / Integer, float / Float, long / Long, double / Double
dates - uses the SHORT format for the Locale associated with the current request
arrays - assuming the individual strings can be coverted to the individual items
collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is created
在默认的拦截器栈中包含了名为conversionError的拦截器,
如果出现了类型转换错误,该拦截器负责封装fieldError,
并转向到名为input的逻辑视图。
默认情况下,错误的提示信息为:Invalid field value for field "{0}"
如需要改提示信息有如下两种方式:
1、在全局资源文件中提供如下内容xwork.default.invalid.fieldvalue=提示信息
2、在Action作用域,包作用域以及全局资源文件中invalid.fieldvalue.域的名字=提示信息

编写自定义类型转换器有三种方式:建议继承StrutsTypeConverter

编写完自定义类型转换器后,我们需要通过配置告诉Struts2,有两种注册方式:
1、配置全局转换器配置文件:在src下面写一个xwork-conversion.properties的配置文件,其中配置类名和转换

器名
“待转换的类型的全名(包括包路径和类名)=转换器类的全名”对
如:com.pk.vo.Point---com.pk.web.converter.MyConverter
2、应用于某个特定类的类型转换器,做法为在该类的包中添加一个格式为:
“类名-conversion.properties”的配置文件,并在文件中加入
“待转换属性的名字=转换器类的全名”对



28. 和spring的整合(两种整合方式)
28.1 Autowiring: Action是struts2自己管理
默认按照name进行自动装配: struts.objectFactory.spring.autoWire = name
28.1.1 注解 Action: 不用加Controller
28.2 Initializing Actions from Spring: Action交给spring管理
28.2.1 在spring配置Action: scope="prototype"(使配置的bean不是单例的)
28.2.2 修改struts.xml <action class="对应的bean的id">
//由spring管理时可配置注解
28.2.3 注解 Action : @Controller @Scope("prototype")
service: @Service
28.3  When an object is to be created, it uses the class attribute in the Struts
configuration to correspond to the id attribute in the Spring configuration.
If not found, the class will try to be created as usual, then be autowired by Spring.
By default, the framework will at least try to use Spring to create all its objects.
If the object cannot be created by Spring,
then the framework will create the object itself.



分享到:
评论

相关推荐

    struts2整理的东西补充

    struts2自己整理的东西,有关ognl用法 validate校验 类型转换等

    struts2整理文档

    struts2整合的一些基础知识点 保证有用

    ireport + struts2整理

    ireport + struts2 ireport里的属性详解和子报表 和集成在web项目中打印的例子

    struts2整理成册.rar

    想了解struts2那么这是你不错的选择

    struts2核心技术整理

    struts2核心技术整理。。。。。。。。。。

    Struts2内容整理

    含有struts2从头到尾的全部学习,含有各个案例的代码,struts2的各个技术,适合学习struts2的人员

    struts2框架学习笔记整理

    struts2框架学习笔记整理,作为参考相互学习,多多指正

    整理的struts2文件

    我整理的struts2文件 我整理的struts2文件 我整理的struts2文件 我整理的struts2文件

    struts2零配置个人整理文档

    默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如: &lt;constant name="struts.convention....

    struts2api文档(全)

    网上很多有关struts2的api文档都是不全的。我经过精心整理出的struts2api文档,非常齐全。

    struts2整体入门级ppt

    struts2整理把握ppt,从整体上讲叙struts2各方面的知识.

    struts2的复习

    struts2的只是复习点,同学总结的,进攻参考的

    struts2学习整理

    我主要参考了《深入浅出Struts2》、《Struts2.0中文教程》以及张冰老师的视频教程,这里借机很感谢他们,大家可以在网上收到这些资料,当然可以向我索要,我会很乐意给大家的。当然我不认为这是最好、最全的学习...

    Struts2框架笔记,自己整理很详细

    Struts2框架笔记,自己整理很详细 不看视频 看文档也能速度搞定Struts2

    struts2 资料整理汇总

    struts2 资料整理汇总 ;包含全部的struts的技术,通过学习我自己整理出来的。

    struts2个人整理文档

    struts2个人整理文档,希望给你有些帮助

    struts2标签整理

    从网上借鉴,自己整理好的struts2标签

    struts2国际化例子源码

    整理struts2国际化例子源码整理struts2国际化例子源码整理struts2国际化例子源码

    struts2入门教程

    很好的struts2入门教程,网上搜集整理的! 内容: ·常用的Struts 2.0的标签(Tag)介绍 ·Struts 2.0的Action讲解 ·Struts2国际化(i18n)您的应用程序 ·Struts2.0转换器(Converter) ·Struts 2.0中实现...

    struts2_S016_S017_repair

    官方描述: ... ... 官方建议修复方案:升级到最新版本 struts-2.3.15.1 但通常现有系统升级,可能导致不稳定及与其他...鉴于此csdn网友jzshmyt整理了一种既可以不用升级现有struts版本,有能完美解决这两个漏洞的方案,

Global site tag (gtag.js) - Google Analytics