SpringBoot官方不推荐的jsp,先来看看整体的框架结构,跟前面介绍Thymeleaf的时候差不多,只是多了webapp这个用来存放jsp的目录,静态资源还是放在resources的static下面。
引入依赖
<!--WEB支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jsp页面使用jstl标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--用于编译jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
使用内嵌的tomcat容器来运行的话只要这3个就好了
application.properties配置
要支持jsp,需要在application.properties中配置返回文件的路径以及类型
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
这里指定了返回文件类型为jsp,路径是在/WEB-INF/jsp/下面。
创建WebApp文件夹
新建Springboot项目如果模板要用jsp的话,需要把jsp文件放到webapp下面,这时需要手动创建文件夹,或者把别的项目中的webapp拷贝过来,这时该文件夹是不是能被识别的,需要如下图:
解决方法:
只需要配置一下,将webapp文件夹关联上就可以了,如下图:
控制类
上面步骤有了,这里就开始写控制类,直接上简单的代码,跟正常的springMVC没啥区别:
@RequestMapping("/showEmp")
public ModelAndView showEmp(){
ModelAndView mav=new ModelAndView("/show");
List list=new ArrayList<Emp>();
Emp emp=new Emp();
list=empService.queryAll(emp);
mav.addObject("list",list);
return mav;
}
jsp页面编写
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>learn Resources</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div style="text-align: center;margin:0 auto;width: 1000px; ">
<h1>Spring boot</h1>
<a href="/emp/toAdd">添加员工</a>
<table width="100%" border="1" cellspacing="1" cellpadding="0">
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
</tr>
<c:forEach var="emp" items="${list}">
<tr>
<td th:text="${emp.empId}">编号</td>
<td th:text="${emp.empName}">姓名</td>
<td th:text="${emp.password}">密码</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
启动类
启动类不变还是最简单的
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
内嵌Tomcat容器运行项目
基本配置好了就可以启动项目http://localhost:8080/emp/showEmp
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/fmwind/article/details/81144905
原文链接:https://blog.csdn.net/fmwind/article/details/81144905
发表评论