虽然 SpringBoot 不推荐使用 JSP, 而是推荐使用 Thymeleaf, 甚至更加推荐流行的前后端分离, 使用 vue 框架, 但是以后说不定能用到, 所以在学习的时候记录一下 . 直接上代码
在项目目录下建一个 webapp, 然后在 webapp 下建一个 jsp, 如图:
pom.xml 如下:
- <?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>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.thr</groupId>
- <artifactId>spring-boot-jsp</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>spring-boot-jsp</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <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>
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- <scope>provided</scope>
- </dependency>
- <!-- servlet-api -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-API</artifactId>
- <version>3.1.0</version>
- <scope>provided</scope>
- </dependency>
- <!--jsp -->
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>javax.servlet.jsp-API</artifactId>
- <version>2.2.1</version>
- <scope>provided</scope>
- </dependency>
- <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- <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>
- </build>
- </project>
- ------------------------------------------------------------------------------------
- pom.xml 里面注意要添加以下依赖, jsp 才能生效.
- <dependency>
- <groupId>org.apache.tomcat.embed</groupId>
- <artifactId>tomcat-embed-jasper</artifactId>
- <scope>provided</scope>
- </dependency>
- <!-- servlet-api -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-API</artifactId>
- <version>3.1.0</version>
- <scope>provided</scope>
- </dependency>
- <!--jsp -->
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>javax.servlet.jsp-API</artifactId>
- <version>2.2.1</version>
- <scope>provided</scope>
- </dependency>
- <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- 下面这段也要在 pom.xml 里面加上, 否则访问不到页面, 该段的意思是: 配置编译时将 Web 资源也进行编译; 在 pom.xml 文件的 < 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>
- <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>
同时在 resources 中的 application.properties 中配置如下:
- ?
- server.port=8080
- spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
上面的配置非常的重要, 如果出错就可能访问出现 404.
下面是 Controller 的代码:
- @Controller
- public class IndexController {
- @RequestMapping(value = "/index",method = RequestMethod.GET)
- public String index(Model model){
- model.addAttribute("msg","Hello SpringBoot JSP");
- return "index";
- }
- }
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> ${msg} </body> </HTML>
启动项目然后访问 http://localhost:8080/index 成功显示界面:
来源: http://www.bubuko.com/infodetail-3717128.html