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>