SpringMVC中JstlView的使用(国际化)

JstlView

导入Jstl相关jar包或配置文件中如下编辑都可以将view对象变成JstlView。可以支持快速国际化。

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
		<!--指定view类-->
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
	</bean>

需要导入的jar包:

<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-spec -->
		<dependency>
			<groupId>org.apache.taglibs</groupId>
			<artifactId>taglibs-standard-spec</artifactId>
			<version>1.2.5</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
		<dependency>
			<groupId>org.apache.taglibs</groupId>
			<artifactId>taglibs-standard-impl</artifactId>
			<version>1.2.5</version>
		</dependency>

JavaWeb国际化步骤

  1. 得得到一个Locale对象
  2. 使用ResourceBundle绑定国际化资源文件
  3. 使用ResourceBuodle.getString(“key”)获取到国际化配置文件中的值。
  4. web页面的国际化,fmt标签库来做。

fmt:setLocale
fmt:setBundle
fmt:message

有了Jstl以后国际化步骤

  1. 让Spring管理国际化资源文件就行
  2. 直接去页面使用

实现步骤

1、新建资源文件(中、英)

在这里插入图片描述

2、编写xml配置文件
<!-- 让SpringMVC管理国际化资源文件:配置一个资源文件管理器
		id必须为messageSource
	 -->
	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<!-- 指定基础名 -->
		<property name="basename" value="i18n"></property>
	</bean>
3、页面进行使用
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!-- 导入fmt标签库 -->
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>
	<fmt:message key="welcomeinfo"></fmt:message>
</h1>
<form action="">
<fmt:message key="username"></fmt:message><input><br>
<fmt:message key="password"></fmt:message><input><br>
<input type="submit" value="<fmt:message key="loginBth"></fmt:message>"/><br>
</form>
</body>
</html>

注意

一定要过SpringMVC的视图解析流程,才会创建一个JstlView帮你快速国际化。
也不能写forword等前缀。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值