Struts2登陆拦截器

package com.nantian.surveypark.struts2.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.nantian.surveypark.domain.Survey;
import com.nantian.surveypark.domain.User;
import com.nantian.surveypark.service.SurveyService;

/**
*
*/
@Controller
@Scope("prototype")
public class SurveyAction extends BaseAction<Survey> implements UserAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private SurveyService surveyService;
	// 接受session的user对象
	private User user;
	private List<Survey> mySurveys;

	public List<Survey> getMySurveys() {
		return mySurveys;
	}

	public void setMySurveys(List<Survey> mySurveys) {
		this.mySurveys = mySurveys;
	}

	/*
	 * 新建调查
	 */
	public String newSurvey() {
		this.model = surveyService.newSurvey(user);
		return "designSurveyPage";
	}

	/**
	 * 查询我的调查
	 */
	public String mySurveys() {
		this.mySurveys = surveyService.findMySurveys(user);
		return "mySurveyListPage";
	}

	// 注入user对象
	public void setUser(User user) {
		this.user = user;
	}

}

 

package com.nantian.surveypark.struts2.action;

import com.nantian.surveypark.domain.User;

/**
 * 用户关注接口
 */
public interface UserAware {
	public void setUser(User user);
}

 

package com.nantian.surveypark.struts2.interceptor;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.nantian.surveypark.domain.User;
import com.nantian.surveypark.struts2.action.BaseAction;
import com.nantian.surveypark.struts2.action.LoginAction;
import com.nantian.surveypark.struts2.action.RegAction;
import com.nantian.surveypark.struts2.action.UserAware;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * LoginInterceptor
 */
public class LoginInterceptor implements Interceptor {

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

	public void destroy() {

	}

	public void init() {

	}

	@SuppressWarnings("rawtypes")
	public String intercept(ActionInvocation invocation) throws Exception {
		BaseAction action = (BaseAction) invocation.getAction();
		if (action instanceof LoginAction || action instanceof RegAction) {
			return invocation.invoke();
		}
		// 登录判断
		else {
			HttpSession s = ServletActionContext.getRequest().getSession();
			User user = (User) s.getAttribute("user");
			if (user == null) {
				return "login";
			} else {
				// 处理action的user注入问题
				if (action instanceof UserAware) {
					((UserAware) action).setUser(user);
				}
				return invocation.invoke();
			}
		}
	}

}

 

<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<!-- 简单主题 -->
	<constant name="struts.ui.theme" value="simple" />
	<!-- 开发模式 -->
	<constant name="struts.devMode" value="true" />
	<package name="surveyParkPkg" namespace="/" extends="struts-default">
		<interceptors>
			<!-- 注册自定义登录拦截器 -->
			<interceptor name="loginInterceptor" class="com.nantian.surveypark.struts2.interceptor.LoginInterceptor" />
			<!-- 自定义拦截器栈 -->
			<interceptor-stack name="surveyparkStack">
				<interceptor-ref name="loginInterceptor" />
				<interceptor-ref name="defaultStack">
					<param name="modelDriven.refreshModelBeforeResult">true</param>
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<!-- 定义默认栈 -->
		<default-interceptor-ref name="surveyparkStack" />
		<!-- 定义全局结果 -->
		<global-results>
			<result name="login">/index.jsp</result>
		</global-results>
		<!-- regAction -->
		<action name="RegAction_*" class="regAction" method="{1}">
			<result name="regPage">/reg.jsp</result>
			<result name="success">/index.jsp</result>
			<result name="input">/reg.jsp</result>
		</action>
		<!-- loginAction -->
		<action name="LoginAction_*" class="loginAction" method="{1}">
			<result name="loginPage">/index.jsp</result>
			<result name="success">/index.jsp</result>
			<result name="input">/index.jsp</result>
		</action>
		<!-- SurveyAction -->
		<action name="SurveyAction_*" class="surveyAction" method="{1}">
			<result name="designSurveyPage">/designSurvey.jsp</result>
			<result name="mySurveyListPage">/mySurveyList.jsp</result>
		</action>
	</package>
</struts>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值