struts2拦截器

package com.huawei.util;
/** 
 * @ClassName: SessionNullInterceptor 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author A18ccms a18ccms_gmail_com 
 * @date 2010-11-3 下午04:30:10 
 * @name 何枫
 */
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;

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

/**
 * session为空拦截器
 */
// LoginInterceptor类主要是实现用户的登录拦截,如果session中没有登录的用户名,
// 则将该用户的请求跳转到Action.LOGIN,如果用户已经登录,则执行return ai.invoke();,表示继续执行其他操作。
public class SessionNullInterceptor extends AbstractInterceptor
{
    
    // 拦截Action处理的拦截 
    
    @Override
    public String intercept(ActionInvocation invocation) throws Exception
    {
        
        // 取得请求有联系的ActionContext举例
        
        ActionContext ctx = invocation.getInvocationContext();
        
        Map session = ctx.getSession();
        
        // 取出名为user的session属性
        
        Object obj = session.get("login");
        
        // 假如没有登陆,或者登陆所有的用户名不正确,都返回重新登陆
        
        if (obj != null)
        {
            System.out.println("拦截器:合法用户登录---");
            
            return invocation.invoke();
        }
        // 没有登陆,将提示设置成一个的HttpServletRequest属性
        
        HttpServletRequest request = (HttpServletRequest) ctx.get(StrutsStatics.HTTP_REQUEST);
        
        request.setAttribute("loginFlag", "1");
       
        System.out.println("拦截器:用户未登录---");
       
        return Action.LOGIN;
        
    }
}
/////////////////////////////////////////////////////////////////////////////////////////
//用户登录的Action
package com.huawei.action;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.SessionAware;

import com.huawei.daoImpl.StudentdaoImpl;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;

@Results( {
		@Result(name = com.opensymphony.xwork2.Action.SUCCESS, location = "/WEB-INF/content/index.jsp", type = "dispatcher"),
		@Result(name = com.opensymphony.xwork2.Action.INPUT, location = "/loginform.jsp") })
public class LoginAction extends ActionSupport implements SessionAware {
 
	private static final long serialVersionUID = 1L;

	private String loginName;

	private String password;

	private String certCode;

	private Map<String, Object> session;

	// 创建一个对象
	StudentdaoImpl stu = new StudentdaoImpl();

	@Override
	public String execute() throws Exception {
		return certCode;
	}
 
	public String getCertCode() {
		return certCode;
	}

	@RequiredStringValidator(message = "用户名不能为空!!!")
	public String getLoginName() {
		return loginName;
	}

	@RequiredStringValidator(message = "密码不能为空!!!")
	public String getPassword() {
		return password;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	@Action(value = "section_list")
 
	public String list() {
		String certCodeTemp = (String) session.get("certCode");
		if (stu.CheckAccount(loginName, password)&& certCode.equals(certCodeTemp)) 
		{
			session.put("login", "login");
			return SUCCESS;
		}
		addActionError("用户名或密码错误  Please try again!");
		return INPUT;
	}

	public void setCertCode(String certCode) {
		this.certCode = certCode;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
////////////////////////////////////////////////////////////////////////////
//在拦截的Action中加入拦截器的注解就可以拦截
// 拦截器的注解
@InterceptorRefs( {
	@InterceptorRef("authority"),
	@InterceptorRef("defaultStack") 
		          })
//////////////////////////////////////////////////////////////////////////
//struts2的配置文件struts.xml配置如下:
<struts>
<include file="struts-default.xml" />
<package name="struts-interceptor" namespace="/" extends="struts-default" >
		<interceptors>
			<interceptor name="authority"
				class="com.huawei.util.SessionNullInterceptor" />
			<interceptor-stack name="annotatedStack">
				<interceptor-ref name="authority" />
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		<!-- 定义默认拦截器 -->
		<default-interceptor-ref name="mydefault" />
		<!-- 定义全局处理结果 -->
		<global-results>
			<!-- 逻辑名为login的结果,映射到/login.jsp页面 -->
			<result name="login">/loginform.jsp</result>
		</global-results>
</package> 
</struts>

你可能感兴趣的:(apache,jsp,struts,servlet,Gmail)