spring:message标签
可以使⽤<spring:message>标签结合 ResourceBundleMessageSource 的功能,在⽹页上显⽰ messages.properties 中的⽂字讯息,例如在messages.properties⽂件中定义如下:
welcome=Hello, {0}  {1}. Welcome to Spring.
注意: messages.properties 档案必须在 Classpath 可以存取到的路径下,例如放在 /WEB-INF/classes/下,您的 Bean 定义档中要加⼊ResourceBundleMessageSource 的定义,例如:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:mvc="/schema/mvc" xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:aop="/schema/aop" xmlns:p="/schema/p"
xmlns:tx="/schema/tx" xmlns:jdbc="/schema/jdbc"
xmlns:util="/schema/util"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-3.2.xsd
/schema/context
/schema/context/spring-context-3.2.xsd
/schema/tx
/schema/tx/spring-tx-3.2.xsd
/schema/util
/schema/util/spring-util-3.2.xsd
/schema/mvc
/schema/mvc/spring-mvc-3.2.xsd">
<!-- 打开注解  -->
<context:annotation-config />
<mvc:annotation-driven />
......
<!-- 国际化操作如果采⽤基于(请求/Session/Cookie)则必需配置 -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<bean id="messageSource" class="t.support.ResourceBundleMessageSource">
<!-- 国际化信息所在的⽂件名 -->
<property name="basename" value="messages/messages"/>
<!-- 如果在国际化资源⽂件中不到对应代码的信息,就⽤这个代码作为名称  -->
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
......
</beans>
在页⾯中可以直接使⽤
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Message Tag Demo</title>
</head>
springmvc常用标签<body>
<h1><spring:message code="welcome" arguments="Justin,Lin"/></h1>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。