SpringBoot10 整合JSP
1 整合JSP
1.1 导入相关依赖
JSP依赖、JSTL依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.xiangxu</groupId> <artifactId>springboottest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboottest</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--JSP相关--> <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!--JSTL相关--> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!--工具相关--> <!--代码生成工具--> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--热部署工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <!--通用工具包--> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
1.2 在springboot项目的main目录下创建webapp/WEB-INF/jsp的文件夹目录
技巧01:这里创建的是文件夹而不是包
技巧02:在jsp文件夹里面创建JSP文件
1.3 在配置文件中配置JSP文件的位置
技巧01:需要从/WEB-INF开始,不要从/webapp开始,否则会提示找不到的(因为WEB项目会从WEB-INF下面寻找JSP文件),错误信息如下
1.4 创建一个jsp文件
<%@page contentType="text/html;charset=utf-8" %> <html> <head> <title>主页面</title> </head> <body> <h2>Welcomo to the city of ChongQing.</h2> </body> </html>
1.5 创建控制层类
package cn.xiangxu.springboottest.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @Slf4j public class JspController { @GetMapping(value = "/index") public String toIndex() { log.info("正在进入登录页面......."); return "index"; } }
技巧01:不能使用@RestController代替@Controller,因为@RestController默认的返回值是json格式,就不会根据字符串去寻找相应的JSP文件了
1.5 参考博文