I am migrating a Struts application to Struts 2. It was developed by Struts 1.2 four years ago.
My question is:
In my JSP, there is such a statement:
<jsp:forward page="/a.do" />
It works well in Struts 1, but does not work in Struts 2, it tells me HTTP 404 error when I was accessing this JSP file.
However, if I access to http://localhost:8080/shell/a.do
, it works well.
I wonder the reason, is it because the forward action cannot be caught by the Struts 2 filter? (org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
)
So the forward cannot be dispatched to the Struts 2 action?
There are many forwards in my application, if forwards cannot work in Struts 2, does it the only solution to use redirect instead of forward?
Actually this question is based on my analysis, the original job is show welcome page of the site. If I use jsp:forward
forwards to a.do
, it doesn't work.
And I followed Roman's advice, use result
instead of forward. So the question now is how to configure the default action for the entire application?
I tried <default-action-ref name="a"/>
, but it works only for unmapped actions, for example http://localhost:8080/shell2/(some-unmapped-action).do
, it goes to the default action. But it does not work if I do not specify the ".do"
.
Of course this can be done with url-rewrite, but I don't want to use this approach.
JSP forward action tag is used for forwarding a request to the another resource (It can be a JSP, static page such as html or Servlet). Request can be forwarded with or without parameter.
The redirect result type calls the standard response. sendRedirect() method, causing the browser to create a new request to the given location.
To forward a request from one page to another JSP page we can use the <jsp:forward> action. This action has a page attribute where we can specify the target page of the forward action. If we want to pass parameter to another page we can include a <jsp:param> in the forward action.
Include the struts tag library
<%@ taglib prefix="s" uri="/struts-tags" %>
and u can simply write in ur jsp page to forword to some action
<s:action name="YourAction" namespace="/PackageNamespace" executeResult="true" />
The concept of forward
from Struts 1 moved to concept of dispatcher result. You have to remove all forward
s from your application and create result
s instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With