您的位置:首页 > 编程语言 > Java开发

(Struts2)登录失败跳转到index.jsp

2015-07-10 12:12 369 查看

登录成功,登录到login.jsp

登录失败,重定向到index.jsp

忙了老长时间,登录失败一直没有进去index.jsp

<package name="default" namespace="/login" extends="struts-default">

<action name="login" class="net.qbzhong.action.LoginAction"
method="execute">
<result name="success">../login.jsp</result>
<result name="fail" type="redirectAction">tologin</result>
</action>

<action name="tologin" class="net.qbzhong.action.ToLoginAction"
method="execute">
<result name="success" type="redirect">/index.jsp</result>
</action>

</package>


最后发现,是



这里当时,我也没有写type类型,所以默认的是dispatcher类型。由于新手,就查了几个简单常用的类型:

dispatcher

dispatcher主要用于返回JSP,HTML等以页面为基础的View视图,这个也是Struts2默认的Result类型。在使用dispatcher时,唯一需要指定的,是JSP或者HTML页面的设置,这个位置将被用于定位返回的页面。

Struts2本身也没有对dispatcher做出什么特殊的处理,只是简单的使用Servlet API进行forward。

如果你在Action执行完毕后,希望执行另一个Action,有2种方式可供选择。一种是forward,另外一种是redirect。在Struts2中,分别对应这两种方式的Result,就是chain和redirect。

redirect,既然是重定向,那么源地址与目标地址之间是2个不同的HttpServletRequest。所以目标地址将无法通过ValueStack等Struts2的特性来获取源Action中的数据。如果你需要对目标地址传递参数,那么需要在目标地址url或者配置文件中指出:

<!--
The redirect-action url generated will be :
/genReport/generateReport.jsp?reportType=pie&width=100&height=100
-->
<action name="gatherReportInfo" class="...">
<result name="showReportResult" type="redirect">
<param name="location">generateReport.jsp</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">${width}</param>
<param name="height">${height}</param>
</result>
</action>


同时,Redirect的Result支持在配置文件中,读取并解析源Action中ValueStack的值,并成为参数传递到Redirect的地址中。上面给出的例子中,width和height就是ValueStack中的值。

chain

chain,之前提到,chain其实只是在一个action执行完毕之后,forward到另外一个action,所以他们之间是共享HttpServletRequest的。在使用chain作为Result时,往往会配合使用ChainingInterceptor。

ChainingInterceptor的作用是在Action直接传递数据。事实上,源Action中ValueStack的数据会被做一次Copy,这样,2个Action中的数据都在ValueStack中,使得对于前台来说,通过ValueStack来取数据,是透明而共享的。

比如说,一张页面中,你可能有许多数据要显示,而某些数据的获取方式可能被很多不同的页面共享(典型来说,“推荐文章”这个小栏目的数据获取,可能会被很多页面所共享)。这种情况下,可以把这部分逻辑抽取到一个独立Action中,并使用chain,将这个Action与主Action串联起来。这样,最后到达页面的时候,页面始终可以得到每个Action中的数据。

stream

StreamResult等价于在Servlet中直接输出Stream流。这种Result被经常使用于输出图片、文档等二进制流到客户端。通过使用StreamResult,我们只需要在Action中准备好需要输出的InputStream即可。

<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="contentDisposition">filename="document.pdf"</param>
<param name="bufferSize">1024</param>
</result>


使用type=“redirectAction”时,结果就只能写Action的配置名,不能带有后缀:“.action”

<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result>
</action>
<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result>
</action>


<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result>
</action>
<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirectAction">User?u_id=${loginBean.u_id}</result>
</action>


使用type=“redirect”时,结果应是action配置名+后缀名

<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result>
</action>


<action name="Login" class="steven.actions.LoginAction">
<result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result>
</action>


redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。

redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。

chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: