SpringMvc国际化

话不多说,直接用一个小例子来说明一切

1.spring.xml配置文件

      <!-- 注意 一定使用messageSource 作为bean名称 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="my"></property>
    </bean>

    <!--该拦截器用于 拦截url上的参数 只是当jsp经过action之后 才会将当前的国家和语言存储在session 同时从session中获取-->
    <mvc:interceptors>
        <bean id="localeChangeInterceptor"
              class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="locale"></property>
        </bean>
    </mvc:interceptors>
    <!-- 参数需要被临时存储在某个地方 当用户再次访问 使用之前设置的参数 -->
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>

注意:id请使用 'messageSource',Spring容器获取这个对象时默认是以这个名字去获取的,不要乱动哈。basename指的是i18n的资源文件 , value填的是路径 + 配置文件前缀:

 

 

2.编写jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    <title>My JSP 'login.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript">
        function checkSubmit(){
            document.forms[0].submit();
        }

    </script>
</head>

<body>
    <s:message code="language"></s:message>: 
    <a href="<%=path %>/demo04/mid?locale=zh_CN"><s:message code="Chinese"></s:message></a>
    <a href="<%=path %>/demo04/mid?locale=en_GB"><s:message code="English"></s:message></a>
    <form action="<%=path %>/demo04/reg" method="post">
        <s:message code="userName"></s:message> :<input type="text" name="userName"/><form:errors path="userInfo.userName" cssStyle="color: red"/> <br/>
        <s:message code="password"></s:message> :<input type="password" name="password"/><br/>
        <s:message code="repassword"></s:message> :<input type="password" name="repassword"/><br/>
        <s:message code="email"></s:message> :<input type="text" name="email"/><br/>
        <s:message code="age"></s:message> :<input type="text" name="age"/><br/>
        <s:message code="phone"></s:message> :<input type="text" name="phone"/><br/>
        <s:message code="date"></s:message> :<input type="text" name="date"/><br/>
        <s:message code="url"></s:message> :<input type="text" name="url"/><br/>
        <button onclick="checkSubmit()"><s:message code="register"></s:message> </button>
    </form>
</body>
</html>

code属性指向的是国际化资源文件中的键:

 

 

3.编写控制层

    /**
     * 切换语言
     * @return
     * @throws Exception
     */
    @RequestMapping( value="demo04/mid",method=RequestMethod.GET)
    public String test1() throws Exception{
        return "/demo04/reg.jsp";
    }

注意启动的时候要通过一个控制层的方法启动 不能直接去掉用页面的地址

 

效果如下

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值