四、springMVC之视图&视图解析器
一、视图解析
过程:
请求处理方法执行完成后,最终返回一个ModelAndView对象。对于那些返回String、View 或 ModeMap等类型的处理方法,springMVC也会在内部将他们装配后成一个ModelAndView对象,他包含了逻辑名和模型对象的视图。
springMVC借助视图解析器得到最终的视图对象View,最终的视图可以是JSP,也可能是Excel等各种表现形式的视图;对于最终究竟采取何种视图对象 对模型数据进行渲染,处理器不关心,处理器工作重点聚焦在生成模型数据的工作上,从而实现MVC的充分解耦;。
视图的作用是渲染模型数据,将模型里的数据以某种形式呈现给客户;视图对象由视图解析器负责实例化。由于视图是无状态的,所以他们不会有有线程安全的问题;

常用的视图实现类:
| 大类 | 视图类型 | 说明 |
| URL视图资源 | InternalResourceView |
|
| JstlView |
|
|
| 文档资源 | AbstractExcelView |
|
| AbstractPdfView |
|
|
| 报表视图 | ConfigurableJsperReportsView |
|
| JasperReportsCsvView | ||
| JasperReportsMulitiFormatView | ||
| JasperReportshtmlView | ||
| JasperReportsPdfView | ||
| JasperReportsXlsView | ||
| JSON视图 | MappingJacksonView |
|
常用的视图解析器实现类:
| 大类 | 视图类型 | 说明 |
| 解析为Bean的名称 | BeanNameViewResolver | 将逻辑视图名解析为一个Bean,Bean的id等于逻辑视图名。 |
| 解析为URL文件 | InternalResourceViewResolver | 将视图名解析为一个URL文件,一般使用该解析器将视图名映射为一个保存在WEB-INF目录下的程序文件(如JSP) |
| JasperReportsViewResolver |
|
|
| 模板文件视图 | FreeMarkViewResolver |
|
| VelocityViewResolver | 解析为基于Velocity模板技术的模板文件 | |
| VelocityLayoutViewResolver |
实例验证:
InternalResourceView 之前的实例都有;这里就验证jstlView实例;
目录结构和配置方式:
同HelloWord的配置方式;

添加jstl jar包:
-
jstl.jar
- standard.jar
spring国际化配置:
basename:默认的扫描的国际化文件名为i18n。
1 <bean id="messageSource" 2 class="org.springframework.context.support.ResourceBundleMessageSource"> 3 <property name="basename" value="i18n">property> 4 bean>
完整文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="handler">context:component-scan> 11 12 13 <bean 14 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="prefix" value="/WEB-INF/views/">property> 16 <property name="suffix" value=".jsp">property> 17 bean> 18 19 20 21 <bean id="messageSource" 22 class="org.springframework.context.support.ResourceBundleMessageSource"> 23 <property name="basename" value="i18n">property> 24 bean> 25 26 beans>
国际化文件:
- i18n_en_US.properties 文件名为i18n ,en_US 代表英文
1 i18n.username=Username 2 i18n.password=Password
- i18n_zh_CN.properties: 文件名为i18n ,zh_CN 代表中文
1 i18n.username=用户名 2 i18n.password=密码
RequestMappingTest:
1 package handler; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @RequestMapping("/springmvc") 7 @Controller 8 public class RequestMappingTest { 9 private static final String SUCCESS = "success"; 10 11 @RequestMapping("/testJstlView") 12 public String jstlView() { 13 System.out.println("jstlView"); 14 return SUCCESS; 15 } 16 }
index.jsp:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" isErrorPage="false"%> 3 DOCTYPE html> 4 <html> 5 <head> 6 <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> 7 <title>Insert title heretitle> 8 head> 9 <body> 10 <a href="/springmvc/testJstlView">testJstlViewa> 11 body> 12 html>
success.jsp:
添加taglib ;
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" isErrorPage="true"%> 3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> 5 <%@ taglib prefix="s" uri="http://www.springframework.org/tags"%> 6 DOCTYPE html> 7 <html> 8 <head> 9 <meta charset="UTF-8"> 10 <title>Insert title heretitle> 11 head> 12 <body> 13 <h4>success成功h4> 14 <br> 15 <fmt:bundle basename="i18n"> 16 <fmt:message key="i18n.username">fmt:message> 17 18 fmt:bundle> 19 <br> 20 <fmt:bundle basename="i18n"> 21 <fmt:message key="i18n.password">fmt:message> 22 fmt:bundle> 23 <br /> 24 <s:message code="i18n.password">s:message> 25 26 <br /> 27 body> 28 html>
运行结果:
index.jsp运行结果如下图;点击testJstlView 跳转到success 页面,且控制台打印jstlView


配置可以直接转发的页面
如果配置了直接可以转发的页面,那么可以直接响应转发的页面,无需再经过Handler的方法。同时需要配置 mvc:annotation-driven标签
配置方法如下:
1 <mvc:view-controller path="/success" view-name="success"/> 2 3 4 <mvc:annotation-driven>mvc:annotation-driven>
spring.xml的完整文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <context:component-scan base-package="handler">context:component-scan> 11 12 13 <bean 14 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 15 <property name="prefix" value="/WEB-INF/views/">property> 16 <property name="suffix" value=".jsp">property> 17 bean> 18 19 20 21 <bean id="messageSource" 22 class="org.springframework.context.support.ResourceBundleMessageSource"> 23 <property name="basename" value="i18n">property> 24 bean> 25 26 27 28 <mvc:view-controller path="/success" view-name="success"/> 29 30 31 <mvc:annotation-driven>mvc:annotation-driven> 32 33 beans>
运行效果如下:
