配置视图解析器
1.导入依赖包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>
该包主要提供了DispatcherServlet前端控制器
2.在spring-webmvc的目录下可以看到DispatcherServlet.properties这样一个配置属性文件,里面包含了各种处理器的配置信息
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
这是视图解析器的配置信息,我们可以在他的父类中看到如下信息
public static final String REDIRECT_URL_PREFIX = "redirect:";
public static final String FORWARD_URL_PREFIX = "forward:";
private String prefix = "";
private String suffix = "";
- REDIRECT_URL_PREFIX:在controller可以使用该属性进行重定向
- FORWARD_URL_PREFIX:在controller可以指定该属性进行请求转发
- prefix:视图解析器路径前缀
- suffix:视图解析器后缀
例如:当controller返回的字符串为page时,对应的路径为 prefix+page+suffix
配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.vmware.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
将视图解析器注册到spring-mvc的核心配置文件中,并指定前缀与后缀