一:InternalResourceViewResolver
1.InternalResourceViewResolver
JSP是常见的视图技术,可以使用InternalResourceViewResolver作为视图解析器。
二:JstlView
1.JstlView说明
如果项目中使用了JSTL。
InternalResourceViewResolver会将InternalResourceView转为JstlView。
2.fmt标签说明
如果使用fmt标签。
需要在springmvc配置文件中配置国际化资源文件。
三:程序
1.添加jstl需要的lib包
2.添加配置文件
3.success
需要导入fmt标签库。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ 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>
success page
<br><br>
<fmt:message key="i18n.username"></fmt:message><br>
<br>
<fmt:message key="i18n.password"></fmt:message><br>
</body>
</html>
4.配置国际化资源文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置自定义扫描的包 -->
<context:component-scan base-package="com.spring.it" ></context:component-scan> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 配置国际化资源文件 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
</beans>
5.controller
package com.spring.it; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloWorldControl {
@RequestMapping("/helloworld")
public String hello() {
System.out.println("hello world*");
return "success";
}
}
6.index
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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>
<a href="helloworld">Test Jstl</a>
</body>
</html>
7.效果