SpringMVC 国际化 Day19 2018-12-07

SpringMVC 国际化

1. messageSource 接口

1.1 多组属性文件配置

     
     
        
            
                message
                testInfo
            
          
     

1.2 单组属性文件配置

   
     
        
        
     

2. localeResolver语言区域解析器接口

实现类都在org.springframework.web.servlet.i18n包中,包括:

  • AcceptHeaderLocaleResolver,默认,读取浏览器accept-language标题来确定使用语言区域。可以不用哪个显示配置。
  • SessionLocaleResolver
  • CookieLocaleResolver

3. message标签

在jsp中引入Spring标签库:

<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>

  • message标签属性:
属性 描述
arguments 标签参数,可以是一个字符串、数组或
argumentSeparator 用来分隔该标签的字符
code 获取消息的key
htmlEscape booleam值,表示被渲染的值是否应该进行HTML转义
javaScriptEscape booleam值,表示被渲染的值是否应该进行JavaScript转义
message MessageSourceresolvable参数
scope 保存var属性中定义的变量的作用
text 如果code属性不存在,所显示的默认文本
var 用于保存消息的变量

4.AcceptHeaderLocaleResolver国际化

4.1 springmvc配置




 
    
    
        /WEB-INF/content/
    
    
    
        .jsp
    

     
     
        
        
      
      
         
         
      
    
    
     
 

4.2 资源文件

  • message_zh_CN.properties
loginname= \u767B\u5F55\u540D:
password = \u5BC6\u7801:
submit = \u63D0\u4EA4
welcome = \u6B22\u8FCE {0} \u8BBF\u95EE\u4E66\u9038\u81EA\u5982
title = \u767B\u5F55\u9875\u9762
username = \u7BA1\u7406\u5458
  • message_en_US.properties
loginname= Login name:
password = Password:
submit = Submit
welcome = Welcom {0} access zhougl
title = Login Page
username = administrator

4.3 Controller层

@Controller
public class UserController{
    
    private static final Log logger = LogFactory.getLog(UserController.class);
     
    @RequestMapping(value="/{formName}")
     public String loginForm(
             @PathVariable String formName,
             Model model){
        User user = new User();
        model.addAttribute("user",user);
        // 动态跳转页面
        return formName;
    }
     
     @RequestMapping(value="/login",method=RequestMethod.POST)
     public String login(
             @ModelAttribute @Validated User user,
             Model model,
             HttpServletRequest request) {
         logger.info(user);
         // 如果登录名是zhougl,密码是123456,则验证通过
         if(user.getLoginname()!=null && user.getLoginname().equals("zhougl")
             && user.getPassword()!=null && user.getPassword().equals("123456")){
             //从后台代码获取国际化信息username
             RequestContext requestContext = new RequestContext(request);
             String username = requestContext.getMessage("username");
             // 将获取的username信息设置到User对象并设置到Model当中
             user.setUsername(username);
             model.addAttribute("user", user);
             return "success";
         }
         return "error";
        
     }

}

4.4 jsp

4.4.1 loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>




测试基于浏览器请求的国际化



"/>
4.4.2 success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>




测试基于浏览器请求的国际化




测试:在设置浏览器语言即可切换语言

5.SessionLocaleResolver国际化

5.1 Springmvc配置

  • SessionLocaleResolver 语言解析器配置
  
    
        
            
                message
            
          
      
    
      
         
         
      
    
    
     

5.2 Controller层

  • 基于HttpSession
@Controller
public class UserController{
    
    private static final Log logger = LogFactory.getLog(UserController.class);
     
    @RequestMapping(value="/{formName}")
     public String loginForm(
             @PathVariable String formName,
             String request_locale,
             Model model,
             HttpServletRequest request){
        System.out.println("request_locale = " + request_locale);
        if(request_locale != null){
            // 设置中文环境
            if(request_locale.equals("zh_CN")){
                Locale locale = new Locale("zh", "CN"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }
            // 设置英文环境
            else if(request_locale.equals("en_US")){
                Locale locale = new Locale("en", "US"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }
            // 使用之前的语言环境
            else {
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
            }      
        }
        User user = new User();
        model.addAttribute("user",user);
        // 动态跳转页面
        return formName;
    }
     
     @RequestMapping(value="/login",method=RequestMethod.POST)
     public String login(
             @ModelAttribute @Validated User user,
             Model model,
             HttpServletRequest request) {
         logger.info(user);
         if(user.getLoginname()!=null && user.getLoginname().equals("zhougl")
             && user.getPassword()!=null && user.getPassword().equals("123456")){
             //从后台代码获取国际化信息
             RequestContext requestContext = new RequestContext(request);
             String username = requestContext.getMessage("username");
             user.setUsername(username);
             model.addAttribute("user", user);
             return "success";
         }
         return "error";
        
     }

}

5.3 loginForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix= "form" uri= "http://www.springframework.org/tags/form" %>
<%@taglib prefix= "spring" uri= "http://www.springframework.org/tags" %>




测试基于SessionLocaleResolver的国际化



中文 | 英文

"/>

其余文件与 4小节相同。

5.CookieLocaleResolver国际化

5.1 Springmvc配置

  • CookieLocaleResolver 语言解析器配置
  
         
         
      
     

5.2 Controller层

  • 基于Cookie
@Controller
public class UserController{
    
    private static final Log logger = LogFactory.getLog(UserController.class);
     
    @RequestMapping(value="/{formName}")
     public String loginForm(
             @PathVariable String formName,
             String request_locale,
             Model model,
             HttpServletRequest request){
        System.out.println("request_locale = " + request_locale);
        if(request_locale != null){
            // 设置中文环境
            if(request_locale.equals("zh_CN")){
                Locale locale = new Locale("zh", "CN"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }
            // 设置英文环境
            else if(request_locale.equals("en_US")){
                Locale locale = new Locale("en", "US"); 
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,locale); 
            }
            // 使用之前的语言环境
            else {
                request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,LocaleContextHolder.getLocale());
            }      
        }
        User user = new User();
        model.addAttribute("user",user);
        // 动态跳转页面
        return formName;
    }
     
     @RequestMapping(value="/login",method=RequestMethod.POST)
     public String login(
             @ModelAttribute @Validated User user,
             Model model,
             HttpServletRequest request) {
         logger.info(user);
         if(user.getLoginname()!=null && user.getLoginname().equals("zhougl")
             && user.getPassword()!=null && user.getPassword().equals("123456")){
             //从后台代码获取国际化信息
             RequestContext requestContext = new RequestContext(request);
             String username = requestContext.getMessage("username");
             user.setUsername(username);
             model.addAttribute("user", user);
             return "success";
         }
         return "error";
        
     }

}

其余文件与 4小节相同。

你可能感兴趣的:(SpringMVC 国际化 Day19 2018-12-07)