Struts2 Convention 系列 2

struts2ActionConvention插件实现的时候,interceptor也就相应地需要用到Annotation来注解了。

1、interceptor需要在struts.xml中完成配置,struts2中的convention插件暂时没有对interceptor的映射支持。需要注意的问题是:当你手动配置interceptor的时候,struts2默认配置的interceptor将不会实现,所以,如果需要用到,就要自己手动将其配置上。

<interceptors>
	<interceptor name="simple" class="com.action.interceptor.SimpleInterceptor"></interceptor>
	<interceptor-stack name="myDefault">
		<interceptor-ref name="simple"></interceptor-ref>
		<interceptor-ref name="defaultStack"></interceptor-ref>
	</interceptor-stack>
</interceptors>

2、通过注解为Action配置上interceptor。
 a)为Action配置上interceptor。
 b)为方法配置上interceptor。
当为Action配置上全局的interceptor时,只需要把@InterceptorRefs(...)配置在类上。

@InterceptorRefs({
	@InterceptorRef("myDefault")
})
public class TestAction extends ActionSupport{
	
	@Override
	@Action(value="test",interceptorRefs={@InterceptorRef("simple")})
	public String execute() throws Exception {
			return SUCCESS;
	}
}

如果只是方法上的interceptor,则只要配置在所需要配置的上的@Action属性中。

public class TestAction extends ActionSupport{
	@Override
	@Action(value="test",interceptorRefs={@InterceptorRef("simple")})
	public String execute() throws Exception {
			return SUCCESS;
	}
}


interceptor源码如下:

package com.action.interceptor;

import java.util.Date;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class SimpleInterceptor 
	extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation invocation) 
			throws Exception {

		long start = System.currentTimeMillis();
		String result = invocation.invoke();
		long end = System.currentTimeMillis();
		
		System.out.println( name + " this interceptor method **********"
				+ " over this request spend time = " + (end - start) + " ms ");
		return result;
	}
	
	@Override
	public void init() {
		System.out.println("SimpleInterceptor init ***********");
		super.init();
	}
	
	@Override
	public void destroy() {
		System.out.println("SimpleInterceptor destroy ***********");
		super.destroy();
	}

}


Action源码如下:

package com.action.user;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("strutsConventionLearning")
@InterceptorRefs({
	@InterceptorRef("myDefault")
})
@Results({
	@Result(name="error",location="/index.jsp")
})
public class TestAction extends ActionSupport{

	private String name;
	private String password;
	
	@Override
	@Action(value="test",interceptorRefs={@InterceptorRef("simple")})
	public String execute() throws Exception {
		System.out.println("convertion test result is true");
		System.out.println("name = " + this.getName() + "*******");
		System.out.println("password = " + this.getPassword() + "*******");

		if(this.getName().length() < 2)
			return ERROR;
		else
			return SUCCESS;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}


struts.xml配置如下:

	<package name="strutsConventionLearning" extends="struts-default">
		<interceptors>
			<interceptor name="simple" class="com.action.interceptor.SimpleInterceptor"></interceptor>
			
			<interceptor-stack name="myDefault">
				<interceptor-ref name="simple"></interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
	</package>


页面太简单就不贴了,页面的结构如下:









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值