读书人

struts2引文配置Action及拦截器几种不

发布时间: 2012-07-29 15:26:14 作者: rapoo

struts2注解配置Action及拦截器几种不同方式写法对应的路径指向

本工程中:

Action类的类路径:com.xxx.xxx.main.action.LotteryAction 需要拦截器检查是否登录

??????????????????????????? com.xxx.xxx.main.action.ActivityAction 不需要拦截器

?

相关页面路径:WebRoot/page/lottery???? 和 WebRoot/page/activity

?

Struts.xml:

?

<constant name="struts.objectFactory" value="spring"/>
??? <constant name="struts.devMode" value="false"/>
??? <constant name="struts.i18n.reload" value="true"/>
??? <constant name="struts.locale" value="zh-CN"/>
??? <constant name="struts.i18n.encoding" value="UTF-8"/>
??? <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
??? <constant name="struts.convention.package.locators" value="action"/>
??? <constant name="struts.convention.default.parent.package" value="crud-default"/>
??? <constant name="struts.convention.result.path" value="/"/>
??? <constant name="struts.action.extension" value="do"/>
??? <constant name="struts.multipart.saveDir" value="temp"/>
??? <constant name="struts.multipart.maxSize" value="102400"/>

?

拦截器配置方式:(此部分讲拦截器配置,不用看Action方法注解上的value与results,这些在下面讲Action配置时会讲到,因为Action类声明上的注解与方法上的注解搭配不同,可以导致Action方法注解上的value与results中路径写法不同)

?

第一种方式,不按包拦截,拦截器配置到全局根包路径下,以具体的Action方式中引用为准

?

? ? ? <package name="crud-default" extends="convention-default">
??????? <interceptors>
??????????? <interceptor name="checkLogin" extends="crud-default">
??????? <!--<interceptors>-->
??????????? <!--<interceptor name="checkLogin" extends="convention-default">
??????? <!--<interceptors>
??????????? <interceptor name="checkLogin" extends="crud-default" namespace="/activity">
??????? <interceptors>
??????????? <interceptor name="checkLogin" extends="convention-default">
??????? <interceptors>
??????????? <interceptor name="checkLogin" extends="crud-default" namespace="/activity">
??????? <!--<interceptors>
??????????? <interceptor name="checkLogin" value="/"/>决定了convention插件去的起始寻找地址,这里配置的/即指向WebRoot,再加上解析出来的Namespace为lottery,所以去WebRoot/lottery/下去寻找页面,如果<constant name="struts.convention.result.path" value="/page"/>,则convention插件去的起始寻找地址为/WebRoot/page/再加上解析出来的Namespace为lottery,就会去WebRoot/page/lottery/去寻找页面,这样的话,location = "lotteryinfo.ftl"即可)

?

第二种方式:Action类声明中注解指定Namespace

?

A、action名中带/

?

@Controller("lotteryAction")
@Namespace("/lottery")
public class LotteryAction extends AbstratcBusiAction {

?

????? @org.apache.struts2.convention.annotation.Action(
??????????? value = "/showLotteryInfo",? 带/
??????????? results = {
??????????????????? @Result(name = "success", type = "freemarker", location = "/page/lottery/lotteryinfo.ftl"),
??????????????????? @Result(name = "fail", type = "freemarker", location = "/page/lottery/failedmessage.ftl")

??????????? })
??? public String showLotteryInfo() {...}

?

}

?

? 此种方式中value = "/showLotteryInfo",location=的值将会以简单的字符串拼接形式组成,(除非location="/"开头的绝对地址)

?

????? location = "../page/lottery/lotteryinfo.ftl",去向的地址将会是:WebRoot/lottery/../page/lottery/lotteryinfo.ftl,这是错误的(这是convention插件根据@Namespace("/lottery")解析出来的寻找页面起始为"WebRoot/lottery/"+"../page/lottery/lotteryinfo.ftl")拼接而成。

?

????? location = "/page/lottery/lotteryinfo.ftl",去向的地址将会是:WebRoot/page/lottery/lotteryinfo.ftl,这是正确的(因为location=/开头,表始绝对地址WebRoot,无论<constant name="struts.convention.result.path" value="/"/>还是<constant name="struts.convention.result.path" value="/page"/>,这个以/开头的 location = "/page/lottery/lotteryinfo.ftl"始终是指向WebRoot/page/lottery/lotteryinfo.ftl的)

?

总结: value = "/showLotteryInfo"这种action名中带/的方式,不会对location指向的一个由./或../开头的相对地址进行解析的,对location指向由/开头的,无论<constant name="struts.convention.result.path" value="?"/>,始终将/解析成WebRoot.

?

B、action名中不带/

?

@Controller("lotteryAction")
@Namespace("/lottery")
public class LotteryAction extends AbstratcBusiAction {

?

????? @org.apache.struts2.convention.annotation.Action(
??????????? value = "showLotteryInfo",? 不带/
??????????? results = {
??????????????????? @Result(name = "success", type = "freemarker", location = "/page/lottery/lotteryinfo.ftl"),
??????????????????? @Result(name = "fail", type = "freemarker", location = "/page/lottery/failedmessage.ftl")

??????????? })
??? public String showLotteryInfo() {...}

?

}

?

location = "../page/lottery/lotteryinfo.ftl",去向的地址将会是:WebRoot/page/lottery/lotteryinfo.ftl,这是正确的(这是convention插件根据@Namespace("/lottery")解析出来的寻找页面起始为"WebRoot/lottery/"

这个地址的../也就是上一级目录下的page/lottery/lotteryinfo.ftl,此时的<constant name="struts.convention.result.path" value="/"/>;

?

如果此时的<constant name="struts.convention.result.path" value="/page"/>,则解析出来的寻找页面起始为WebRoot/page/lottery/,?? location = "../page/lottery/lotteryinfo.ftl"就会变成WebRoot/page/page/lottery/lotteryinfo.ftl,这是错误的,应同步修正成location = "lotteryinfo.ftl"即可,不能是location = "/lotteryinfo.ftl"(带上/就变成了WebRoot/lotteryinfo.ftl这个地址了)

?

?

????? location = "/page/lottery/lotteryinfo.ftl",去向的地址将会是:WebRoot/page/lottery/lotteryinfo.ftl,这是正确的(因为location=/开头,表始绝对地址WebRoot,无论<constant name="struts.convention.result.path" value="/"/>还是<constant name="struts.convention.result.path" value="/page"/>,这个以/开头的 location = "/page/lottery/lotteryinfo.ftl"始终是指向WebRoot/page/lottery/lotteryinfo.ftl的)

?

总结:value = "showLotteryInfo"这种action名中不带/的方式,将会对location指向的一个由./或../开头的相对地址进行解析的,对location指向由/开头的,无论<constant name="struts.convention.result.path" value="?"/>,始终将/解析成WebRoot.

?

本工程使用到的相关jar包

?

<dependencies>
??? ??? <!-- spring相关 -->??? ???
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-beans</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-context</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-context-support</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-core</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-web</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-aop</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-orm</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-jdbc</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-tx</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.springframework</groupId>
??? ??? ??? <artifactId>spring-test</artifactId>
??? ??? ??? <version>2.5.6</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>

??? ??? <!-- 页面相关 -->
??? ??? <dependency>
??? ??? ??? <groupId>org.freemarker</groupId>
??? ??? ??? <artifactId>freemarker</artifactId>
??? ??? ??? <version>2.3.18</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.apache.struts</groupId>
??? ??? ??? <artifactId>struts2-core</artifactId>
??? ??? ??? <version>2.1.8</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.apache.struts</groupId>
??? ??? ??? <artifactId>struts2-spring-plugin</artifactId>
??? ??? ??? <version>2.1.8</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.apache.struts</groupId>
??? ??? ??? <artifactId>struts2-convention-plugin</artifactId>
??? ??? ??? <version>2.1.8</version>
??? ??? </dependency>

??? ??? <!-- db与memcache相关 -->
??? ??? <dependency>
??? ??? ??? <groupId>spy</groupId>
??? ??? ??? <artifactId>spymemcached</artifactId>
??? ??? ??? <version>2.7.3</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.experlog</groupId>
??? ??? ??? <artifactId>xapool</artifactId>
??? ??? ??? <version>1.6-beta</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>javax.transaction</groupId>
??? ??? ??? <artifactId>jta</artifactId>
??? ??? ??? <version>1.1</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>postgresql</groupId>
??? ??? ??? <artifactId>postgresql</artifactId>
??? ??? ??? <version>9.0-801.jdbc4</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>c3p0</groupId>
??? ??? ??? <artifactId>c3p0</artifactId>
??? ??? ??? <version>0.9.1.2</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>commons-pool</groupId>
??? ??? ??? <artifactId>commons-pool</artifactId>
??? ??? ??? <version>1.5.5</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.apache.ibatis</groupId>
??? ??? ??? <artifactId>ibatis-sqlmap</artifactId>
??? ??? ??? <version>2.3.4.726</version>
??? ??? </dependency>
??? ??? <!-- 全局分布式事务所需jar -->
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>atomikos-util</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>transactions</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>transactions-api</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>transactions-jta</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>transactions-osgi</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>transactions-jms-deprecated</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>transactions-jms</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.atomikos</groupId>
??? ??? ??? <artifactId>transactions-jdbc</artifactId>
??? ??? ??? <version>3.7.0</version>
??? ??? </dependency>

??? ??? <!-- 其他第三方 -->
??? ??? <dependency>
??? ??? ??? <groupId>org.aspectj</groupId>
??? ??? ??? <artifactId>aspectjrt</artifactId>
??? ??? ??? <version>1.6.12</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.aspectj</groupId>
??? ??? ??? <artifactId>aspectjweaver</artifactId>
??? ??? ??? <version>1.6.12</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>cglib</groupId>
??? ??? ??? <artifactId>cglib-nodep</artifactId>
??? ??? ??? <version>2.2.2</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>aopalliance</groupId>
??? ??? ??? <artifactId>aopalliance</artifactId>
??? ??? ??? <version>1.0</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.apache.mina</groupId>
??? ??? ??? <artifactId>mina-core</artifactId>
??? ??? ??? <version>2.0.4</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.sun.jdmk</groupId>
??? ??? ??? <artifactId>jmxtools</artifactId>
??? ??? ??? <version>1.2.1</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>javax.management</groupId>
??? ??? ??? <artifactId>jmxremote_optional</artifactId>
??? ??? ??? <version>1.0.1_04</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>commons-lang</groupId>
??? ??? ??? <artifactId>commons-lang</artifactId>
??? ??? ??? <version>2.2</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>commons-collections</groupId>
??? ??? ??? <artifactId>commons-collections</artifactId>
??? ??? ??? <version>3.2.1</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>commons-codec</groupId>
??? ??? ??? <artifactId>commons-codec</artifactId>
??? ??? ??? <version>1.5</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>com.caucho</groupId>
??? ??? ??? <artifactId>hessian</artifactId>
??? ??? ??? <version>4.0.7</version>
??? ??? </dependency>??? ???

??? ??? <!-- 日志与测试 相关 -->
??? ??? <dependency>
??? ??? ??? <groupId>org.slf4j</groupId>
??? ??? ??? <artifactId>slf4j-log4j12</artifactId>
??? ??? ??? <version>1.6.1</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>log4j</groupId>
??? ??? ??? <artifactId>log4j</artifactId>
??? ??? ??? <version>1.2.16</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>org.slf4j</groupId>
??? ??? ??? <artifactId>slf4j-nop</artifactId>
??? ??? ??? <version>1.6.4</version>
??? ??? </dependency>
??? ??? <dependency>
??? ??? ??? <groupId>junit</groupId>
??? ??? ??? <artifactId>junit</artifactId>
??? ??? ??? <version>4.4</version>
??? ??? ??? <scope>compile</scope>
??? ??? </dependency>
??????? <dependency>
??????????? <groupId>org.apache.struts.xwork</groupId>
??????????? <artifactId>xwork-core</artifactId>
??????????? <version>2.2.3.1</version>
??????? </dependency>
??? </dependencies>

?

读书人网 >软件架构设计

热点推荐