Reputation: 305
Here is my index.jsp file:
<%@ 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>
<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. It is printing "Entering execute() of Action". It means it is going in Action class. Please correct me if I am doing something wrong.
Upvotes: 3
Views: 5861
Reputation: 50193
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:
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.
You can find an example of usage here: 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)
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:
<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 )
If you instead want to redirect when pressing a button, use <s:submit>
tag:
<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.
Upvotes: 8