springboot不推荐使用jsp,默认使用的是Thymeleaf模板。不过,如果你一定要使用jsp,需要做一些配置。

依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tommy.springboot.jsp</groupId>
  <artifactId>jsp-demo</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>jsp-demo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springboot.version>1.5.9.RELEASE</springboot.version>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>
  <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>
    <!--配置servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
    </dependency>
    <!--配置jsp jstl的支持-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
    </dependency>
    <!--对jsp的支持-->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

application.properties

指定jsp文件的位置在/WEB-INF/jsp/

1
2
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

应用启动类

1
2
3
4
5
6
7
8
9
10
11
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
    public static void main( String[] args )
    {
        SpringApplication.run(Application.class, args);
    }
}

测试controller

1
2
3
4
5
6
7
8
9
@Controller
public class TestController {
    @RequestMapping(value = {"","/test"})
    public ModelAndView index(ModelAndView mv) {
        mv.setViewName("/test");
        mv.addObject("msg","你好,世界!");
        return mv;
    }
}

测试页面

1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Test Page</title>
</head>
<body>
  ${msg}
</body>
</html>

部署测试

在idea的tomcat中添加该web项目,启动tomcat。

可以看到,测试成功。

注意事项

由于jsp的性能比较低,而且对JSP的支持比较麻烦,spring推荐使用Thymeleaf模板,spring的官网即是使用的该模板。所以如无必要尽量还是不要使用JSP了。
如果是打jar包运行,把spring-boot-start-tomcat的依赖去掉。

springboot打jar包运行相关问题

1.idea多模块项目启动访问JSP404;
需要指定工作目录为当前模块
Run->Edit Configurations,将spring-boot启动类的Working Directory指定为$MODULE-WORKING-DIR$。

2.jar包无法读取配置文件(classpath路径);
可以使用spring的ResourcesLoader

1
2
3
4
5
6
7
@Bean
public ResourceLoader resourceLoader() {
	return new DefaultResourceLoader();
}

// 使用流,不要使用File
InputStream cc = resourceLoader.getResource(classPathLocation).getInputStream();

3.向资源文件写入数据

1
File file = new File(getClass().getClassLoader().getResource(URLCONFIGFILE).getFile());

4.打成jar包后运行访问页面404
a.spring-boot-maven-plugin使用1.4.2版本,并指定启动类

1
2
3
4
5
6
7
8
9
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<version>1.4.2.RELEASE</version>
	<configuration>
	  <mainClass>com.hhly.rbnode.Application</mainClass>
	  <layout>JAR</layout>
	</configuration>
  </plugin>

b.将webapp下的文件拷贝到/META-INF

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<resources>
  <resource>
	<directory>src/main/webapp</directory>
	<targetPath>META-INF/resources</targetPath>
	<includes>
	  <include>**/**</include>
	</includes>
  </resource>
  <resource>
	<directory>src/main/resources</directory>
	<filtering>false</filtering>
	<includes>
	  <include>**/**</include>
	</includes>
  </resource>
</resources>

参考:https://blog.csdn.net/u013189824/article/details/80389419