一、介绍
当请求仅需要返回页面,不需要进行业务处理时,可用该标签。该标签能将负责返回页面和负责业务处理的方法分开。
二、项目测试
(1)编写SpringMVC-config.xml文件,使用视图控制器标签来处理只需返回页面的请求(注:记得开启注解驱动<mvc:annotation-driven/>
,否则控制器中的@RequestMapping失效)
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.wsh.controller"></context:component-scan>
<!-- 配置Thymeleaf视图解析器-->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<!--视图解析器优先级-->
<property name="order" value="1"/>
<!--解析视图用的编码-->
<property name="characterEncoding" value="UTF-8"/>
<!--视图解析器模板引擎-->
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!--视图前缀-->
<property name="prefix" value="/WEB-INF/templates/"/>
<!--视图后缀-->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
</property>
</bean>
</property>
</bean>
<!--开启注解驱动,否则控制器中带有@RequestMapping的方法失效-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--指定请求返回对应页面-->
<mvc:view-controller path="/info" view-name="info"></mvc:view-controller>
</beans>
(2)控制器类
@Controller
public class MyController {
@RequestMapping(value = {"/index"})
public String index(){
return "index";
}
}
(3)运行