File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes How to do Action mapping for this scenario ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to do Action mapping for this scenario ??" Watch "How to do Action mapping for this scenario ??" New topic

How to do Action mapping for this scenario ??

steve francisco
Ranch Hand

Joined: Jan 26, 2005
Posts: 46
Prior to learning struts, I wrote a servlet/jsp/action application and it works like this ---

In my central servlet "ControlServlet", it takes "action" as a parameter and the servlet code includes ---

if(action.equsls("act1")) { initiate and call Action1 class; forward to jsp1; }
if(action.equals("act2")) { initiate and call Action2 class; forward to jsp2; }

In jsp1 and jsp2, both of them include html FORM. when "submit" is clicked, it calls "ControlServlet" again, they also carry the "action" parameter as a hidden variable so that "ControlServlet" knows what's the next page it should dispatch to. SO, the overall flow is like

Browser --> /ControlServlet?action=act1 , then it calls "Action1" class and directs to jsp1; in jsp1 after "submit" is clicked --> /ControlServlet?action=act2, then it calls "Action2" class and directs to jsp2...

How to change it into a struts ? the biggest issue is "how to do the action and action mapping " ? Please help me check if the following makes sense ---

<action path="/ControlServlet?action=act1"
(I do NOT put "name=" here because in the First time the servlet gets no input bean !)
<forward name="ok" path="/jsp1" /></action>

<action path="/ControlServlet?action=act2"
name="jsp1BeanForm" (this is the content of jsp1 input page)
<forward name="ok" path="/jsp2" /></action>

alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
ok, first of all, struts itself is a single controller servlet. what you are looking to do is to define 2 separate Action classes (a.k.a request processor) you can use also DispatchedAction or MappedDispatchedAction to combine into 1 class), each with its own action-mapping and forward. they are essentially identical. The first action-mapping calls
<p />

<action path="action1" type="">
<forward name="showJsp1" path="jsp1.jsp"/>

<action path="action2" type="">
<forward name="showJsp1" path="jsp1.jsp"/>

-/a<br />certified slacker...yes, my last name is 'do' - <a href="" target="_blank" rel="nofollow"></a>
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
stupid form!!! i hit submit by mistake while copy and paste...the second action should be:
<br />
<action path="action2" type="">
<forward name="showJsp2" path="jsp2.jsp"/>

the meat of what you are doing now in your controller should be done inside the execute() of each action. upon success, you must set your forward value to "showJsp1" or "showJsp2".

you can also use 1 action and pass parameter in (in the form of the first jsp). then user request.getParameter() to do conditional statement. what you are asking is very basic of struts.
I agree. Here's the link:
subject: How to do Action mapping for this scenario ??
It's not a secret anymore!