0

I am very new to Spring MVC framework but I have worked with Struts for some time. In Struts each view can be bound with an "ActionForm" object and view can be generated extracting this object.Now I am moving to Spring MVC and have doubt about generating the View from controllers.
Currently I know the following two methods to build View from controller

  1. Return "ModelAndView" object from controller for each request and extract the data in respective JSP (View).

  2. Return JSON and extract it within JSP (View).

Is this the normal way of generating View in Spring MVC?

I found this question which describes best practices when communicating between controller and view. I want to be know, it's done in Spring MVC.

Community
  • 1
  • 1
  • The model, i.e. the Java object(s) stored in ModelAndView, is indeed what the view is supposed to display. No need to transform anything to JSON. – JB Nizet May 10 '16 at 08:54

3 Answers3

0

I am years working with Spring and I think the best way to work with controllers and views is using the ModelAndView object.

Here i leave you a link from the Spring documentation about Web applications:

http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/mvc.html

cralfaro
  • 5,822
  • 3
  • 20
  • 30
  • How to decouple the view from the model? – Roman C Jun 11 '16 at 08:42
  • @RomanC option could be return always JSON format data, and in the view you can render it as you wish or xml over xslt, is this what you mean? – cralfaro Jun 20 '16 at 08:36
  • JSON data if it not belong to the model then can't be rendered in the View. It's different architecture, not MVC where Model an View tied together. – Roman C Jun 20 '16 at 09:56
  • Then i guess model and view need to be always in some way coupled, view need to know the format/structure of received data – cralfaro Jun 20 '16 at 10:06
  • If they are coupled then there's dependency between them, format of data doesn't matter in this case. – Roman C Jun 20 '16 at 10:28
0
  1. One way is to create a ModelAndView from a Controller which will then forward to a JSP.

  2. Another way is to use mvc:view-controller tag in spring xml file

    By using the second option you can directly attach a request to a JSP page without going to the Controller.

shankarsh15
  • 1,947
  • 1
  • 11
  • 16
0
  • Best way to use user ModelAndView and object associated within it if you are using Web Application. Also it is good the returning object having name value pair properties i.e. POJO
  • If you are using Web Service and exposing web service other than web e.g. Mobile Device then you definitely go with JSON.
Shivkalyan
  • 39
  • 9