(1)创建Maven web project
使用Eclipse新建一个Maven Web Project ,项目取名为:
spring-boot-jsp
(2)在pom.xml文件添加依赖
<!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
<java.version>1.8</java.version>
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat 的支持.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
(3)application.properties配置
添加src/main/resources/application.properties:
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application
(4)编写测试Controller
@Controller
public class HelloController {
private String hello;
@RequestMapping("/helloJsp")
public String helloJsp(Map<String,Object> map){
System.out.println("HelloController.helloJsp().hello=hello");
map.put("hello", hello);
return "helloJsp";
}
}
(5)编写JSP页面
在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面:helloJsp.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>Insert title here</title>
</head>
<body>
helloJsp
<hr>
${hello}
</body>
</html>
(6)编写启动类
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
特别说明:针对el表达式,类似${hello} 这个对于servlet的版本是有限制的,2.4版本版本以下是不支持的,是无法进行识别的,请注意。