2010-08-14 241 views
6

我正在嘗試重定向到用戶嘗試登錄的頁面。如何獲取請求URL?

我的意思是,somepage→登錄→somepage

我知道這個;

在LoginAction中

HttpServletRequest request = ServletActionContext.getRequest(); 
String url = request.getServletPath(); 
setUrl(url); 

在struts.xml中

<action name="LoginPro" method="login" class="LoginAction"> 
    <result type="redirect">${url}</result> 
    <result name="input" type="tiles">login.error</result> 
</action> 

但它不工作。 請求的url始終是「LoginPro」,它正在處理登錄過程。 當用戶點擊登錄按鈕時,頁面進入LoginPro。所以請求url始終是loginPro ...

它似乎是這樣; somepage→登錄→loginPro→LoginAction(請求url是loginPro ..)→loginPro

如何將用戶重定向到他們嘗試登錄的頁面?

+0

是否可以選擇讓每個頁面包含一個隱藏的輸入或URL參數以傳遞給登錄操作?它的值可以是當前頁面,然後你可以設置你的重定向'$ {url}'。 – Pat 2010-08-15 00:20:25

+0

我試過你的選擇..我在登錄JSP中添加<%= request.getServletPath()%>,它的值是這樣的... /MyContextPath/tiles/MyTilesLayout.jsp ...我不知道但是它以某種方式與瓷磚相關。 – Deckard 2010-08-15 14:16:06

回答

5

感謝您的回答。

我發現這種方式,它的工作原理!

url = request.getHeader(「referer」);

此網址是動作被調用的確切網址。

+0

謝謝!很有用! – 2014-06-03 11:51:23

0

如何重定向到您的登錄操作?如果只有一個地方(或重定向的一些公共基地)可以向會話添加一個變量,請使用Login Action操作SessionAware,然後在成功登錄後檢索/刪除源URL,然後使用它?

+0

我的登錄表單總是位於頁面的左側部分(由瓦片包含)。而..抱歉,我不明白你的意思。我不知道如何在會話中查找URL ..? – Deckard 2010-08-15 14:11:04

+0

我的意思是,假設您想訪問需要登錄的某個位置。您是否說「您需要在此登錄」並提供鏈接,或者您是否返回「登錄」結果並重定向至登錄操作? – 2010-08-15 14:57:17

3

當你點擊一個Login的鏈接,然後在java後面的那個請求存儲url作爲靜態變量。

static String url = request.getHeader("referer");</p> 

然後在插入登錄詳細信息後,您可以調用其他方法。使用該靜態變量來重定向。

例如:我用在我的網站。

<action name="login" class="actions.Login.LoginAuthenticate" method="input">  
    <!--Cookie functionality done --> 
    <result name="input">Login/login.jsp</result> 
</action> 

<action name="loginAuthenticate" class="actions.Login.LoginAuthenticate" method="execute"> 
     <!--Cookie functionality done --> 
     <result name="redirect" type="redirect">${redirectUrl}</result> 
     <result name="input">Login/login.jsp</result> 
</action> 

public String execute() throws Exception { 
    if(getCheckCookies()){ 
      setRedirectUrl("/login"); 
      return "redirect"; 
    } 
    Cookie un = new Cookie("un" , lemail); 
    un.setMaxAge(-1); 
    un.setVersion(1); 
    servletResponse.addCookie(un); 
    System.out.println("------>--------->------> " + redirectUrl); 
    return "redirect"; 
} 


public String input() throws Exception { 
    HttpServletRequest request = ServletActionContext.getRequest(); 
    setRedirectUrl(request.getHeader("Referer")); 
    return INPUT; 
} 

public static String redirectUrl; 

public void setRedirectUrl(String redirectUrl){ 
    this.redirectUrl = redirectUrl; 
} 
public String getRedirectUrl(){ 
    return this.redirectUrl; 
}