PDA

View Full Version : Which controller should I use?


raj_skromis
Dec 26th, 2005, 11:40 PM
Hi,
I am new to Spring. Just wanted to know can I do this with a single controller.
- when my page loads a list of students will get displayed
- each has a Edit link
- page has a add link
- if click on edit form will come with populated values for that student
- if add then same from without any values
- should be able to bind command and validator class with controller
- save should same for both

I could not do this in a single controller, please let me know if can be done.
Thanks.

ganesh pol
Dec 27th, 2005, 01:50 AM
i also face same problem which one to select from the rich set of spring controller


by running pet clinic application i have observed that developers used one controller for one form which is subclass of SimpleFormController

if u wants to work with link then i think multiaction will be better option

raj_skromis
Dec 27th, 2005, 07:46 AM
Hi Ganesh,
Thanks for your reply, so you are saying its not possible to use a single controller for all the functionalities I mentioned?

Oranger
Dec 29th, 2005, 06:38 AM
hello:
you can using the MultiActionController and it has bind(request,commandobj)
method.this method can put page form into ur commandobj.
and it also has a method setValidators(Validator[] validators) to set a validator class for ur commandobj.
you should set the url with the list of student like this:
if you want to add a student.
http://xxxx.xxx/student.do?task=add
if you want to view a student's info.
http://xxxx.xxx/student.do?task=view
and then set the student controller like this:
<bean id="StudentController"
class="StudentController">
<property name="methodNameResolver">
<ref local="StudentMethodNameResolver"/>
</property>
</bean>
and using ParameterMethodNameResolver.
<bean id="StudentMethodNameResolver"
class="org.springframework.web.servlet.mvc.multiaction.Pa rameterMethodNameResolver">
<property name="paramName"><value>task</value></property>
<property name="defaultMethodName"><value>view</value></property>
</bean>

and than you can set some method which the name is same as the parameter
after the task in the class extends MultiActionController.
for example task=add.there should be a method named add.and the dispatcherservlet will call the add method if the parameter is the add after the task.
at last in the method,you can using the bind method and set a validator.

hope this useful for you.

:)

rstearns01
Dec 29th, 2005, 06:17 PM
You *could* do it all with one controller if your needs are very simple, but right off the bat you are basically complicating things by having a list view and form view. Populating two disparate views is not really the job of one controller - I suggest you at least split them into two.

Even adding and editing can change the entire context of a workflow and view - non-editable fields, permission checking on edits, duplicate checking, changing messaging, and similar business rules can overwhelm a single controller and view. If you start with one, be prepared to refactor.

raj_skromis
Jan 1st, 2006, 07:42 PM
Hi everyone,
Thanks to all of you for very useful information.

Oranger - is bind() method called by the framework, for me I did not get it
called by framework and I called it from each method. Is this correct?

Oranger
Jan 1st, 2006, 09:19 PM
yes.in SimpleFormController the bind() method called by the framework itself.

and here you should call it from each method or you can set a methodbeforeinterceptor.before the controller is called , bind the request to the commandobj.

:)