Fork me on Gitee

springmvc配置的国际化资源文件显示为???key???的错误原因

 问题概述:

在spring mvc 的jstl view 视图解析器中 使用jstl <fmt:message key=”“”></fmt> 不不要绑定也可以正常显示,这里却显示标题上的错误,

一般我们遇到乱码都是直接排查编码问题,为此我耽误接近3小时,真是可恨,这里直接绑定就好;

 

1 <fmt:bundle basename="messages">
2      <fmt:message key="messages.username"></fmt:message>
3      </fmt:bundle> 

 

大纲图:

 

基本配置图:

这里只是起到对应作用,前提你要会spring mvc

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    <context:component-scan base-package="restcrud"></context:component-scan>
    <context:component-scan base-package="converters"></context:component-scan>
    <context:component-scan base-package="springmvctest"></context:component-scan>
    <context:component-scan base-package="religon"></context:component-scan>
        <!-- 在实际开发中通常都需配置 mvc:annotation-driven 标签 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
    <!-- 自定义类型转换 支持格式化 与自定义类型转换 -->
    <!-- <bean id="conversionService"  class="org.springframework.context.support.ConversionServiceFactoryBean"> 不支持格式化注解 -->
    <bean id="conversionService"  class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
            <ref bean="rmployeeConverter"/>
            </set>
        </property>
    </bean>
    <!-- 静态文件 -->
    <mvc:default-servlet-handler/>
    
    <!-- 国际化文件 -->
    <bean class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"></property>
                   <!-- 支持UTF-8的中文 -->
        <!-- <property name="defaultEncoding" value="UTF-8"/>
         <property name="useCodeAsDefaultMessage" value="true" /> -->
        <property name="cacheSeconds" value="0"/>
         <property name="defaultEncoding" value="iso-8859-1" />
    </bean>
    
    <!-- 配置展示国家化文件的  -->
     <mvc:view-controller path="/CN" view-name="CN"/> 
    <mvc:view-controller path="/US" view-name="US"/>
    <!-- <mvc:view-controller path="/linker" view-name="linker"/> -->
    
    
    <!-- 支持超连接国际化 -->
    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
           <property name="defaultLocale" value="zh_CN"></property>
    </bean>
    <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean>
    </mvc:interceptors>

</beans>

 

 

 

 jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<%
    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>??</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">
</head>
<body>
    <fmt:bundle basename="messages">
     <fmt:message key="messages.username"></fmt:message>
     </fmt:bundle> 
    <br>
    切换中文:
    <a href="linker?locale=zh_CN">中文</a><br>
     切换英文:
    <a href="linker?locale=en_US">英文</a>
</body>
</html>

 

Locale

 

static public final Locale TAIWAN = TRADITIONAL_CHINESE;

    /** Useful constant for country.
     */
    static public final Locale UK = createConstant("en", "GB");

    /** Useful constant for country.
     */
    static public final Locale US = createConstant("en", "US");

    /** Useful constant for country.
     */
    static public final Locale CANADA = createConstant("en", "CA");

    /** Useful constant for country.
     */
    static public final Locale CANADA_FRENCH = createConstant("fr", "CA");

    /**

 

posted @ 2018-08-31 10:53  ---dgw博客  阅读(1151)  评论(0编辑  收藏  举报