• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mapping.findForward doubt

 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Srilakshmi Vara
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try writing in if .. else if .. structure
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic