SpringBoot集成JSP
加依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 添加Servlet依赖模块 --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provided</scope></dependency><!-- 添加JSTL标签库依赖模块 --><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><!-- 使用JSP引擎,SpringBoot内置Tomcat没有此依赖 --><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><scope>provided</scope></dependency>
</dependencies>
加配置
#Spring boot视图配置
spring:mvc:view:prefix: /WEB-INF/pages/suffix: .jsp
加web.xml
src/main/webapp/WEB-INF/web.xml
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5">
</web-app>
加controller
@Controller
public class DemoController {@RequestMapping("/index")public String index(HttpServletRequest request){//将用户信息保存到Request对象中request.setAttribute("name","张三");//返回首页return "index";}
}
加jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>首页</title>
</head>
<body>
<h3>使用JSP表达式:</h3>
用户姓名:<%= request.getAttribute("name") %><br/><h3>使用EL表达式语言:</h3>
用户姓名:${requestScope.name}<br/><h3>使用JSTL标签库:</h3>
用户姓名:<c:out value="${requestScope.name}" /><br/>
</body>
</html>
注意这里面的:isELIgnored="false"
, 否则el表达式不识别。
SpringBoot打war包
启动类继承SpringBootServletInitializer
@SpringBootApplication
public class JspApplication extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(JspApplication.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(JspApplication.class);}
}
pom设置打包方式为war
<groupId>com.github.xjs</groupId>
<artifactId>springboot-jsp-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
IDEA集成Tomcat
源码下载:https://github.com/xjs1919/enumdemo/tree/master/springboot-jsp-demo