Springboot学习-4
Springboot集成jsp
- 在pom文件加入Spring boot 内嵌的Tomcat 对JSP的解析包
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--servlet依赖的jar包start-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!--servlet依赖的jar包end-->
<!--jsp依赖的jar包start-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
<!--jsp依赖的jar包end-->
<!--jstl标签依赖的jar包start-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--jstl标签的jar包end-->
-
在 application.properties文件配置spring mvc 的视图展示为jsp:
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp -
在src/main建一个webapp目录,然后建一个jsp,
jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>hello springboot</title>
</head>
<body>
${msg}
</body>
</html>
然后新建 JspController
package com.nanianxiatian.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author nanianxtiatian
*/
@Controller
public class JspController {
@GetMapping("/test/index")
public String index(Model model){
model.addAttribute("msg","springboot 集成jsp!");
return "index";
}
}
运行代码,发现找不到界面:
这时需要在 pom文件的 build 标签下添加 标签
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
再次访问,可以访问到!