之前一直用的是Thymeleaf视图解析器:ThymeleafViewResolver。需要在SpringMVC的核心配置文件配置ThymeleafViewResolver。参考:https://blog.csdn.net/Archer__13/article/details/124260242
InternalResourceViewResolver是jsp的视图解析器,需要将SpringMVC的核心配置文件改为如下
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启组件扫描:如果扫描多个包,多个包之间使用逗号隔开或者直接写这些包共有的上层目录 -->
<context:component-scan base-package="com.mvc.controller"></context:component-scan>
<!-- 配置JSP视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/templates/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
配置好jsp视图解析器之后,可以使用JSP页面。
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JspController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testJsp")
public String goToTest() {
return "test";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>index.jsp页面</h1>
<a href="${pageContext.request.contextPath}/testJsp">访问test.jsp页面</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>test.jsp页面</h1>
</body>
</html>