File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes Spring MVC -- where does the model code go? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring MVC -- where does the model code go?" Watch "Spring MVC -- where does the model code go?" New topic
Author

Spring MVC -- where does the model code go?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
In Struts 1.x, you generally instantiate a model class from you action class for business-specific logic.

I'm trying to understand where in the Spring MVC architecture you basically do the same thing?

Is it in the ModelAndView method or where exactly?

Thanks in advance.

- M
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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: 1034
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
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1034
So you return a model class you've instantiated to the view and Spring takes care of posting the values on the JSP?

I've been looking for a good Spring book, but haven't found one yet.

Any suggestions like the perl above about AbstractController and SimpleFormController and the differences in how they're implemented would be great.

Thanks.

-- M
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

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: 1034
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!



Thanks again.

- M
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Nathan's is a great post.

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: 1034
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring MVC -- where does the model code go?
 
Similar Threads
making a Spring MVC website
What is model
Spring MVC controller Form
Spring In Action 3 Book Question
Relationship between Model (in MVC) and DAO