1.拦截器类 UserPerssionInterceptor.java
- package com.test.interceptor;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
- import com.opensymphony.xwork2.ActionContext;
- import org.apache.commons.lang.StringUtils;
- import org.apache.struts2.ServletActionContext;
- import java.util.Map;
- import java.util.Enumeration;
- import javax.servlet.http.HttpServletRequest;
- public class UserPerssionInterceptor extends AbstractInterceptor {
- private String useURL;
- public static final String MEMBER_KEY="MEMBER";
- public static final String URL_KEY="GOING_TO";
- public UserPerssionInterceptor() {
- super();
- }
- public void init() {
- super.init();
- }
- public void destroy() {
- super.destroy();
- }
- public String intercept(ActionInvocation actionInvocation) throws Exception {
- String name = actionInvocation.getInvocationContext().getName();
- String userLoginActionName = "memberlogin";
- if (name.equals(userLoginActionName)) {
- return actionInvocation.invoke();
- } else {
- ActionContext ac = actionInvocation.getInvocationContext();
- Map session = ac.getSession();
- setGoingToURL(session, actionInvocation);
- if (session.get(MEMBER_KEY) != null) {
- return actionInvocation.invoke();
- } else {
- return userLoginActionName;
- }
- }
- }
- private void setGoingToURL(Map session, ActionInvocation invocation){
- String url = "";
- String namespace = invocation.getProxy().getNamespace();
- if (StringUtils.isNotBlank(namespace) && !namespace.equals("/")){
- url = url + namespace;
- }
- String actionName = invocation.getProxy().getActionName();
- if (StringUtils.isNotBlank(actionName)){
- url = url + "/" + actionName + ".action";
- }
- session.put(URL_KEY, url);
- }
- public String getUseURL() {
- return useURL;
- }
- public void setUseURL(String useURL) {
- this.useURL = useURL;
- }
- }
2.struts.xml代码片段
- <interceptors>
- <interceptor name ="userPerssion" class ="com.test.interceptor.UserPerssionInterceptor" />
- <interceptor-stack name="shopdefault">
- <interceptor-ref name="defaultStack"/>
- </interceptor-stack>
- </interceptors>
- <global-results>
- <result name="memberlogin" type="tiles">page.member.login</result>
- </global-results>
在需要拦截的方法中加入
<interceptor-ref
name
=
"shopdefault"
/>
- <action name ="payProduct" method="payProduct" class ="orderAction">
- <interceptor-ref name ="shopdefault" />
- <result type="tiles">page.order.payproduct</result>
- </action>
登录xml
- <action name ="login!*" method="{1}" class ="loginAction">
- <result name="input" type="tiles">page.product.entry</result>
- <result type ="chain">loginAuthor</result>
- <result type="redirect">${goingToURL}</result>
- <result name="error" type="tiles">page.product.entry</result>
- </action>
3.Login.java登录类
- package com.test.action;
- import java.util.Map;
- import javax.servlet.http.HttpSession;
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.struts2.ServletActionContext;
- import org.apache.struts2.interceptor.SessionAware;
- import com.test.service.MemberService;
- import com.test.interceptor.UserPerssionInterceptor;
- import com.opensymphony.xwork2.ActionSupport;
- public class LoginAction extends ActionSupport implements SessionAware
- {
- private MemberService memberService ;
- private String memberId ;
- private String password ;
- public Map session;
- private String goingToURL;
- public String execute(){
- String url = null;
- boolean flag = this.memberService.isValidLogon(memberId, password);
- HttpSession se = ServletActionContext.getRequest().getSession();
- String goingToURL = (String) session.get(UserPerssionInterceptor.URL_KEY);
- if (StringUtils.isNotBlank(goingToURL)){
- setGoingToURL(goingToURL);
- session.remove(UserPerssionInterceptor.URL_KEY);
- }else{
- setGoingToURL("index.jhtml");
- }
- if(flag == true){
- session.put("MEMBER",memberId);
- return SUCCESS ;
- }
- else{
- this.memberId = memberId;
- return ERROR;
- }
- }
- public MemberService getMemberService() {
- return memberService;
- }
- public void setMemberService(MemberService memberService) {
- this.memberService = memberService;
- }
- public String getMemberId() {
- return memberId;
- }
- public void setMemberId(String memberId) {
- this.memberId = memberId;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public void setSession(Map session) {
- this.session = session;
- }
- public Map getSession() {
- return session;
- }
- public String getGoingToURL() {
- return goingToURL;
- }
- public void setGoingToURL(String goingToURL) {
- this.goingToURL = goingToURL;
- }
- }
登录成功以后根据之前保存好的url 跳转。