Viewing Options in Spring
by, 11-30-2011 at 04:38 AM (1620 Views)
In your application, once the service have handled the request and returned the results desired, you will need to render the results in a useful manner to the user. This is the purpose of viewing technology such as JavaServer Pages (JSP). Although you can use JSPs in Spring there are a number of other viewing technologies that Spring support. In this tip, I will outline some of the other view technologies that are available.
When Spring is looking to render some content, the DispatcherServlet will a consult a view resolver in order to determine which view should handle a particular request. The view resolver will map a logical view name to an implementation of one of the viewing technologies that implement, org.springframework.web.servlet.View. Many of the key view resolver implementation are shown in the table below:
The DispatcherServlet queries InternalResourceViewResolver to resolve the views. Iím using JSP technology in this example, so I have placed my JSPs in the /WEB-INF/views directory. I configure the view resolver in my springexamples.xml file in the following manner:
The InternalResourceViewResolver will take the logical view name, prefix it with /WEB-INF/views/ and suffix it with .jsp. As well I have used the JstlView rather than use the default view object, InternalResourceView, in order to take advantage and expose JSTL-specific request attributes. This results in the path of the JSP (or other view) that will render the output. It also hands the path to the View object that dispatches the request to the JSP. For example, when the HomeController returns home as the logical view name, it resolves to /WEB-INF/views/home.jsp. It will resolve in a similar manner.Java Code:<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean>