Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts Controller to Controller

 
Suresh Kumar Ramasamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constraint:

Design one action servlet for each jsp - All actions emanating from a JSP should be handled by the corresponding servlet.

Scenario:

Assume that one.jsp on click of a button has to go to two.jsp. This two.jsp needs some values (say for populating a combo) before loading.

Possible Solution without considering the Constraint:

Invoke two.jsp from one.jsp (View to View). Call a function in the header of two.jsp which will submit the form to two.java for retrieval of the values (Hope this will work).

What I have done:

one.jsp submits the form to one.java.

one.java has the following code:
return new two().execute(mapping,form,request,response);

two.java has nothing special. After retrieving the required values, the execute method issues mapping.findForward(forwardName);

The struts config file has an entry for one.java for the forward name set by two.java to navigate to two.jsp.

Question:

Is this a standard way? If not, what are the better ways?

Thanks & Regards.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suresh Kumar Ramasamy:

Is this a standard way?

No, it's not. There are several problems with it. The main one is that the execute method in two.java is expecting the mapping and form objects to be those defined for the two action. Instead, in this case they are the mapping and form for the one action.

Originally posted by Suresh Kumar Ramasamy:
If not, what are the better ways?

Yes, there are better ways. Here are two possibilities:

Method 1: Refactor the code that prepares for two into a method in a Controller Helper or Controller Utility class. Example: ControllerUtils.prepareTwo(). You can then call this method from either the one or two actions.

Method 2: Create both a preparatory and an execution action for each page. Then create a forward for the page one execution action that forwards to the preparatory action for page two. Example:

[ March 18, 2008: Message edited by: Merrill Higginson ]
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generally use "Method 2".

- Brent
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic