标签:todo let resolve eth undle setattr png bsp 定义
I18TestController.java:
1 package com.atguigu.controller; 2 3 import java.util.Locale; 4 5 import javax.servlet.http.HttpSession; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.context.MessageSource; 9 import org.springframework.context.support.ResourceBundleMessageSource; 10 import org.springframework.stereotype.Controller; 11 import org.springframework.ui.Model; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.RequestParam; 14 import org.springframework.web.servlet.i18n.SessionLocaleResolver; 15 16 @Controller 17 public class I18TestController { 18 19 @Autowired 20 private MessageSource messageSource; 21 22 @RequestMapping("/tologinpage") 23 public String tologinPage(@RequestParam(value="locale",defaultValue="zh_CN")String localeStr, Locale locale, Model model,HttpSession session) { 24 System.out.println(locale); 25 String welcomeinfo = messageSource.getMessage("welcomeinfo", null, 26 locale); 27 System.out.println(welcomeinfo); 28 model.addAttribute("msg", "哈哈哈"); 29 30 // // zh_CN 31 // Locale l = null; 32 // // 如果带了locale参数就用参数指定的区域信息,如果没带就用请求头的 33 // if (localeStr != null && !"".equals(localeStr)) { 34 // l = new Locale(localeStr.split("_")[0], localeStr.split("_")[1]); 35 // } else { 36 // l = locale; 37 // } 38 // session.setAttribute(SessionLocaleResolver.class.getName() + ".LOCALE", l); 39 return "login"; 40 } 41 42 }
MyLocaleResolver.java:
1 package com.atguigu.controller; 2 3 import java.util.Locale; 4 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 import org.springframework.web.servlet.LocaleResolver; 9 10 public class MyLocaleResolver implements LocaleResolver { 11 12 /** 13 * 解析返回locale 14 */ 15 @Override 16 public Locale resolveLocale(HttpServletRequest req) { 17 // TODO Auto-generated method stub 18 // zh_CN 19 Locale l = null; 20 String localeStr = req.getParameter("locale"); 21 //如果带了locale参数就用参数指定的区域信息,如果没带就用请求头的 22 if (localeStr != null && !"".equals(localeStr)) { 23 l = new Locale(localeStr.split("_")[0], localeStr.split("_")[1]); 24 } else { 25 l = req.getLocale(); 26 } 27 return l; 28 } 29 30 /** 31 * 修改locale 32 */ 33 @Override 34 public void setLocale(HttpServletRequest arg0, HttpServletResponse arg1, 35 Locale arg2) { 36 throw new UnsupportedOperationException( 37 "Cannot change HTTP accept header - use a different locale resolution strategy"); 38 } 39 40 }
login_en_US.properties:
1 welcomeinfo=welcome to atguigu.com 2 username=USERNAME 3 password=PASSWORD 4 loginBtn=LOGIN
login_zh_CN.properties:
1 welcomeinfo=\u6B22\u8FCE\u6765\u5230\u5C1A\u7845\u8C37 2 username=\u7528\u6237\u540D 3 password=\u5BC6\u7801 4 loginBtn=\u767B\u9646
springmvc.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 <context:component-scan base-package="com.atguigu"></context:component-scan> 11 12 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 13 <property name="prefix" value="/WEB-INF/pages/"></property> 14 <property name="suffix" value=".jsp"></property> 15 </bean> 16 17 <mvc:default-servlet-handler/> 18 <mvc:annotation-driven></mvc:annotation-driven> 19 20 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 21 <property name="basename" value="loginpage/login"></property> 22 </bean> 23 24 <!--自定义区域信息解析器 --> 25 <!-- <bean id="localeResolver" class="com.atguigu.controller.MyLocaleResolver"></bean> --> 26 27 <!-- 区域信息从session中拿 --> 28 <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean> 29 <mvc:interceptors> 30 <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean> 31 </mvc:interceptors> 32 </beans>
index.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 <% 9 pageContext.setAttribute("ctp", request.getContextPath()); 10 %> 11 </head> 12 <body> 13 <a href="${ctp }/test01">test01</a><br/> 14 <a href="tologinpage">去登陆页面</a> 15 </body> 16 </html>
login.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <h1> 12 <!-- 国际化的区域信息是决定国际化显示的因素 --> 13 <fmt:message key="welcomeinfo"/> 14 </h1> 15 <form action=""> 16 <fmt:message key="username"/>:<input /><br/> 17 <fmt:message key="password"/>:<input /><br/> 18 <input type="submit" value="<fmt:message key="loginBtn"/>"/> 19 </form> 20 <!-- 如果点击超链接切换国际化 --> 21 <a href="tologinpage?locale=zh_CN">中文</a>|<a href="tologinpage?locale=en_US">English</a> 22 </body> 23 </html>
标签:todo let resolve eth undle setattr png bsp 定义
原文地址:https://www.cnblogs.com/116970u/p/13178169.html