简体   繁体   English

重定向在struts2中不起作用

[英]Redirect is not working in struts2

Here is my index.jsp file: 这是我的index.jsp文件:

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

  <body>
    <s:action name="getUrl"></s:action>
  </body>
</html>

Here is my struts.xml: 这是我的struts.xml:

<struts>
  <action name="getUrl" class="UrlAction">
    <result name="redirect" type="redirect">${url}</result>
  </action>
</struts>

Here is my action class: 这是我的动作类:

public class UrlAction extends ActionSupport {

  private String url;

  public void setUrl(String url) {
    this.url = url;
  }

  public String getUrl(){
    return url;
  }

  public String execute() throws Exception {
    System.out.println("Entering execute() of Action");
    url = "https://www.google.com/";
    return "redirect";
  }

}

So when I run index.jsp then it should redirect me to https://www.google.com , but I am not getting that. 因此,当我运行index.jsp时,它应该将我重定向到https://www.google.com ,但我没有得到它。 It is printing "Entering execute() of Action". 正在打印“输入执行的执行()”。 It means it is going in Action class. 这意味着它将进入Action类。 Please correct me if I am doing something wrong. 如果我做错了,请纠正我。

You should change 你应该改变

<result name="redirect" type="redirect">${url}</result>

to

<result name="redirect" type="redirect">
    <param name="location">${url}</param>
</result>

and

<s:action name="getUrl"></s:action>

to

<s:action name="getUrl" executeResults="true"></s:action>

to make it works; 使其有效;

but, even after doing that, you won't have your page redirected, because that is not what the <s:action> tag does, according to the documentation : 但是,即使这样做,你也不会重定向你的页面,因为这不是 <s:action>标签所做的, 根据文档

This tag enables developers to call actions directly from a JSP page by specifying the action name and an optional namespace. 此标记使开发人员可以通过指定操作名称和可选命名空间直接从JSP页面调用操作。 The body content of the tag is used to render the results from the Action. 标记的正文内容用于呈现Action的结果。 Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified. 除非指定了executeResult参数,否则将忽略在struts.xml中为此操作定义的任何结果处理器。

You can find an example of usage here: http://www.mkyong.com/struts2/struts-2-action-tag-example/ 您可以在此处找到一个使用示例: http//www.mkyong.com/struts2/struts-2-action-tag-example/

I guess you will probably see the redirected google page inside your tag (after the modifies) 我想你可能会在你的标签内看到重定向的谷歌页面(修改后)


EDIT 编辑

You should describe accurately what you are trying to achieve; 你应该准确地描述你想要达到的目标; you started by asking a question about the <s:action> tag, but I'm pretty sure that is not what you need (although I've still not understood precisely what you want, apart from a "redirect" somewhere at some point) 你开始问一个关于<s:action>标签的问题,但我很确定这不是你所需要的(虽然我还没有完全理解你想要的东西,除了某个地方某个地方的“重定向” )

If you want to redirect immediately when opening the page, just use <s:url> tag to mount an url to your action, and put it in a javascript script altering the location: 如果您想在打开页面时立即重定向,只需使用<s:url>标签为您的操作安装网址,并将其放入更改位置的javascript脚本中:

<script>
    location.href = "<s:url action="getUrl.action" />";
</script>

(but it would be weird, because if you need to redirect when coming from another action, there isn't a reason to pass from the JSP page, just use redirectAction result instead of redirect result ) (但这很奇怪,因为如果你需要在来自另一个动作时重定向,没有理由从JSP页面传递,只需使用redirectAction结果而不是redirect结果)

If you instead want to redirect when pressing a button, use <s:submit> tag: 如果您想在按下按钮时重定向,请使用<s:submit>标记:

<s:submit action="getUrl.action" value="press here to redirect" />

First identify and describe agnostically your goal , then acquire information about the technologies that help you to do that. 首先识别和不可知描述你的目标 ,然后获得有关帮助你做到这一点的技术信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM