aspose file tools*
The moose likes Spring and the fly likes Can same command class be used by 2 different controllers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Can same command class be used by 2 different controllers?" Watch "Can same command class be used by 2 different controllers?" New topic
Author

Can same command class be used by 2 different controllers?

jay desi
Greenhorn

Joined: Jul 02, 2009
Posts: 27
I have a command object - TestCommandObject and 2 controllers - TestController1 and TestController2




dispatcher-servlet.xml relevant TestController1 contents..



Now, both TestController1 and TestController2 have same input JSP views practically speaking i.e. TestInput1.jsp. This jsp gets its data from
TestCommandObject command object. However subsequent processing of both controllers is entirely different i.e different
logic, different output views and so on.

How should I specify TestController2 in dispatcher-servlet.xml. Should I use

1) 2 different command objects and same jsp view i.e tco1,tco2 and TestInput1? OR
2) same command object but different jsp views i.e tco and TestInput1,TestInput2 OR
3) allow 2 controllers to use same command object i.e tco and same jsp input view i.e TestInput1.OR
4) use 2 diiferent command objects tco1,tco2 and 2 different jsp views TestInput1,TestInput2 for 2 different controllers. Although I think that this option is not a good choice.

Any tips/help would be highly appreciated.
Thanks



Syednizar Kamal
Greenhorn

Joined: Jul 04, 2008
Posts: 15
Yes we can use same command object for different controllers. I'm not able to understand what you are expecting particulary. IMHO better to use TestController1 alone and redirect to different view depending upon your logic.

To redirect to different view other than success view use below code.
modelAndView.setViewName("jsp file path");
[/size]
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Yes.
But I don't quite understand what you want to do.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
jay desi
Greenhorn

Joined: Jul 02, 2009
Posts: 27
I will explain in detail here.

I have 2 completely different reports to output(Hence I prefer 2 different controllers). Both reports have same input page(hence single form backing object). But the business logic and associated beans and output for both reports are completely different.

Hence, I created TestController1 and TestController2 (because of 2 different reports). I have 2 different output views(TestOutput1 and TestOutput2).
As both these controllers are using same input view, I have form backing object corresponding to 1 class i.e entities.TestCommandObject.

Now my question is regarding configuration to be specified in deispatcher-servlet.xml for TestController2.

This is what I have for TestController1:-


What should come in "formView" and "commandName" for TestController2? For TestContoller1 I have "formView"=TestInput1 and
"commandName"=tco. Should I create a different jsp view for TestController2 and name it as "formView"=TestInput2?
Should I have different "commandName" for TestController2 and name it as "commandName"=tco2. Or should I use same commandName but
different jsp form view for other controller or other way round?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Use different JSPs because as I understand UIs are different. You can use same command name or any name it's just a name anyway.
jay desi
Greenhorn

Joined: Jul 02, 2009
Posts: 27
Thanks for the reply KengKaj..
So, if both controllers i.e TestController1 and TestController2 use same command name (tco in this case),
will single instance be shared by 2 controllers or spring mvc creates 2 instances of same class and gives 1
each to 2 controllers
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
You have two different screens but fortunately you have same fields but it is obvious that they will have different values as both screens are different. So IMO it is safe to think that it creates two different instances of that command class.
jay desi
Greenhorn

Joined: Jul 02, 2009
Posts: 27
I should then:-

1) use same value for commandName for both controllers - tco
2) use different names for formviews "TestInput1 and TestInput2" for both controllers

and spring mvc will create 2 different instances of command class and pass it on to their controllers.

Thanks vishal..

continuing on this, I had a query. If it so happens that many people are requesting for "TestController1" at
same time, does spring mvc create different instances of same command class for each request?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Yes, new instance will be created.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can same command class be used by 2 different controllers?