struts2實現登入後跳轉回本頁面

bigzoom發表於2009-07-21
這是一個前臺頁面的登入,在好幾個頁面上都有登入視窗,剛開始做的不管在哪一個登入後都轉到了首頁,這顯然是不太友好的,而且使用者直接就能感覺出來的。我現在要實現的就是要在哪一頁登入,返回到哪一頁的這種情況,不過其它的情況也就大同小異了。下面,我說明方法:
一、新建一個攔截器LoginInter.java。
程式碼如下:

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

程式碼如下:

<%
String going_to = (String)session.getAttribute("GOING_TO");

if(going_to != null) {
response.sendRedirect(going_to);
}
%>

三、配置struts.xml檔案:
程式碼如下:

<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>

轉載地址:
http://blog.csdn.net/caoxiaohong/archive/2009/06/09/4253765.aspx

相關文章