In struts-config I have action like this :
<action
path="/action/basket"
type="com.xxx.BasketAction"
name="basketForm"
scope="session"
unknown="false"
validate="false"
>
<forward
name="displayItems"
path="EshelfItems"
redirect="false"
/>
<forward
name="displayItems-redirect"
path="/action/basket.do?fn=display"
redirect="true"
/>
<forward
name="displayBasket"
path="/basket.jsp"
redirect="false"
/>
</action>
Where I can find the mapping for /action/basket.do?fn=display ?
According to http://struts.apache.org/1.x/faqs/works.html :
In the framework configuration file(s), you associate paths with the
controller components of your application, known as Action classes
(i.e. "login" ==> LoginAction class). This tells the ActionServlet
that for the incoming request 'http://myhost/myapp/login.do' it should
invoke your controller component, LoginAction.
Note the extension .do in this URL. The extension causes your
container (i.e. Tomcat) to call the ActionServlet, which sees the word
"login" as the thing you want to do. The configuration is referenced,
and your LoginAction is executed.
Check your BasketAction class where the fn=display request parameter is checked.
If you can't find the action class for a given mapping then debugging your
RequestProcessor's process method can be also useful.
Ya your question is not clear..
When you hit /action/basket.do?fn=display its going to use the following mapping in your struts config.
<action
path="/action/basket"
And then it will execute
com.xxx.BasketAction
and in that class if you want you can read the param fn=display and then do a forward and that will work as mapped in <forward>...</forward> mapping of your struts config
Related
I have been searching web for hours, and I can't find answer to a simple question in Struts 2. Basically, I have the following action in Struts 1 which is a simple forward, and I want to reproduce the same in Struts 2:
<action path="/az/api/v22/my-tenants" forward="/components/c/apis/v22/my-tenants.jsp">
</action>
I could write an action class to do this, but I think Struts2 has to have some way of doing this without having to write an action class since it is is a simple redirection.
Create actionless result in the struts.xml
struts.xml:
<package name="v22" namespace="/az/api/v22" extends="struts-default">
<action name="my-tenants">
<result>/components/c/apis/v22/my-tenants.jsp</result>
</action>
</package>
This configuration defines a package with namespace /az/api/v22 and action name my-tenants. So if you use path /az/api/v22/my-tenants it will be mapped to the action config above, because default action mapper is using namespace and action name together to get the action config.
There's no class attribute in the action tag, and it uses class ActionSupport instead. This class is configured by default in struts-default package.
In the result it's enough to define the location of the JSP, because struts2 defaults are using a dispatcher that forwards to JSP, and it's using "success" result code by default in the result config which is returned by default by ActionSupport class.
I'm developing a simple virtual store for an university project. I'm using struts 1.3. My problem is that I have this:
<action name="ComprarMaisForm" path="/ComprarMais" scope="session" type="com.myapp.struts.ComprarMaisAction">
<forward name="pagar" path="/pago.jsp"/>
<forward name="eliminar" path="/vistaCarrito.jsp"/>
<forward name="comprarmais" path="/index.jsp"/>
</action>
I want to have several actions instead of one. How can I do?
For different type of actions you have to specify different action mappings with different 'paths'
<action path="/goto1" ...> .. </action>
<action path="/goto2" ...> .. </action>
Anyways If You want to have several actions with same name (form-bean name) scope type and forward mappings (probably silly ques), You need the SAME action, just give same action name wherever you want to use it.
If any 1 parameter is different you need to specify different <action/> mappings.
I have a slight problem with forwarding using Struts.
Now when users accessing my page like this -> http://mypage/
they are automatically forwarded to /index.jsp.
But I'd also like to have index.jsp to be linked to name "sg".
So when they access page like this :
http://mypage/ > they will be forwarded to http://mypage/sg
which is http://mypage/index.jsp.
As I've already mentioned above I'm using Struts to handle all these action. The below example is what I have in my struts.xml file. But it's working rather partially. When I access the page as stated above I'm getting redirected to http://mypage/sg and it also gives me 404 - Not Found.
However when I try manually accessing the url (http://mypage/sg), it works perfectly.
<package name="index" namespace="/" extends="default">
<action name="">
<result>/sg</result>
</action>
<action name="/sg">
<result>/index.jsp</result>
</action>
</package>
When I access the page as stated above I'm getting redirected to http://mypage.com/sg and it also gives me 404 - Not Found.
Answer :
If you want to call another action as result of one action then you need to mention attribute type of result tag
<action name="">
<result type="redirect">/sg</result>
</action>
This will redirect to action sg.
The redirect result type:
The redirect result type calls the standard response.sendRedirect() method, causing the browser to create a new request to the given location.
I'm working on a Struts application.
In order to edit the struts-config.xml file, I think I have to add an attribute - scope, in the action tag. I'm not sure about its meaning, or its usage.
<action path="/WetsVpnSwapTraffic"
type="com.kpn.bop.web.action.vpn.wets.WetsVpnSwapTraffic"
scope="request"
name="WetsVpnSwapTrafficForm"
roles="bop_wetsvpn_migrate"
validate="false">
<forward name="success" path="/WetsVpnSwapTrafficValidate.do"/>
<forward name="failure" path="/WetsVpnList.do"/>
</action>
Can anyone explain me if I have to put this attribute?
The attribute scope is used to define the scope (life of the object, the form) of the object action form that used in that action configuration.
There's also different scopes, page, request, session, application. That's all from servlet specs. If you specify the scope of request that you want the form object is available during servlet http request.
You can check this reference to determine how to use scopes.
There's also link to action mapping configuration.
It determines if the ActionForm is in the request or session.
I'm working on a little project which uses Struts 1.3 and I encountered the following problem.
After some business logic takes place in an Action i want to forward the control to another Action which is mapped in struts-config.xml.
Usually this is the way I'm solving this:
struts-config.xml
<action path="/boardCreate" type="com.example.BoardCreateAction" name="BoardCreateForm" input="/board.jsp">
<forward name="success" path="/board.do" redirect="true" />
</action>
Java action class
return mapping.findForward("success");
This will take make a redirect to the board.do action which is also mapped there.
My problem is that I want to redirect the control to something like:
<forward name="success" path="/board.do?id=1" redirect="true" />
Notice the id=1 parameter. Is this any other way except rebuilding my own action forward for this? I can't find any documentation debating this matter. Thanks!
ActionRedirect redirect = new ActionRedirect(mapping.findForward("success"));
redirect.addParameter("id", theId);
return redirect;
See http://tool.oschina.net/uploads/apidocs/struts-1.3.10/org/apache/struts/action/ActionRedirect.html