struts2实现登录后跳转回本页面

   昨天在项目中用到这种技术,也在网上参考了不少资料,最终还是自己用拦截器处理了。说明方法之前,先说说我项目的情况。

      这是一个前台页面的登录,在好几个页面上都有登录窗口,刚开始做的不管在哪一个登录后都转到了首页,这显然是不太友好的,而且用户直接就能感觉出来的。我现在要实现的就是要在哪一页登录,返回到哪一页的这种情况,不过其它的情况也就大同小异了。下面,我说明方法:   一、新建一个拦截器LoginInter.java。

    代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.Map;

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

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.aircom.zzy.util.StringUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

//继承可以指定方法的拦截器接口

public class LoginInter extends MethodFilterInterceptor {

  private static final long serialVersionUID = -72827036782663208L;
 
  private static  final Logger log = Logger.getLogger(LoginInter.class);
 
     public final static String SESSION_KEY="regUserId";
     public final static String GOTO_URL_KEY="GOING_TO";  

     protected String doIntercept(ActionInvocation invocation) throws Exception   
      {    
          HttpServletRequest request=ServletActionContext.getRequest();   
            
          HttpSession session=request.getSession();   
          //通过session 判断用户是否登录
          if(session!=null && session.getAttribute(SESSION_KEY)!=null) {   
                  return invocation.invoke();   
           }   
          //这里是关键点了 设置客户原来请求的url地址   
          setToGoingURL(request, session, invocation); 
          return invocation.invoke();   
      }   
  private void setToGoingURL(HttpServletRequest request,HttpSession session,ActionInvocation invocation)   
      {   
          //如果referer不为空 直接使用它。如果为空我们分别获得命名空间,action名,以及请求参数   
          //从新构造成一个URL保存在session中   
          String url=request.getHeader("referer");   
          log.info("待转向URL:"+request.getHeader("referer"));   
          if(url==null || url.equals(""))   
              {   
                  url="";   
                  String path=request.getContextPath();   
                  String actionName=invocation.getProxy().getActionName();   
                  String nameSpace=invocation.getProxy().getNamespace();   
                  if(StringUtil.validateNull(nameSpace))   
                      {   
                          url = url+path+nameSpace;   
                             
                      }   
                  if(StringUtil.validateNull(actionName))   
                      {   
                          url = url+"/"+actionName+".action"+"?";   
                      }   
                     
                  Map<String,String[]> zzMap = request.getParameterMap();   
                  if(zzMap!=null)   
                      {   
                          for(String s:zzMap.keySet())   
                              {   
                                  String[] value=zzMap.get(s);   
                                  for(String val:value)   
                                      {   
                                          url=url+s+"="+val+"&";   
                                      }   
                                     
                              }   
                      }   
                  log.info("完整URL:"+url);   
              }   
                 session.setAttribute(GOTO_URL_KEY, url);   
      }

}

二、新建统一中间跳转页面login_result.jsp

代码如下:

1
2
3
4
5
6
7
 <%
     String going_to = (String)session.getAttribute("GOING_TO");
     
     if(going_to != null) {
      response.sendRedirect(going_to);
      }
     %>

三、配置struts.xml文件:

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<struts>
 <package name="login" extends="common">
  <interceptors>
   <interceptor name="loginInter" class="com.aircom.zzy.interceptor.LoginInter">
   </interceptor>
  </interceptors>
        <action name="login" class="com.aircom.zzy.action.LoginRegistAction" method="login">
            <result name="error">/webapp/login_result.jsp</result>
            <result name="input">/webapp/login_result.jsp</result>
            <result name="success">/webapp/login_result.jsp</result>
          <interceptor-ref name="defaultStack"/>
   <interceptor-ref name="loginInter">

    <!--只拦截这个action中的login方法-->
    <param name="includeMethods">login</param>
   </interceptor-ref>
        </action>
        <action name="logout" class="com.aircom.zzy.action.LoginRegistAction" method="logout">
            <result name="success">/webapp/login_result.jsp</result>
          <interceptor-ref name="defaultStack"/>
   <interceptor-ref name="loginInter">
    <param name="includeMethods">logout</param>
   </interceptor-ref>
        </action>
        <!-- The following set is the action for register. -->
        <action name="goRegister" class="com.aircom.zzy.action.LoginRegistAction" method="goRegisterPage">
            <result name="success">/webapp/register.jsp?num=1</result>
        </action>
        <action name="register" class="com.aircom.zzy.action.LoginRegistAction" method="register">
            <result name="success">/webapp/index.jsp</result>
            <result name="input">/webapp/register.jsp?num=1</result>
        </action>
 </package>
</struts>

 

结束了,如有什么不懂可以留言噢。呵呵。

坚持原创技术分享,您的支持将鼓励我继续创作!