这是我的 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>
这是我的 struts.xml:
<struts>
<action name="getUrl" class="UrlAction">
<result name="redirect" type="redirect">${url}</result>
</action>
</struts>
这是我的 Action 类:
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";
}
}
所以当我运行 index.jsp 时,它应该将我重定向到 https://www.google.com ,但我不明白。它正在打印“Entering execute() of Action”。这意味着它将进入 Action 类。如果我做错了什么,请纠正我。
最佳答案
你应该改变
<result name="redirect" type="redirect">${url}</result>
到
<result name="redirect" type="redirect">
<param name="location">${url}</param>
</result>
和
<s:action name="getUrl"></s:action>
到
<s:action name="getUrl" executeResults="true"></s:action>
让它发挥作用;
但是,即使这样做了,您的页面也不会被重定向,因为那不是 <s:action>
是什么标签确实如此,according to the documentation :
This tag enables developers to call actions directly from a JSP page by specifying the action name and an optional namespace. The body content of the tag is used to render the results from the Action. Any result processor defined for this action in struts.xml will be ignored, unless the executeResult parameter is specified.
您可以在此处找到使用示例:http://www.mkyong.com/struts2/struts-2-action-tag-example/
我猜你可能会在你的标签中看到重定向的谷歌页面(修改后)
编辑
您应该准确描述您想要达到的目标;你首先问了一个关于 <s:action>
的问题标签,但我很确定这不是你需要的(虽然我仍然不明白你想要什么,除了在某个时候某处的“重定向”之外)
如果您想在打开页面时立即重定向,只需使用 <s:url>
标记以将 url 安装到您的操作中,并将其放入更改位置的 javascript 脚本中:
<script>
location.href = "<s:url action="getUrl.action" />";
</script>
(但这会很奇怪,因为如果您需要在来自另一个操作时重定向,则没有理由从 JSP 页面传递,只需使用 redirectAction
结果而不是 redirect
结果)
如果您想在按下按钮时重定向,请使用 <s:submit>
标签:
<s:submit action="getUrl.action" value="press here to redirect" />
首先确定并描述不可知论您的目标,然后获取有关帮助您实现该目标的技术的信息。
关于java - 重定向在 struts2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16823909/