• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring MVC -- where does the model code go?

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic