您的位置:首页 > 编程语言 > Java开发

Struts2注解配置之@Action(二)

2014-08-06 13:16 316 查看
当注解配置能够简化繁杂的xml,你是否想过也要使用注解?

最近对原有项目的框架使用的Struts2进行升级。很早以前就想尝试注解的方式进行配置。但是由于项目稳定性和改配置方式可能带来的问题一直没有进行。但是这不妨碍我们程序员求知的心。哈哈~~~~。

言归正传,开始Struts2最常用的几个注解的学习吧。

@Action

使用@Action注解改变Convention plug约定的action与url之间的映射关系。

废话少说代码说事儿。

[java] view
plaincopy

package com.example.actions;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld2 extends ActionSupport {

@Action("/different/url")

public String execute() {

return SUCCESS;

}

}

本例中在String execute()方法上使用了@Action("/different/url"),它改变了Action 与Action URL之间的映射关系,而这种改变并不是“OverWrite”而是在原来约定的基础上增加了另外一种调用方式。

原本在没有@Action("/different/url")注解的情况下,我们可以在浏览器中输入:http://应用服务器ip:端口/应用/hello-world2,即可访问到com.example.actions.HelloWorld2,并最终显示http://应用服务器ip:端口/应用/hello-world2.jsp页面。(当然这里也可能是html等页面)。

在@Action("/different/url")注解的情况下,除了上述的访问方式外,我们还可以在浏览器中输入:http://应用服务器ip:端口/应用

/different/url,也可访问到com.example.actions.HelloWorld2,但最终显示http://应用服务器ip:端口/应用/different/url.jsp页面。

使用@Action注解解决一个ACTION中的一个方法,响应多个不同的URL.

实际项目开发过程中有很大的几率碰到Action的处理相同,但是最终跳转的页面不同的情况。将@Actions和@Action组合起来用即可解决这样的需求。

还是先上代码

[java] view
plaincopy

package com.example.actions;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.Actions;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld3 extends ActionSupport {

private String message;

public String getMessage() {

return message;

}

@Actions( { @Action("/one/url"), @Action("/another/url") })

public String execute() {

message = "经过HelloWorld3的处理";

return SUCCESS;

}

}

本例中在String execute()方法上使用了@Actions和@Action注解。其中@Actions注解包含@Action("/one/url")和@Action("/another/url")注解。这样的注解除了可以通过http://应用服务器ip:端口/应用/hello-world3访问com.example.actions.HelloWorld3,并最终显示http://应用服务器ip:端口/应用/hello-world3.jsp之外,还有两种访问方式:

即:

通过http://应用服务器ip:端口/应用/one/url,访问com.example.actions.HelloWorld3,并最终显示http://应用服务器ip:端口/应用/one/url.jsp。

通过http://应用服务器ip:端口/应用/another/url,访问com.example.actions.HelloWorld3,并最终显示http://应用服务器ip:端口/应用/another/url.jsp。

这样就达到了一个ACTION中的一个方法,响应多个不同的URL的效果。

使用@Action注解解决一个ACTION中的多个方法,其中每个方法响应不同的URL.

这是实际项目开发过程中最常用的。

再上一个代码示例:

[java] view
plaincopy

package com.example.actions;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld4 extends ActionSupport {

private String message;

public String getMessage() {

return message;

}

@Action("/H4/url")

public String execute() {

message = "HelloWorld4 execute()!";

return SUCCESS;

}

@Action("url")

public String doSomething() {

message = "HelloWorld4 doSomething()!";

return SUCCESS;

}

}

本例中在String execute()方法上使用了@Action("H4/url")注解。在StringdoSomething()方法中使用了@Action("url")。前者可以通过http://应用服务器ip:端口/应用/H4/url访问com.example.actions.HelloWorld4中的execute()方法,并最终显示http://应用服务器ip:端口/应用/H4/url.jsp。后者是通过http://应用服务器ip:端口/应用/url来访问com.example.actions.HelloWorld4中的doSomething()方法,并最终显示http://应用服务器ip:端口/应用/url.jsp。

这个例子里面需要注意的是@Action("/H4/url")和@Action("url")的不同,在“url”前并没有“/”这意味着"url"是基于HelloWorld4的命名空间的。假设HelloWorld4所在的包是com.example.actions.aaa,那么,只有通过http://应用服务器ip:端口/应用/aaa/url才可以访问到com.example.actions.aaa.HelloWorld4,并最终显示http://应用服务器ip:端口/应用/aaa/url.jsp。而“/H4/url”是基于“/”命名空间的。

日后再继续学习@Results和@Result。

转自:/article/9066857.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: