Home Spring Spring3.2 Customizing the MVC Java config or XML Namespace



Customizing the MVC Java config or XML Namespace
Posted on: February 1, 2013 at 12:00 AM
In this section, you will learn about customizing the MVC Java config or the MVC XML Namespace.

Customizing the MVC Java config or XML Namespace

In this section, you will learn about customizing the MVC Java config or the MVC XML Namespace.

For configuring Spring MVC, there are two ways : MVC Java config and the MVC XML namespace. These two ways provide the same type of configuration that overrides the DispatcherServlet defaults. You can choose either MVC Java config or MVC XML namespace according to your preference.

You enable the MVC Java config, as shown below :

@Configuration
@EnableWebMvc
public class MyWebAppConfig {

}

For customizing the default MVC Java config, you need to implement the WebMvcConfigurer interface or you can extend the WebMvcConfigurerAdapter class directly and overrid the method for customization. You can do this as shown below :

@Configuration
@EnableWebMvc
public class MyWebAppConfig extends WebMvcConfigurerAdapter {
	
	@Override
	protected void addFormatters(FormatterRegistry registry) {
	// formatters / converters
	}
	
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	// HttpMessageConverters configuration
	}

}

Default configuration for MVC XML Namespace :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<mvc:annotation-driven />

</beans>

You can customize the above default configuration of <mvc:annotation-driven /> as shown below :

<mvc:annotation-driven conversion-service="conversionService">
	<mvc:message-converters>
		<bean class="net.roseindia.MyAppHttpMessageConverter"/>
		<bean class="net.roseindia.MyAppOtherHttpMessageConverter"/>
	</mvc:message-converters>
</mvc:annotation-driven>

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	<property name="formatters">
		<list>
			<bean class="net.roseindia.MyAppFormatter"/>
			<bean class="net.roseindia.MyAppOtherFormatter"/>
		</list>
	</property>
</bean>

For complete list of attributes and sub-elements supported by <mvc:annotation-driven /> , you can view the Spring MVC XML schema. Click here to view Spring MVC XML schema.



Related Tags for Customizing the MVC Java config or XML Namespace:


More Tutorials from this section

Ask Questions?    Discuss: Customizing the MVC Java config or XML Namespace  

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.