Spring MVC i18n Internationalization tutorial with example



This tutorial shows you how to give language support to application using Spring MVC

Code

Dispatcher-servlet.xml

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

  
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>
    
 
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>
 
 
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
        <property name="interceptors">
            <list>
                <ref bean="localeChangeInterceptor" />
            </list>
        </property>
    </bean>
    
 
    <!-- Register the bean -->
    <bean class="com.candidjava.springmvc.controller.WelcomeController" />
    
 
    <!-- Register the welcome.properties -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="welcome" />
    </bean>

<!-- ************************************************************* --> 

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

<!-- ************************************************************* -->
	


<!-- ************************************************************* -->
	
</beans>


Properties

English

welcome.springmvc = Happy learning Spring MVC

Chinese

welcome.springmvc = \u5feb\u4e50\u5b66\u4e60 Spring MVC


View page

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<html>
<body>
<h1>CandidJava Spring MVC Internationalization Example</h1>
Language : <a href="?language=en">English</a>| <a href="?language=zh_CN">Chinese</a>
<h3> welcome.springmvc :
  <spring:message code="welcome.springmvc" text="default text" />
</h3>
Current Locale : ${pageContext.response.locale}
</body>
</html>

Screenshot

             

             

            


Download

            Spring MVC File upload maven zip

            Spring MVC File Upload war




Comments


©candidjava.com