拦截器应用例子

用户是否登录拦截器:

package interceptor;

import action.LoginAction;

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

public class LoginInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		if (LoginAction.class == invocation.getAction().getClass()) {
			String name = invocation.invoke();
			
			return name;
		}
		ActionContext con = invocation.getInvocationContext();
		
		if (null == con.get("userInfo")) {
			return Action.LOGIN;
		}
		return invocation.invoke();
	}

}

LoginAction.java

package action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import service.LoginService;
import service.impl.LoginServiceImpl;


import bean.Person;
import bean.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor;


public class LoginAction extends ActionSupport  {
	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	private String username;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	private String password;
	private LoginService loginService = new LoginServiceImpl();

	
	@Override
	public String execute() throws Exception {
		
		if (this.loginService.isLogin(username, password)) {
				User user = new User ();
				user.setUsername(username);
				user.setPassword(password);
				ActionContext.getContext().getSession().put("userInfo", user);
				return SUCCESS;
		}
		return Action.INPUT;
	}
	//为什么不在validate验证登陆。
	//一般在validate值是进行一些没有业务逻辑的验证,比如:验证用户的名字长度时候合法,
	//日期时候合法。而登陆是有义务逻辑,不要放在validate中。
	  public void validate() {
		  System.out.println("validate execute!");
	    }
}

struts.xml

<interceptors>
				<interceptor name = "theInterceptor1" class = "interceptor.TheInterceptor1">
					<param name="test">zhangsan</param>
				</interceptor>
				<interceptor name = "theInterceptor2" class = "interceptor.TheInterceptor2"></interceptor>
				<interceptor name = "theInterceptor3" class = "interceptor.TheInterceptor3"></interceptor>
				<interceptor name = "loginInterceptor" class = "interceptor.LoginInterceptor"></interceptor>
				<interceptor-stack name="myDefaultInterceptorStack">
					<interceptor-ref name="loginInterceptor"></interceptor-ref>
					<interceptor-ref name = "defaultStack"></interceptor-ref>
				</interceptor-stack>
			</interceptors>
			<default-interceptor-ref name = "myDefaultInterceptorStack"></default-interceptor-ref>
<global-results>
				<result name="usernameInvalid" >/usernameInvalid.jsp</result>
				<result name = "passwordInvalid">/passwordInvalid.jsp</result>
				<result name = "login">/error.jsp</result>
			</global-results>

自定义拦截栈。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值