SpringMVC国际化以及多语言的使用

    xiaoxiao2024-03-11  126

    创建SpringMVC项目 配置web.xml <?xml version="1.0" encoding="UTF-8"?>

    <!--Spring 配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!--监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置转发器--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

    配置Spring文件(dispatcher-servlet.xml)

    <?xml version="1.0" encoding="UTF-8"?>

    mvc:annotation-driven/ mvc:default-servlet-handler/ <context:component-scan base-package=“controller”/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/><!-- 前缀 --> <property name="suffix" value=".jsp"/><!-- 后缀,自动拼接 --> </bean> 添加多语言的配置文件 添加language_en_US.properties到src目录下 language.cn = \u4e2d\u6587 language.en = English internationalisation = \u0020Internationalisation welcome = This is the English environment introduce= This is I18N Demo

    添加language_zh_CN.properties到src目录下 language.cn = \u4e2d\u6587 language.en = English internationalisation = \u56fd\u9645\u5316 welcome = \u8fd9\u662f\u4e2d\u6587\u73af\u5883 introduce= \u8fd9\u662f\u56fd\u9645\u5316\u7684\u4e8b\u4f8b

    加入i18n 过滤器到配置文件中 将配置添加到dispatcher-servelet中

    mvc:interceptors </mvc:interceptors>

    在页面中使用多语言 在Controller中添加路径 @Controller public class HelloController { @RequestMapping("/hello.action") public String index() { return “hello”; } }

    在JSP页面中使用 通过<spring:message code=“welcome”/>将配置文件中的内容读取 <%@ page language=“java” contentType=“text/html; charset=UTF-8”%> <%@taglib prefix=“spring” uri=“http://www.springframework.org/tags” %>

    SpringMVC Language:    

    当前语言: ${pageContext.response.locale }

    访问该项目的/hello.action,通过链接可以切换语言 中文

    英文

    项目结构


    作者:呜呜呜啦啦啦 来源: 原文:https://blog.csdn.net/u013360850/article/details/70860144 版权声明:本文为博主原创文章,转载请附上博文链接!

    最新回复(0)