我正在为我的网络应用程序使用 Struts 2 并处理身份验证部分。我想知道在重定向页面(操作)时,如何更改它重定向到而不是从中显示的 URL?
比如我有一个login.action页面,会提交到authentication.action,如果用户名和密码正确,就会重定向到securePage.action。
我确实知道如何进行重定向部分(我尝试了链和 redirectAction 结果类型)。我想要的是当页面被重定向到安全页面时,URL 将显示 ..../securePage.action。现在,它仍然是带有 session 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,这是将提交给身份验证的登录名
身份验证的操作类只是从登录中获取用户名和密码,如果通过则设置 session 值“授权”,如果用户名和密码不匹配则重定向。
然后身份验证将使用结果类型链重定向到 hello(安全页面)。
hello 的 Action 使用我自己的 authInterceptor 来检查 session 值——当前 session 是否被授权。如果没有,请返回登录。
最佳答案
不要使用 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
关于java - Struts 2 - 重定向页面并显示不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21178906/