File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes mapping.findForward doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "mapping.findForward doubt" Watch "mapping.findForward doubt" New topic
Author

mapping.findForward doubt

Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
Here is my problem:

if(condition1){
do something
mapping.findForward("success1");

}
if(condition2){
do something
mapping.findForward("success2");

}
if(condition3){
do something
mapping.findForward("success3");

}
If all the 3 conditions hold good at the same time, then which mapping will be considered and then what about the other mappings?


- Christian
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
I wish you thought about this before posting the query. The code snippet is too abstract. Are you not going to store findForward() call to a ActionForward variable? What are you going to do with the selected action forward?
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
Actually I will be forwarding success1 and success2 to the same Action class which contains the above code. Only success3 will be forwarded to some other Action class.
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425


will not automatically forward the page. You will be returning forward which will be acted on by RequestProcessor. It will help better if you paste the workable code.
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
success1 and success2 are mapped in the action mapping present in struts-config.xml to the same action class containing the code.
Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
Ok here is the code:

<action-mappings>
<action path="/MyAction" type="com.MyAction" validate="false">
<forward name="success1" path="/MyAction.do"></forward>
<forward name="failure" path="/jsp/error.jsp"></forward>
<forward name="success2" path="/MyAction.do"></forward>
<forward name="success3" path="/SomeAction.do"></forward>
</action>
<action path="/SomeAction" type="com.SomeAction" validate="false">
<forward name="failure" path="/jsp/error.jsp"></forward>
</action>

</action-mappings>


public class MyAction extends Action {

public ActionForward perform(ActionMapping mapping,ActionForm form,
HttpServletRequest request,
HttpServletResponse response)throws IOException, ServletException {

boolean condition1=true,condition2=true,condition3=true;

try {
if(condition1){
System.out.println("condition1 is:"+condition1);
mapping.findForward("success1");
}
if(condition2){
System.out.println("condition2 is:"+condition2);
mapping.findForward("success2");
}
if(condition3){
System.out.println("condition3 is:"+condition3);
mapping.findForward("success3");
}
}catch (Exception ex) {
return mapping.findForward("failure");
}
}

}
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, if this is the entire method, the code would not compile because there is no unconditional return of an ActionForward object.

Even if there is a return of an ActionForward object elsewhere in the code, the code you have shown us would have no affect at all on the outcome, because it neither returns a value nor sets a variable to be returned. The catch block is the only code that returns a value.

Had you written:

if(condition1){
System.out.println("condition1 is:"+condition1);
return mapping.findForward("success1");
}
if(condition2){
System.out.println("condition2 is:"+condition2);
return mapping.findForward("success2");
}
if(condition3){
System.out.println("condition3 is:"+condition3);
return mapping.findForward("success3");
}

Then Struts would forward to "success1" if condition1 is true regardless of whether condition2 and condition3 are true or not. This happens simply because it's the first block of code to be evaluated, and if it's true, a return occurs and the rest of the code is not executed.

P.S. If you're using Struts version 1.1 or above, the name of your method should be execute, not perform.
[ June 21, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169
try writing in if .. else if .. structure
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: mapping.findForward doubt