Internationalization 的縮寫。html
MessageSource
,用於加載資源文件MessageResolver
,用於解析用戶所處的位置(Locale)<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:messages" /> <property name="defaultEncoding" value="GBK" /> </bean>
xxx_zh.properties文件代碼:java
title=這是一個測試 user.id=用戶編號 user.name=用戶姓名 user.sal=用戶工資 page.cl=點擊切換語言: global.greeting={0}近來可好?
package com.xiaobai.controller; import com.xiaobai.entity.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ThemeResolver; import javax.imageio.ImageIO; import javax.mail.Multipart; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.text.DateFormat; import java.util.Date; import java.util.Locale; @Controller @RequestMapping("/book") public class BookController { @RequestMapping(method = RequestMethod.GET) public String home(Book book) { return "book_input"; } @RequestMapping(method = RequestMethod.POST) public String add(@Valid Book book, BindingResult result) { if (result.hasErrors()) return "book_input"; System.out.println("正常業務。。。。"); return "book_home"; } @Autowired private LocaleResolver localeResolver; @GetMapping("/cl/{loc}") public String changeLocale(@PathVariable("loc") String localeStr, HttpServletRequest req, HttpServletResponse resp) { Locale locale = new Locale(localeStr); localeResolver.setLocale(req, resp, locale); return "redirect: /book"; } }
以後咱們就能夠直接在JSP文件上使用了web
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <style> .error { color: red; } </style> <link href="<spring:theme code="main.style"/>"/> </head> <body style="color: <spring:theme code="main.color" />; background: <spring:theme code="main.bgcolor" />;"> <div> <spring:message code="global.greeting" arguments="兄嘚" /> <br> <a href="/book/cl/zh">中文</a> <a href="/book/cl/en">English.</a> <a href="/book/cl/jp">小日本</a> <br><br> </div> <div> <br> <a href="/book/ct/boy">boy</a> <a href="/book/ct/girl">girl</a> <a href="/book/ct/default">默認</a> <br><br> </div> <form:form action="/book" method="post" modelAttribute="book"> <div> <form:input path="name" placeholder="name"/> <form:errors path="name"/> </div> <div> <form:input path="price" placeholder="price"/> <form:errors path="price"/> </div> <input type="submit"> </form:form> </body> </html>
這樣咱們就完成了點擊切換不一樣的語言。spring
效果截圖:app