【问题标题】:Struts 2 - redirect page and display a different URLStruts 2 - 重定向页面并显示不同的 URL
【发布时间】:2014-02-06 08:53:48
【问题描述】:

我将 Struts 2 用于我的 Web 应用程序并使用身份验证部分。我想知道在重定向页面(操作)时,如何更改它重定向到的显示 URL 而不是来自?

比如我有一个login.action页面,会提交到authentication.action,如果用户名和密码正确,就会重定向到securePage.action。

我确实知道如何执行重定向部分(我尝试了链和 redirectAction 结果类型)。我想要的是当页面被重定向到安全页面时,URL 将显示 ..../securePage.action。现在,它仍然是带有会话 ID 的 authentication.action。

我在下面分享我的部分 struts.xml

 <action name="login">
    <result>index.jsp</result>
 </action>
 <action name="authentication" class="com.myapp.action.AuthenticationAction">
    <result name="success" type="chain">hello</result>
    <result name="failure">index.jsp</result>
 </action>
  <action name="hello" 
        class="com.myapp.action.HelloWorldAction" 
        method="execute">
        <interceptor-ref name="myDefault"/>
        <result name="success">/HelloWorld.jsp</result>
        <result name="failure">index.jsp</result>
  </action>

login.action 只会打开 index.jsp,这是将提交到身份验证的登录

身份验证的动作类简单地从登录中获取用户名和密码,如果通过则设置会话值“授权”,如果用户名和密码不匹配则重定向。

然后,身份验证将使用结果类型链重定向到 hello(安全页面)。

action for hello 使用我自己的 authInterceptor 来检查会话值——当前会话是否被授权。如果没有,请返回登录。

【问题讨论】:

  • 在重定向操作的情况下,您是否指定了操作的结果(错误/成功)?您是否使用 URL 映射了这些操作?
  • 你能分享你的 struts.xml 吗?我想看几件事
  • 如果在 redirect 期间 URL 没有更改,那么您没有进行重定向。
  • 我的观点正是@DaveNewton。 redirect 将始终更新 URL。似乎 OP 配置了其他东西,使他对重定向感到困惑。也许像一个 ajax 调用或类似的东西
  • @Ved 是的。我做到了。我已经添加了我的 struts.xml 片段。

标签: java struts2


【解决方案1】:

不要使用type="chain",而是尝试使用type="redirectAction"的推荐方式。

将你当前的连锁动作改为

<action name="authentication" class="com.myapp.action.AuthenticationAction">
    <result name="success" type="redirectAction">
        <param name="actionName">hello</param>
        <param name="namespace">/</param>
    </result>
    <result name="failure">index.jsp</result>
</action>
<action name="hello" 
    class="com.myapp.action.HelloWorldAction" 
    method="execute">
    <interceptor-ref name="myDefault"/>
    <result name="success">/HelloWorld.jsp</result>
    <result name="failure">index.jsp</result>
</action>

有关更多详细信息,请查看文档here

【讨论】:

  • 我按照你的指示做了,但是页面重定向后 URL 仍然是 .../authentication.action..
【解决方案2】:

您可以在struts.xml 中使用以下内容,您可以找到您的Apache Docs Reference here

<struts>
.
.
<constant name="struts.action.extension" value="" /> 
.
.
</struts>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-29
    • 2013-10-20
    • 2013-12-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多