Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can same command class be used by 2 different controllers?

 
jay desi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
But I don't quite understand what you want to do.
 
jay desi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, new instance will be created.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic