Struts2 如何在Struts 2中获取请求URL?

Struts2 如何在Struts 2中获取请求URL?,struts2,Struts2,我正在尝试重定向到用户尝试登录的页面 我是说,某页→ 登录→ 某页 我知道这一点 登录 HttpServletRequest request = ServletActionContext.getRequest(); String url = request.getServletPath(); setUrl(url); 在struts.xml中 <action name="LoginPro" method="login" class="LoginAction"> <r

我正在尝试重定向到用户尝试登录的页面

我是说,某页→ 登录→ 某页

我知道这一点

登录

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}
登录错误
但它不起作用。 请求的url始终是“LoginPro”,它正在处理登录过程。 当用户单击登录按钮时,页面将转到LoginPro。所以请求url始终是loginPro

似乎是这样,; 某页→ 登录→ 登录→ LoginAction(请求url为loginPro.)→ 登录


如何将用户重定向到他们尝试登录的页面?

您如何重定向到登录操作?如果只有一个地方(或执行重定向的公共基础),您可以向会话添加一个变量,使您的登录操作SessionAware,然后在成功登录后检索/删除源URL,并使用它吗?

谢谢您的回答

我找到了这条路,而且很管用

url=request.getHeader(“referer”)


此url是调用操作的确切url。

当您单击登录链接时,然后在该请求后面的java中将url存储为静态变量

static String url = request.getHeader("referer");</p>
静态字符串url=request.getHeader(“referer”)

然后在插入登录详细信息后,调用其他方法。使用该静态变量重定向

例如:我在我的网站上使用过

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

Login/Login.jsp
${redirectUrl}
Login/Login.jsp
公共字符串execute()引发异常{
if(getCheckCookies()){
setRedirectUrl(“/login”);
返回“重定向”;
}
Cookie un=新Cookie(“un”,lemail);
联合国设置最大年龄(-1);
联合国版本(1);
servletResponse.addCookie(un);
System.out.println(“-->-->-->”+重定向URL);
返回“重定向”;
}
公共字符串输入()引发异常{
HttpServletRequest=ServletActionContext.getRequest();
setRedirectUrl(request.getHeader(“Referer”);
返回输入;
}
公共静态字符串重定向URL;
public void setRedirectUrl(字符串重定向URL){
this.redirectUrl=重定向URL;
}
公共字符串getRedirectUrl(){
返回此.URL;
}

您是否可以选择在传递给登录操作的每个页面上包含隐藏的输入或URL参数?它的值可以是当前页面,然后您可以从中设置重定向
${url}
。。我添加了登录JSP,它的值如下/MyContextPath/tiles/MyTilesLayout.jsp。。。我不知道,但不知怎么的,它与tiles相关。我的登录表单总是在页面的左侧。(包含在tiles中)。和。。对不起,我不明白你的意思。我不知道如何在会话中找到URL…?我的意思是,假设您想要访问某个需要登录的位置。您是说“您需要在此处登录”并提供链接,还是返回“登录”结果并重定向到登录操作?
    HttpServletRequest request = ServletActionContext.getRequest(); 
    String url = request.getRequestURI();