spring mvc利用AcceptHeaderLocaleResolver实现国际化语言

这种技能就属于套路化操作,

跟着走一次就OK了。


springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.2.xsd">
		
	<context:component-scan base-package="org.fkit.controller"/>
	<mvc:annotation-driven/>
	<mvc:default-servlet-handler/>
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/content/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	
	</bean>
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames" value="message" />	
	</bean>
	<mvc:interceptors>
		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
	</mvc:interceptors>
	<bean id="localResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver" />
</beans>

message_en_US.properties


loginname = Login name:
password = Password:
submit = Submit
welcome = Welcome {0} access fkit
title = Login Page
username = administrator

message_zh_CN.properties


loginname = \u767B\u9646\u540D:
password = \u5BC6\u7801:
submit = \u63D0\u4EA4
welcome = \u6B22\u8FCE {0} \u8BBF\u95EE fkit
title = \u767B\u9646\u9875\u9762
username = \u7BA1\u7406\u5458

UserController.java


package org.fkit.controller;




import javax.servlet.http.HttpServletRequest;

import org.fkit.domain.User;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.validation.annotation.Validated;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.support.RequestContext;


@Controller

public class UserController {
	@RequestMapping(value="/{formName}", method=RequestMethod.GET)
	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) {
		
		if (user.getUsername() != null && user.getUsername().equals("fkit")
				&& user.getSex() != null && user.getSex().equals("male")) {
			RequestContext requestContext = new RequestContext(request);
			String username = requestContext.getMessage("username");
			user.setUsername(username);
			model.addAttribute("user", user);
			return "success";
		}
		return "error";
		
		
	}

	
}

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" %>

<!DOCTYPE html PUBLIC "-//W3C/DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" >
<html>
<head>
	<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
	<title>基于浏览器请求的国际化</title>
</head>
<body>

<h3><spring:message code="title"/></h3>
<br>
<form:form modelAttribute="user" action="login" method="post">
	<table>
	<tr>
		<td><spring:message code="loginname"/></td>
		<td><form:input path="username" /> </td>
	</tr>
	<tr>
		<td><spring:message code="loginname"/></td>
		<td><form:input path="sex" /> </td>
	</tr>
	
	<tr>
		<td><input id="submit" type="submit" value="<spring:message code="submit"/>"></td>
	</tr>
	
	</table>

</form:form>

</body>
</html>

这样,根据浏览器的设置,就会自动切换中英文了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值