SpringMVC完成HTML资源国际化例子

日期:0303

SpringMVC完成HTML资源国际化例子

题目:设计一个html页面.这个页面中有两个超链接和一段文字
两个超链接都链接到本页面,但是要求根据超链提供国际化参数替换显示对应的文字内容.

工具:eclipse、springmvc

  • 首先我们有个jsp页面如下:
    在这里插入图片描述

  • 通过IOC容器的配置直接访问html资源文件

<!--添加一个HTML访问配置 -->
<mvc:resources location="/WEB-INF/html/" mapping="/html/**.html"></mvc:resources>
  • 在这个页面我们要显示这样几个内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h4 id="content">这是一段中文内容</h4>
	<a id="a1" href="/SpringMVC-20200304/resource?locale=zh_CN">中文国际化资源</a>
	<a id="a2" href="/SpringMVC-20200304/resource?locale=en_US">英文国际化资源</a>
</body>
</html>
  • 处理流程:index->html->controller

    a).先准备资源文件(中文资源、英文资源)
    在这里插入图片描述

配置到IOC容器中

<!--配置读取国际化资源对象 -->
<bean  id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basename" value="test"></property>
</bean>

b).index到html我们检查浏览器语言的类型先加载一次资源文件所以我们通过跳转到html页面 使用异步来获取国际化资源文件,在加载出来

在test.html下添加 js代码:

<script type="text/javascript" src="/SpringMVC-20200304/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
	$(function(){
		//第一次加载
		$.ajax({
			type:"GET",
			url:"/SpringMVC-20200304/resource",
			statusCode:{
				200:function(data){
					$("#content").html(data.content);
					$("#a1").html(data.a1);
					$("#a2").html(data.a2);
				},
				500:function(){
					alert("服务器内部错误");
				}
			}
		});
		//中文国际化
		$("#a1").click(function(){
			$.ajax({
				type:"GET",
				url:this.href,
				statusCode:{
					200:function(data){
						$("#content").html(data.content);
						$("#a1").html(data.a1);
						$("#a2").html(data.a2);
					},
					500:function(){
						alert("服务器内部错误");
					}
				}
			});
			return false;
		});
		//英文国际化
		$("#a2").click(function(){
			$.ajax({
				type:"GET",
				url:this.href,
				statusCode:{
					200:function(data){
						$("#content").html(data.content);
						$("#a1").html(data.a1);
						$("#a2").html(data.a2);
					},
					500:function(){
						alert("服务器内部错误");
					}
				}
			});
			return false;
		});
	});
</script>

到Controller代码

package com.springmvc.controller;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ResourceController {

	//从容器中自动装配上
	@Autowired
	private ResourceBundleMessageSource messageSource;
	
	@RequestMapping(value="/resource",method=RequestMethod.GET)
	public ResponseEntity<Map<String,String>> getResource(Locale locale){
		try {
			Map<String,String> map = new HashMap<String,String>();
			map.put("content", messageSource.getMessage("content", null, locale));
			map.put("a1", messageSource.getMessage("a1", null, locale));
			map.put("a2", messageSource.getMessage("a2", null, locale));
			//200
			return ResponseEntity.status(HttpStatus.OK).body(map);
		} catch (NoSuchMessageException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
}

c)使用超链接传locale参数改变国际化资源,默认情况下找浏览器里的,我们要根据请求处理要用到SessionLocaleResolver替换默认的LocaleResolver。同时还要添加一个拦截器让它在你提交参数时进行工作转变和创建对应的locale对象。

	<!-- 配置SessionLocaleResolver替换默认的LocaleResolver -->
	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
	
	<!-- 配置LocaleChangeInterceptor拦截器,能够根据请求中的locale参数创建对应的locale对象 -->
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
	</mvc:interceptors>

结果:
在这里插入图片描述在这里插入图片描述
总结:使用HTML页面存国际化资源不能存在作用域里,不然获取不到。使用超链接一定要写配置和拦截器。

原创文章 39 获赞 40 访问量 4172

猜你喜欢

转载自blog.csdn.net/qq_45048713/article/details/104691643