Autogenerated logical view name through RequestToViewNameTranslator

In this section, you will learn about handling the situation where view name is not provided, through RequestToViewNameTranslator interface.

Ads

Tutorials   
Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration Interceptors Configuration using Java or XML Customizing the MVC Java config or XML Namespace MVC Java Config or the MVC XML Namespace Servlet container initialization through code Support for ETag The checkbox tag Autogenerated logical view name through RequestToViewNameTranslator Modification in conventional ModelAndView ControllerClassNameHandlerMapping class for handling convention mapping The password tag The input tag The form tag Form Tag library configuration Customizing the Default Error Page @ExceptionHandler & @ResponseStatus annotation Multipart support for file upload in Spring MVC Themes and Theme resolvers in Spring MVC Locales in Spring MVC ContentNegotiatingViewResolver Redirecting and forwarding to views Chaining of Multiple view resolvers View Resolving through ViewResolver interface Requests Intercepting through a HandlerInterceptor Defining handler methods using @RequestMapping annotation Spring 3.2 Asynchronous Request Processing @RequestMapping annotation for mapping requests @Controller annotation for defining new controller
Ads

Autogenerated logical view name through RequestToViewNameTranslator

In this section, you will learn about handling the situation where view name is not provided, through RequestToViewNameTranslator interface.

Where no view name is provided, the RequestToViewNameTranslator interface determines the view name automatically. For this you need to configure DefaultRequestToViewNameTranslator class in Spring MVC configuration file.

To understand it completely, lets take an example :

Consider the below controller code :

public class WritingController implements Controller {
	
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
		// request processing 
		ModelAndView model= new ModelAndView(); // note that no view name is provided
		// addition of data as to the model
		return model;
	}
}

Note that no view name is provided in the above code. For auto generation of the view name, you need to configure the DefaultRequestToViewNameTranslator class in Spring MVC configuration file as shown below :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- this bean with the well known name generates view names for us -->
<bean id="viewNameTranslator"
class="org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator"/>

<bean class="x.y.WritingController">
<!-- inject dependencies as necessary -->
</bean>

<!-- maps request URLs to Controller names -->
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>

</beans>

In the above controller code, you can see that no view name is provided. The DefaultRequestToViewNameTranslator class, configured in conjunction with ControllerClassNameHandlerMapping , is responsible for auto generating view name.

In the above case, if the requested URL is :

http://localhost/writing.html

the resultant generated logical view name would be writing .According to above configuration and returned view name the resultant view name would be reside in " /WEB-INF/jsp/ " as writing.jsp.

Advertisements

Ads
Share on Google+Share on Google+

Autogenerated logical view name through RequestToViewNameTranslator

Posted on: January 30, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Autogenerated logical view name through RequestToViewNameTranslator  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads