【问题标题】:redirect to an action then to another jsp using <s:action> in struts2在struts2中使用<s:action>重定向到一个动作然后到另一个jsp
【发布时间】:2015-02-13 05:48:28
【问题描述】:

我的 index.jsp 是

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 <head>
<title>HOME</title>
</head>

<body>
Hi you are in index.jsp page
<s:action name="error" executeResult="true"></s:action>
</body>
</html>

我的错误动作类是

public class error extends ActionSupport {
  public String execute() throws Exception {
    System.out.println("in error action.........");
    return SUCCESS;
  }
}

我的 struts.xml

<action name="error" class="action.error">            
        <result name="success" type="redirect">                                                      
            <param name="location">/Login.jsp</param>
        </result>            
</action>

当我点击 index.jsp 页面时,我想重定向到 Login.jsp。但每次我点击 index.jsp 时,我都会回到我的 index.jsp 页面,但这次它是空白的。它将在打印“错误操作............”时采取行动。但它没有重定向到 Login.jsp

我做错了什么?

【问题讨论】:

    标签: jsp struts2


    【解决方案1】:

    我做错了什么?

    基本上都是。

    1. 根据Java Naming Conventions,类名必须以大写字母开头:

      public class Error extends ActionSupport {
      
    2. 命名error 一个返回登录数据(或页面)的操作是对POLA 的重大违反; login 这样的名字更合适;

    3. redirect 结果必须指向一个 URL,通常是外部的(例如 http://www.google.com/)。要重定向到动作,使用redirectAction 结果,指向视图(例如JSP)使用默认结果dispatcher(可以省略)。

      <action name="login" class="action.Login">  
          <result name="success">/Login.jsp</result>
      </action>
      
    4. 使用&lt;s:action&gt; 标签在 99% 的情况下是不必要的,有时也是错误的,就像在这种情况下,因为结果执行将被注入到主页中。您可以注入一个 JSP sn-p,其中包含一个带有 window.location.href = something 的 Javascript 块,但这不是 Circus;
      重定向正确的方式,或者使用meta refresh,或者(更好)甚至不登陆index.jsp,直接从Index.action重定向(使用@987654339 @result) 到Login.action,然后登陆login.jsp

      否则,您可以使用这样的小 Scriptlet:

      <% response.sendRedirect("/login.action"); %> 
      

      但不是来自包含的 JSP,仅来自顶层。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 2016-07-16
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      相关资源
      最近更新 更多