一、介绍
(1)Tomcat中提供了JSPServlet负责处理JSP文件。
(2)SpringMVC默认有请求转发视图(forward:)和重定向视图(redirect:),我们可以利用请求转发视图,将JSP文件的视图解析任务交给JSPServlet处理,故配置JSP视图解析器实际上是配置请求转发视图解析器,同时可以配置视图解析器的前缀和后缀属性来减少冗余代码。
二、项目测试
(1)在/WEB-INF/templates下创建index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>Title</title></head><body><h1>jsp</h1></body></html>
(2)编写SpringMVC-config.xml文件,配置JSP视图解析器
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:mvc="/schema/mvc"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd/schema/context /schema/context/spring-context.xsd/schema/mvc /schema/mvc/spring-mvc.xsd"><context:component-scan base-package="com.wsh.controller"></context:component-scan><!--配置JSP视图解析器--><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/templates/"></property><property name="suffix" value=".jsp"></property></bean><!--开启注解驱动,否则控制器中带有@RequestMapping的方法失效--><mvc:annotation-driven/><!--指定请求返回对应页面--><mvc:view-controller path="/index" view-name="index"></mvc:view-controller></beans>
(3)运行