Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Struts Controller to Controller

 
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.
 
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 ]
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generally use "Method 2".

- Brent
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic