本示例基于Spring MVC 4.0.2版本,且使用@RequestMapping注释来映射请求和方法。下列所有文件包括资源文件全部是UTF-8。
spring-mvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:annotation-config />
<!-- auto scan the following packages -->
<context:component-scan base-package="com.tercel.controller" />
<!-- mapping static resource -->
<mvc:annotation-driven />
<mvc:resources mapping="*.html" location="/" />
<mvc:resources mapping="/css/*.*" location="/css/" />
<mvc:resources mapping="/js/*.*" location="/js/" />
<mvc:resources mapping="/fonts/*.*" location="/fonts/" />
<mvc:interceptors>
<ref bean="localeChangeInterceptor" />
</mvc:interceptors>
<!-- implement converting between json and HTTP message -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- start SpringMVC annotation, implement POJO mapping for request and
annotation -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON converter -->
</list>
</property>
</bean>
<!-- view resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- upload file -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- default encoding -->
<property name="defaultEncoding" value="utf-8" />
<!-- max size -->
<property name="maxUploadSize" value="10485760000" />
<!-- min size -->
<property name="maxInMemorySize" value="40960" />
</bean>
<!-- internationalization source. messages used for tips,errors,etc. labels
used for label,title,etc. -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="utf-8" />
<property name="useCodeAsDefaultMessage" value="false" />
<property name="cacheSeconds" value="10" />
<property name="basenames">
<list>
<value>WEB-INF/resource/messages</value>
<value>WEB-INF/resource/labels</value>
</list>
</property>
</bean>
<!-- locale Resolver -->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="zh_CN" />
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
<!-- locale interceptor -->
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</beans>
indexController.java文件内容如下:
package com.tercel.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value="/")
public class indexController {
@RequestMapping("/")
public String index(HttpServletRequest request,HttpServletResponse response,Model model){
return "index";
}
}
index.jsp文件内容如下:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Bootstrap</title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body>
<nav class="navbar navbar-inverse" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" href="#"><spring:message
code="systemname" text="default text" /></a>
</div>
</nav>
Language: <a href="?lang=zh_CN">中文</a> - <a href="?lang=en_US">英语</a>
<div>Current Locale : ${pageContext.response.locale}</div>
<div>Accept-language : ${header["accept-language"]}</div>
<script src="js/jquery-3.2.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
labels_en_US.properties资源文件内容如下:
systemname = fairyland
labels_zh_CN.properties资源文件内容如下:
systemname=\u7F16\u7A0B\u4ED9\u5883
labels.properties资源文件内容如下:
systemname=\u7F16\u7A0B\u4ED9\u5883
文件目录结构如下: