You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ei...@evita.no on 2000/09/25 13:59:02 UTC
Bug in struts -- making "forward mappings" redirectable
I've been using the "forward"-feature of the action.xml-file, to redirect
the user to another page. For example, in the login-action (which is
/login.do), I want to redirect the user to the main menu. Since I don't
want the URL to be "/login.do" for the main menu, but rather
"/mainmenu.jsp", I used the redirect-attribute:
<forward name="success" path="/mainmenu.jsp" redirect="true" />
Now, this does not work. Struts (and servlets, it seems) does not
acknowledge the presense of web applications while forwarding. This makes
the coding of actions.xml really weird, with references to "/path" and
"/webapps/thisapp/path" depending on the situation.
To make the forward and redirect cased behave similarly, I propose the
following change to ActionServlet.processActionPerform(...) -- commented
with C-style preprocessor directives:
// Obey any returned ActionForward
if (forward != null) {
String path = forward.getPath();
#ifdef OLD_CODE
if (forward.getRedirect())
response.sendRedirect(path);
#else
if (forward.getRedirect()) {
if (path.startsWith("/"))
path = request.getContextPath() + path;
response.sendRedirect(path);
}
#endif
else {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
}
What do you think?
Eirik
++++++++++
Eirik Lygre
eirik.lygre@evita.no
e-vita as, Stortorvet 3, Oslo
Mobil: (+47) 905 66476
Fax: (+47) 23 35 70 51
----- Forwarded by Eirik Lygre/evita/no on 20.09.2000 21:25 -----
Mike Papper <mp...@geocities.com>
14.09.2000 09:38
Please respond to struts-user
To: "'struts-user@jakarta.apache.org'" <st...@jakarta.apache.org>
cc:
Subject: making "forward mappings" redirectable
Struts question #1:
We have something like the following in action.xml
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
what I really want is
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/ redirect="true">
</action>
I have looked high and low throughoutthe struts code for how to add code
to understand this and cannot find it. The closest I am getting is the
digester "magic" code that does who knows what...
anyone know if this can be done? Or how I could edit the ocde myself to
do it?
Also, if I could do such a thing then I would want the class that
"interprets" the path for a redirect to be SMARt enough to prepend the
path with the context path (but only if necessary) so that it works
correctly. (I.e., if the URL starts with / then it must be prepended with
the request.getContextPath() but if it doesnt start with a / then its a
relative url and will be interpreted relative to the current page and the
current page already has the context's path in front of it). I would be
happy to explain this in more detail.
Speaking of which, is the following from ActionServlet's
processActionInstance method a bug?
...
ActionForward forward =
actionInstance.perform(this, mapping, formInstance,
request, response);
if (forward != null) {
String path = forward.getPath();
if (forward.getRedirect()) {
response.sendRedirect(path);
shouldnt the last line be:
response.sendRedirect(response.encodeRedirectURL(path));
--
Mike Papper, E-mail: mpapp@geocities.com
Hardhat Wholesale, 180 Borica St., San Francisco, CA 94127
Tel: 510-568-7770 ext. 17, Fax: (at work) 510-568-7750
Re: Bug in struts -- making "forward mappings" redirectable
Posted by Mike Papper <mp...@geocities.com>.
I discovered this same problem and created a method that wraps around the
forward method it examoines the paths and makes sure its "ok" for
forwarding. Heres some sample code (I put into my version of ActionBase):
public ActionForward findForward(HttpServletRequest request,
ActionMapping mapping, String name) {
ActionForward method = mapping.findForward("method");
ActionForward forward = mapping.findForward(name);
String path = forward.getPath();
// if were redirecting, we must use the full path (a forward will
always be relative to our context root)
// if were forwarding, it calculates the path relative to our context
// (even if the path begins with a /)
// if were redirecting and the path begins with a / then we must put
the context (i.e., /hhw)
// into the path ourselves. If we redirect to a path that doesnt begin
with a /
// then we dont know if this path is relative to the current page or
implies "use the context root"
// from the ActionForward class:
// Context-relative URI to which control should
// be forwarded, or an absolute or relative URI to which control
should
// be redirected
// NOTE: to do a forward the path MUST be context relative which means
it MUST start with a /
// because the request dispatcher is used
// however for a forward, the response is used and it converts a
relative path to an absolute one
// (but does not take into account the context).
// If they do start with a / they are not relative to the context and
we have to sub-in the context
if (method != null && method.getPath().equals("redirect")) {
forward.setRedirect(true);
// if path starts with a / then prepend the context path (unless it
happens to also start with the context
// path (as that was already there)
if (path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
}
else if (!forward.getRedirect() && !path.startsWith("/")) { // if path
doesnt start with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
public ActionForward createForward(HttpServletRequest request,
ActionMapping mapping, String path) {
boolean redirect = false;
if (mapping != null) {
ActionForward method = mapping.findForward("method");
redirect = method != null && method.getPath().equals("redirect");
}
ActionForward forward = new ActionForward(path, redirect);
if (redirect && path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
else if (!redirect && !path.startsWith("/")) { // if path doesnt start
with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
Original Message dated 9/25/2000, 4:59:02 AM
Author: eirik.lygre@evita.no
Re: Bug in struts -- making "forward mappings" redirectable:
I've been using the "forward"-feature of the action.xml-file, to redirect
the user to another page. For example, in the login-action (which is
/login.do), I want to redirect the user to the main menu. Since I don't
want the URL to be "/login.do" for the main menu, but rather
"/mainmenu.jsp", I used the redirect-attribute:
<forward name="success" path="/mainmenu.jsp" redirect="true" />
Now, this does not work. Struts (and servlets, it seems) does not
acknowledge the presense of web applications while forwarding. This makes
the coding of actions.xml really weird, with references to "/path" and
"/webapps/thisapp/path" depending on the situation.
To make the forward and redirect cased behave similarly, I propose the
following change to ActionServlet.processActionPerform(...) -- commented
with C-style preprocessor directives:
// Obey any returned ActionForward
if (forward != null) {
String path = forward.getPath();
#ifdef OLD_CODE
if (forward.getRedirect())
response.sendRedirect(path);
#else
if (forward.getRedirect()) {
if (path.startsWith("/"))
path = request.getContextPath() + path;
response.sendRedirect(path);
}
#endif
else {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
}
What do you think?
Eirik
++++++++++
Eirik Lygre
eirik.lygre@evita.no
e-vita as, Stortorvet 3, Oslo
Mobil: (+47) 905 66476
Fax: (+47) 23 35 70 51
----- Forwarded by Eirik Lygre/evita/no on 20.09.2000 21:25 -----
Mike Papper <mp...@geocities.com>
14.09.2000 09:38
Please respond to struts-user
To: "'struts-user@jakarta.apache.org'"
<st...@jakarta.apache.org>
cc:
Subject: making "forward mappings" redirectable
Struts question #1:
We have something like the following in action.xml
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
what I really want is
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/ redirect="true">
</action>
I have looked high and low throughoutthe struts code for how to add code
to understand this and cannot find it. The closest I am getting is the
digester "magic" code that does who knows what...
anyone know if this can be done? Or how I could edit the ocde myself to
do it?
Also, if I could do such a thing then I would want the class that
"interprets" the path for a redirect to be SMARt enough to prepend the
path with the context path (but only if necessary) so that it works
correctly. (I.e., if the URL starts with / then it must be prepended with
the request.getContextPath() but if it doesnt start with a / then its a
relative url and will be interpreted relative to the current page and the
current page already has the context's path in front of it). I would be
happy to explain this in more detail.
Speaking of which, is the following from ActionServlet's
processActionInstance method a bug?
...
ActionForward forward =
actionInstance.perform(this, mapping, formInstance,
request, response);
if (forward != null) {
String path = forward.getPath();
if (forward.getRedirect()) {
response.sendRedirect(path);
shouldnt the last line be:
response.sendRedirect(response.encodeRedirectURL(path));
--
Mike Papper, E-mail: mpapp@geocities.com
Hardhat Wholesale, 180 Borica St., San Francisco, CA 94127
Tel: 510-568-7770 ext. 17, Fax: (at work) 510-568-7750
Re: Bug in struts -- making "forward mappings" redirectable
Posted by Mike Papper <mp...@geocities.com>.
I discovered this same problem and created a method that wraps around the
forward method it examoines the paths and makes sure its "ok" for
forwarding. Heres some sample code (I put into my version of ActionBase):
public ActionForward findForward(HttpServletRequest request,
ActionMapping mapping, String name) {
ActionForward method = mapping.findForward("method");
ActionForward forward = mapping.findForward(name);
String path = forward.getPath();
// if were redirecting, we must use the full path (a
forward will
always be relative to our context root)
// if were forwarding, it calculates the path relative to
our context
// (even if the path begins with a /)
// if were redirecting and the path begins with a / then
we must put
the context (i.e., /hhw)
// into the path ourselves. If we redirect to a path that
doesnt begin
with a /
// then we dont know if this path is relative to the
current page or
implies "use the context root"
// from the ActionForward class:
// Context-relative URI to which control should
// be forwarded, or an absolute or relative URI
to which control
should
// be redirected
// NOTE: to do a forward the path MUST be context
relative which means
it MUST start with a /
// because the request dispatcher is used
// however for a forward, the response is used and it
converts a
relative path to an absolute one
// (but does not take into account the context).
// If they do start with a / they are not relative to the
context and
we have to sub-in the context
if (method != null &&
method.getPath().equals("redirect")) {
forward.setRedirect(true);
// if path starts with a / then prepend the
context path (unless it
happens to also start with the context
// path (as that was already there)
if (path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
}
else if (!forward.getRedirect() && !path.startsWith("/"))
{ // if path
doesnt start with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
public ActionForward createForward(HttpServletRequest request,
ActionMapping mapping, String path) {
boolean redirect = false;
if (mapping != null) {
ActionForward method =
mapping.findForward("method");
redirect = method != null &&
method.getPath().equals("redirect");
}
ActionForward forward = new ActionForward(path,
redirect);
if (redirect && path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
else if (!redirect && !path.startsWith("/")) { // if
path doesnt start
with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
Original Message dated 9/25/2000, 4:59:02 AM
Author: eirik.lygre@evita.no
Re: Bug in struts -- making "forward mappings" redirectable:
I've been using the "forward"-feature of the action.xml-file, to redirect
the user to another page. For example, in the login-action (which is
/login.do), I want to redirect the user to the main menu. Since I don't
want the URL to be "/login.do" for the main menu, but rather
"/mainmenu.jsp", I used the redirect-attribute:
<forward name="success" path="/mainmenu.jsp" redirect="true" />
Now, this does not work. Struts (and servlets, it seems) does not
acknowledge the presense of web applications while forwarding. This makes
the coding of actions.xml really weird, with references to "/path" and
"/webapps/thisapp/path" depending on the situation.
To make the forward and redirect cased behave similarly, I propose the
following change to ActionServlet.processActionPerform(...) -- commented
with C-style preprocessor directives:
// Obey any returned ActionForward
if (forward != null) {
String path = forward.getPath();
#ifdef OLD_CODE
if (forward.getRedirect())
response.sendRedirect(path);
#else
if (forward.getRedirect()) {
if (path.startsWith("/"))
path = request.getContextPath() + path;
response.sendRedirect(path);
}
#endif
else {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
}
What do you think?
Eirik
++++++++++
Eirik Lygre
eirik.lygre@evita.no
e-vita as, Stortorvet 3, Oslo
Mobil: (+47) 905 66476
Fax: (+47) 23 35 70 51
----- Forwarded by Eirik Lygre/evita/no on 20.09.2000 21:25 -----
Mike Papper <mp...@geocities.com>
14.09.2000 09:38
Please respond to struts-user
To: "'struts-user@jakarta.apache.org'"
<st...@jakarta.apache.org>
cc:
Subject: making "forward mappings" redirectable
Struts question #1:
We have something like the following in action.xml
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
what I really want is
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/ redirect="true">
</action>
I have looked high and low throughoutthe struts code for how to add code
to understand this and cannot find it. The closest I am getting is the
digester "magic" code that does who knows what...
anyone know if this can be done? Or how I could edit the ocde myself to
do it?
Also, if I could do such a thing then I would want the class that
"interprets" the path for a redirect to be SMARt enough to prepend the
path with the context path (but only if necessary) so that it works
correctly. (I.e., if the URL starts with / then it must be prepended with
the request.getContextPath() but if it doesnt start with a / then its a
relative url and will be interpreted relative to the current page and the
current page already has the context's path in front of it). I would be
happy to explain this in more detail.
Speaking of which, is the following from ActionServlet's
processActionInstance method a bug?
...
ActionForward forward =
actionInstance.perform(this, mapping, formInstance,
request, response);
if (forward != null) {
String path = forward.getPath();
if (forward.getRedirect()) {
response.sendRedirect(path);
shouldnt the last line be:
response.sendRedirect(response.encodeRedirectURL(path));
--
Mike Papper, E-mail: mpapp@geocities.com
Hardhat Wholesale, 180 Borica St., San Francisco, CA 94127
Tel: 510-568-7770 ext. 17, Fax: (at work) 510-568-7750
Re: Bug in struts -- making "forward mappings" redirectable
Posted by Mike Papper <mp...@geocities.com>.
I discovered this same problem and created a method that wraps around the
forward method it examoines the paths and makes sure its "ok" for
forwarding. Heres some sample code (I put into my version of ActionBase):
public ActionForward findForward(HttpServletRequest request,
ActionMapping mapping, String name) {
ActionForward method = mapping.findForward("method");
ActionForward forward = mapping.findForward(name);
String path = forward.getPath();
// if were redirecting, we must use the full path (a forward will
always be relative to our context root)
// if were forwarding, it calculates the path relative to our context
// (even if the path begins with a /)
// if were redirecting and the path begins with a / then we must put
the context (i.e., /hhw)
// into the path ourselves. If we redirect to a path that doesnt begin
with a /
// then we dont know if this path is relative to the current page or
implies "use the context root"
// from the ActionForward class:
// Context-relative URI to which control should
// be forwarded, or an absolute or relative URI to which control
should
// be redirected
// NOTE: to do a forward the path MUST be context relative which means
it MUST start with a /
// because the request dispatcher is used
// however for a forward, the response is used and it converts a
relative path to an absolute one
// (but does not take into account the context).
// If they do start with a / they are not relative to the context and
we have to sub-in the context
if (method != null && method.getPath().equals("redirect")) {
forward.setRedirect(true);
// if path starts with a / then prepend the context path (unless it
happens to also start with the context
// path (as that was already there)
if (path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
}
else if (!forward.getRedirect() && !path.startsWith("/")) { // if path
doesnt start with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
public ActionForward createForward(HttpServletRequest request,
ActionMapping mapping, String path) {
boolean redirect = false;
if (mapping != null) {
ActionForward method = mapping.findForward("method");
redirect = method != null && method.getPath().equals("redirect");
}
ActionForward forward = new ActionForward(path, redirect);
if (redirect && path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
else if (!redirect && !path.startsWith("/")) { // if path doesnt start
with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
Original Message dated 9/25/2000, 4:59:02 AM
Author: eirik.lygre@evita.no
Re: Bug in struts -- making "forward mappings" redirectable:
I've been using the "forward"-feature of the action.xml-file, to redirect
the user to another page. For example, in the login-action (which is
/login.do), I want to redirect the user to the main menu. Since I don't
want the URL to be "/login.do" for the main menu, but rather
"/mainmenu.jsp", I used the redirect-attribute:
<forward name="success" path="/mainmenu.jsp" redirect="true" />
Now, this does not work. Struts (and servlets, it seems) does not
acknowledge the presense of web applications while forwarding. This makes
the coding of actions.xml really weird, with references to "/path" and
"/webapps/thisapp/path" depending on the situation.
To make the forward and redirect cased behave similarly, I propose the
following change to ActionServlet.processActionPerform(...) -- commented
with C-style preprocessor directives:
// Obey any returned ActionForward
if (forward != null) {
String path = forward.getPath();
#ifdef OLD_CODE
if (forward.getRedirect())
response.sendRedirect(path);
#else
if (forward.getRedirect()) {
if (path.startsWith("/"))
path = request.getContextPath() + path;
response.sendRedirect(path);
}
#endif
else {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
}
What do you think?
Eirik
++++++++++
Eirik Lygre
eirik.lygre@evita.no
e-vita as, Stortorvet 3, Oslo
Mobil: (+47) 905 66476
Fax: (+47) 23 35 70 51
----- Forwarded by Eirik Lygre/evita/no on 20.09.2000 21:25 -----
Mike Papper <mp...@geocities.com>
14.09.2000 09:38
Please respond to struts-user
To: "'struts-user@jakarta.apache.org'"
<st...@jakarta.apache.org>
cc:
Subject: making "forward mappings" redirectable
Struts question #1:
We have something like the following in action.xml
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
what I really want is
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/ redirect="true">
</action>
I have looked high and low throughoutthe struts code for how to add code
to understand this and cannot find it. The closest I am getting is the
digester "magic" code that does who knows what...
anyone know if this can be done? Or how I could edit the ocde myself to
do it?
Also, if I could do such a thing then I would want the class that
"interprets" the path for a redirect to be SMARt enough to prepend the
path with the context path (but only if necessary) so that it works
correctly. (I.e., if the URL starts with / then it must be prepended with
the request.getContextPath() but if it doesnt start with a / then its a
relative url and will be interpreted relative to the current page and the
current page already has the context's path in front of it). I would be
happy to explain this in more detail.
Speaking of which, is the following from ActionServlet's
processActionInstance method a bug?
...
ActionForward forward =
actionInstance.perform(this, mapping, formInstance,
request, response);
if (forward != null) {
String path = forward.getPath();
if (forward.getRedirect()) {
response.sendRedirect(path);
shouldnt the last line be:
response.sendRedirect(response.encodeRedirectURL(path));
--
Mike Papper, E-mail: mpapp@geocities.com
Hardhat Wholesale, 180 Borica St., San Francisco, CA 94127
Tel: 510-568-7770 ext. 17, Fax: (at work) 510-568-7750
Re: Bug in struts -- making "forward mappings" redirectable
Posted by Mike Papper <mp...@geocities.com>.
I discovered this same problem and created a method that wraps around the
forward method it examoines the paths and makes sure its "ok" for
forwarding. Heres some sample code (I put into my version of ActionBase):
public ActionForward findForward(HttpServletRequest request,
ActionMapping mapping, String name) {
ActionForward method = mapping.findForward("method");
ActionForward forward = mapping.findForward(name);
String path = forward.getPath();
// if were redirecting, we must use the full path (a forward will
always be relative to our context root)
// if were forwarding, it calculates the path relative to our context
// (even if the path begins with a /)
// if were redirecting and the path begins with a / then we must put
the context (i.e., /hhw)
// into the path ourselves. If we redirect to a path that doesnt begin
with a /
// then we dont know if this path is relative to the current page or
implies "use the context root"
// from the ActionForward class:
// Context-relative URI to which control should
// be forwarded, or an absolute or relative URI to which control
should
// be redirected
// NOTE: to do a forward the path MUST be context relative which means
it MUST start with a /
// because the request dispatcher is used
// however for a forward, the response is used and it converts a
relative path to an absolute one
// (but does not take into account the context).
// If they do start with a / they are not relative to the context and
we have to sub-in the context
if (method != null && method.getPath().equals("redirect")) {
forward.setRedirect(true);
// if path starts with a / then prepend the context path (unless it
happens to also start with the context
// path (as that was already there)
if (path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
}
else if (!forward.getRedirect() && !path.startsWith("/")) { // if path
doesnt start with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
public ActionForward createForward(HttpServletRequest request,
ActionMapping mapping, String path) {
boolean redirect = false;
if (mapping != null) {
ActionForward method = mapping.findForward("method");
redirect = method != null && method.getPath().equals("redirect");
}
ActionForward forward = new ActionForward(path, redirect);
if (redirect && path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
else if (!redirect && !path.startsWith("/")) { // if path doesnt start
with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
Original Message dated 9/25/2000, 4:59:02 AM
Author: eirik.lygre@evita.no
Re: Bug in struts -- making "forward mappings" redirectable:
I've been using the "forward"-feature of the action.xml-file, to redirect
the user to another page. For example, in the login-action (which is
/login.do), I want to redirect the user to the main menu. Since I don't
want the URL to be "/login.do" for the main menu, but rather
"/mainmenu.jsp", I used the redirect-attribute:
<forward name="success" path="/mainmenu.jsp" redirect="true" />
Now, this does not work. Struts (and servlets, it seems) does not
acknowledge the presense of web applications while forwarding. This makes
the coding of actions.xml really weird, with references to "/path" and
"/webapps/thisapp/path" depending on the situation.
To make the forward and redirect cased behave similarly, I propose the
following change to ActionServlet.processActionPerform(...) -- commented
with C-style preprocessor directives:
// Obey any returned ActionForward
if (forward != null) {
String path = forward.getPath();
#ifdef OLD_CODE
if (forward.getRedirect())
response.sendRedirect(path);
#else
if (forward.getRedirect()) {
if (path.startsWith("/"))
path = request.getContextPath() + path;
response.sendRedirect(path);
}
#endif
else {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
}
What do you think?
Eirik
++++++++++
Eirik Lygre
eirik.lygre@evita.no
e-vita as, Stortorvet 3, Oslo
Mobil: (+47) 905 66476
Fax: (+47) 23 35 70 51
----- Forwarded by Eirik Lygre/evita/no on 20.09.2000 21:25 -----
Mike Papper <mp...@geocities.com>
14.09.2000 09:38
Please respond to struts-user
To: "'struts-user@jakarta.apache.org'"
<st...@jakarta.apache.org>
cc:
Subject: making "forward mappings" redirectable
Struts question #1:
We have something like the following in action.xml
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
what I really want is
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/ redirect="true">
</action>
I have looked high and low throughoutthe struts code for how to add code
to understand this and cannot find it. The closest I am getting is the
digester "magic" code that does who knows what...
anyone know if this can be done? Or how I could edit the ocde myself to
do it?
Also, if I could do such a thing then I would want the class that
"interprets" the path for a redirect to be SMARt enough to prepend the
path with the context path (but only if necessary) so that it works
correctly. (I.e., if the URL starts with / then it must be prepended with
the request.getContextPath() but if it doesnt start with a / then its a
relative url and will be interpreted relative to the current page and the
current page already has the context's path in front of it). I would be
happy to explain this in more detail.
Speaking of which, is the following from ActionServlet's
processActionInstance method a bug?
...
ActionForward forward =
actionInstance.perform(this, mapping, formInstance,
request, response);
if (forward != null) {
String path = forward.getPath();
if (forward.getRedirect()) {
response.sendRedirect(path);
shouldnt the last line be:
response.sendRedirect(response.encodeRedirectURL(path));
--
Mike Papper, E-mail: mpapp@geocities.com
Hardhat Wholesale, 180 Borica St., San Francisco, CA 94127
Tel: 510-568-7770 ext. 17, Fax: (at work) 510-568-7750
Re: Bug in struts -- making "forward mappings" redirectable
Posted by Mike Papper <mp...@geocities.com>.
I discovered this same problem and created a method that wraps around the
forward method it examoines the paths and makes sure its "ok" for
forwarding. Heres some sample code (I put into my version of ActionBase):
public ActionForward findForward(HttpServletRequest request,
ActionMapping mapping, String name) {
ActionForward method = mapping.findForward("method");
ActionForward forward = mapping.findForward(name);
String path = forward.getPath();
// if were redirecting, we must use the full path (a forward will
always be relative to our context root)
// if were forwarding, it calculates the path relative to our context
// (even if the path begins with a /)
// if were redirecting and the path begins with a / then we must put
the context (i.e., /hhw)
// into the path ourselves. If we redirect to a path that doesnt begin
with a /
// then we dont know if this path is relative to the current page or
implies "use the context root"
// from the ActionForward class:
// Context-relative URI to which control should
// be forwarded, or an absolute or relative URI to which control
should
// be redirected
// NOTE: to do a forward the path MUST be context relative which means
it MUST start with a /
// because the request dispatcher is used
// however for a forward, the response is used and it converts a
relative path to an absolute one
// (but does not take into account the context).
// If they do start with a / they are not relative to the context and
we have to sub-in the context
if (method != null && method.getPath().equals("redirect")) {
forward.setRedirect(true);
// if path starts with a / then prepend the context path (unless it
happens to also start with the context
// path (as that was already there)
if (path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
}
else if (!forward.getRedirect() && !path.startsWith("/")) { // if path
doesnt start with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
public ActionForward createForward(HttpServletRequest request,
ActionMapping mapping, String path) {
boolean redirect = false;
if (mapping != null) {
ActionForward method = mapping.findForward("method");
redirect = method != null && method.getPath().equals("redirect");
}
ActionForward forward = new ActionForward(path, redirect);
if (redirect && path.startsWith("/") &&
!path.startsWith(request.getContextPath())) {
path = request.getContextPath() + path;
forward.setPath(path);
}
else if (!redirect && !path.startsWith("/")) { // if path doesnt start
with a / make sure it does (for a forward action)
forward.setPath("/" + path);
}
return forward;
}
Original Message dated 9/25/2000, 4:59:02 AM
Author: eirik.lygre@evita.no
Re: Bug in struts -- making "forward mappings" redirectable:
I've been using the "forward"-feature of the action.xml-file, to redirect
the user to another page. For example, in the login-action (which is
/login.do), I want to redirect the user to the main menu. Since I don't
want the URL to be "/login.do" for the main menu, but rather
"/mainmenu.jsp", I used the redirect-attribute:
<forward name="success" path="/mainmenu.jsp" redirect="true" />
Now, this does not work. Struts (and servlets, it seems) does not
acknowledge the presense of web applications while forwarding. This makes
the coding of actions.xml really weird, with references to "/path" and
"/webapps/thisapp/path" depending on the situation.
To make the forward and redirect cased behave similarly, I propose the
following change to ActionServlet.processActionPerform(...) -- commented
with C-style preprocessor directives:
// Obey any returned ActionForward
if (forward != null) {
String path = forward.getPath();
#ifdef OLD_CODE
if (forward.getRedirect())
response.sendRedirect(path);
#else
if (forward.getRedirect()) {
if (path.startsWith("/"))
path = request.getContextPath() + path;
response.sendRedirect(path);
}
#endif
else {
RequestDispatcher rd =
getServletContext().getRequestDispatcher(path);
rd.forward(request, response);
}
}
What do you think?
Eirik
++++++++++
Eirik Lygre
eirik.lygre@evita.no
e-vita as, Stortorvet 3, Oslo
Mobil: (+47) 905 66476
Fax: (+47) 23 35 70 51
----- Forwarded by Eirik Lygre/evita/no on 20.09.2000 21:25 -----
Mike Papper <mp...@geocities.com>
14.09.2000 09:38
Please respond to struts-user
To: "'struts-user@jakarta.apache.org'"
<st...@jakarta.apache.org>
cc:
Subject: making "forward mappings" redirectable
Struts question #1:
We have something like the following in action.xml
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
what I really want is
<action path="/logoff"
actionClass="hhw.auth.LogoffAction">
<forward name="success" path="/index.jsp"/ redirect="true">
</action>
I have looked high and low throughoutthe struts code for how to add code
to understand this and cannot find it. The closest I am getting is the
digester "magic" code that does who knows what...
anyone know if this can be done? Or how I could edit the ocde myself to
do it?
Also, if I could do such a thing then I would want the class that
"interprets" the path for a redirect to be SMARt enough to prepend the
path with the context path (but only if necessary) so that it works
correctly. (I.e., if the URL starts with / then it must be prepended with
the request.getContextPath() but if it doesnt start with a / then its a
relative url and will be interpreted relative to the current page and the
current page already has the context's path in front of it). I would be
happy to explain this in more detail.
Speaking of which, is the following from ActionServlet's
processActionInstance method a bug?
...
ActionForward forward =
actionInstance.perform(this, mapping, formInstance,
request, response);
if (forward != null) {
String path = forward.getPath();
if (forward.getRedirect()) {
response.sendRedirect(path);
shouldnt the last line be:
response.sendRedirect(response.encodeRedirectURL(path));
--
Mike Papper, E-mail: mpapp@geocities.com
Hardhat Wholesale, 180 Borica St., San Francisco, CA 94127
Tel: 510-568-7770 ext. 17, Fax: (at work) 510-568-7750