最近在学习springboot,根据网上各位大神的博客以及官网例子,经历了小白的各种艰难险阻,尚未达到理想效果。spring boot整合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> <groupId>com.example</groupId> <artifactId>spring-boot-sample-jsp1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>spring-boot-sample-jsp1</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加对tomcat的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!--对jsp的支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>required</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- 测试controller,需要留意这里要注解是@Controller,不是@RestController,否则怎么配置都不会返回页面的,后者返回的是内容,各位可以去自习了解下
package com.example.springbootsamplejsp1; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; /** * 功能说明 * @date 2017年9月22日 下午3:00:06 * @author wusj * @version 1.0 */ @Controller public class TestController { @RequestMapping("/index") public String index(Map<String,Object> map){ map.put("name", "wusj"); return "index"; } }
- 新建maven项目时,需要选择war,而非jar,我不知道jar包该如何弄。我一开始哪里都对的,就是项目是jar包,而非war包,一直报错:This application has no explicit mapping for /error, so you are seeing this as a fallback
- 启动类需要继承SpringBootServletInitializer,或者单独写个类继承它ServletInitializer.java
package com.example.springbootsamplejsp1; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import com.example.SpringBootSampleJsp1Application; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootSampleJsp1Application.class); } }
- 配置文件需要配置相关信息
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
- 以上都做到了还出不来的话,初步判断可能rp不如我好。
- 希望大家不要走弯路,嘻嘻。项目结构如下