本篇示例基于第一个SpringMVC程序。
如果控制器方法中只需要返回(或者设置)视图名称,方法中不需要其他处理过程,视图控制器也是另一种实现方式。具体看个例子吧。
使用控制器方法
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
使用视图控制器view-controller
在SpringMVC配置文件中使用view-controller标签,配置请求路径与视图名称的对应关系。
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.example.mvc.controller"/>
<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>
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
</beans>
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
请求路径path对应的视图名称是view-name,即请求路径/
对应的视图名称是index
。
此时,可以不需要控制器方法。
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
@Controller
public class TestController {
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
annotation-driven
如果既使用了视图控制器,又使用了控制器方法呢?如下,
视图控制器中,实现了请求路径/
、视图名称index
的映射。
控制器方法中,实现请求路径/test
、视图名称test
的映射。
<!-- SpringMVC.xml -->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
package com.example.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/test")
public String test(){
return "test";
}
}
<!-- index.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/test}">测试</a>
</body>
</html>
<!-- test.html -->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<p>this is a test.</p>
</body>
</html>
可以看到,控制器方法中的请求映射失效了。
解决方法是,在SpringMVC配置文件中添加annotation-driven,以开启注解驱动。
<!-- SpringMVC.xml -->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:annotation-driven></mvc:annotation-driven>