---
title: "Struts2 2.2.1 零配置总结"
author: "Myplace"
date: "2010-09-17T17:25:35Z"
tags: []
categories: ["非技术"]
slug: "struts2-2-2-1"
draft: false
lastmod: "2010-09-18T10:11:03Z"
id: 766077
url: "https://myplace.iteye.com/blog/766077"
---

Struts2 2.2.1这个版本相对于长使用的Struts2 2.0.14版本变化很多。
    一、Web.xml中使用的拦截器变化了，Struts2 2.2.1已经不推荐使用
```java
org.apache.struts2.dispatcher.FilterDispatcher
```
,现在推荐使用
```java
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
```
，这点改进官方说法是更好的支持Struts2与其他拦截器的共同使用
    二、Struts2的零配置 Struts2 2.2.1与Struts2 2.0.14零配置方便发生了很大的变动，Struts2 2.0.14零配置Annotation由struts2-codebehind-plugin-2.0.14.jar支持 而最新版本中已经推荐使用struts2-convention-plugin-2.2.1.jar。主要有一下几点变动
    1) @Result 中的type属性由类类型变成字符串类型。
    
```java
chain com.opensymphony.xwork2.ActionChainResult
    dispatcher org.apache.struts2.dispatcher.ServletDispatcherResult
    freemarker org.apache.struts2.views.freemarker.FreemarkerResult
    httpheader org.apache.struts2.dispatcher.HttpHeaderResult
    redirect org.apache.struts2.dispatcher.ServletRedirectResult
    redirectAction org.apache.struts2.dispatcher.ServletActionRedirectResult
    stream org.apache.struts2.dispatcher.StreamResult
    velocity org.apache.struts2.dispatcher.VelocityResult
    xslt org.apache.struts2.views.xslt.XSLTResult
    plainText org.apache.struts2.dispatcher.PlainTextResult
```

    这是名称和类的映射关系
    2) @Result value属性由location取代
    3) 经典配置比较:
    Structs2 2.0.14:
    
```java
@Namespace("/")
    @ParentPackage("default")
    @Results({
		@Result(name = Action.SUCCESS, type = ActionChainResult.class, 
		         value = "result", params = { "actionName", "chain", "namespace", "/" }
        ),
        @Result(name = "exception", value="error.jsp")
     })
```

    Structs2 2.2.1:
    
```java
@Namespace("/")
    @ParentPackage("default")
    @Results({
		@Result(name = Action.SUCCESS, type = “chain”, 
		        params = { "actionName", "chain", "namespace", "/" }
        ),
        @Result(name = "exception", location="error.jsp")
     })
```

    4) 使用零配置后命名规则变更，Struts2 2.0.14命名规则是首字符小写,例如HelloWorldAction的Action是helloWorld.action。Struts2 2.2.1默认是所有小写两个单词间使用-，对于HelloWorldAction的Action是hello-world.action。
       Struts2 2.2.1可以通过在Struts.xml中加入配置恢复以前的命名规则：
       
```java
<bean type="org.apache.struts2.convention.ActionNameBuilder" name="defaultConvention" class="org.apache.struts2.convention.DefaultActionNameBuilder"/>
       <constant name="struts.convention.actionNameBuilder" value="defaultConvention"/>
```

    5) Struts2 2.2.1的ChainIntercepter有Bug，导致使用chain Result时，两个Action中的内容不能复制，方法是自定一个ChainIntercepter，自定义的ChainIntercepter使用Struts2 2.0.14即可