If you're just using an AbstractController it's much more like the Struts way:
However, you'll probably be using AbstractCommandController or SimpleFormController - these are more "Spring-ified" - in your configuration you can inject the commandClass and commandName and the controller will handle creating the POJO model class, and using the supplied name to bind it to the session. It will also handle binding request parameters to fields of the model. In the simplest case you'd just need to override the doSubmitAction() method:
To map the above controller in the configuration you'll need something like the following:
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 948
posted
0
Good reply, a couple questions...
In your first Struts-like example below, would the model code (business logic) be in the handleRequestInternal() method before you return mav; ?
And, I'm assuming in the second example, based on your comment, the business logic would be where you wrote "do stuff here", right?
And if you use say Annotations instead of extending a Spring specific class, then your method can either return a ModelAndView object, where you place your model objects in that object, or the method can just return a model object of any type. So if you return a MyCustomObjectICreated in a method in an @Controller class, then the MyCustomObjectICreated is the returned model object.
Mike London wrote:Good reply, a couple questions...
In your first Struts-like example below, would the model code (business logic) be in the handleRequestInternal() method before you return mav; ?
And, I'm assuming in the second example, based on your comment, the business logic would be where you wrote "do stuff here", right?
Thanks very much in advance.
- M
Correct on both counts - however, the preferred way of handling all this would be to write a service interface that handles the "business process" parts of your program, and invoke the service interface at these locations - not put this logic directly in the controller.
Usually Spring web apps use 3 layers -
1.) DAO (Data Access Object) layer - handles database interaction to store/load/find model objects.
2.) Service layer - handles business processes using model objects.
3.) Web layer - handles rendering web pages and translating request parameters into model object values.
You'd want to create a service layer interface/class to do what you need - something like below:
As for books - I used Pro Spring and Expert Spring MVC and WebFlow from Apress - both of these were for older versions of Spring, but I've heard there are new editions out. There's also a lot of tutorials and guides on springframework.com, and there are some examples packaged in the Spring distribution if you download the full version.
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 948
posted
0
This is really a great reply.
As for the books you mentioned, the first one gets good reviews, but, IMHO, doesn't explain things as clearly as you just did!
But also note that is for Spring 2.0 whereas in Spring 2.5 and beyond, it is now the old way to do it. The only difference would be if you had a simple Model object you wanted to return and you didn't want to couple your code to a Spring class, then you would just do
I like this approach because of how clean it looks and is.
Also, I did a review of the latest edition of the Pro Spring book, which I liked very much.
Mark
Mike London
Ranch Hand
Joined: Jul 12, 2002
Posts: 948
posted
0
Mark,
Thanks. Of course, these one-liner-method returns only look good if you understand what's actually happening....
Does the latest edition of the Pro Spring book discuss approaches like the ones described in this thread?
My take on the last edition of the book was that it seemed to assume a lot.
- M
subject: Spring MVC -- where does the model code go?