Spring Boot下使用JSP页面

一、创建webapp目录

在src/main下创建webapp目录,用于存放jsp文件。这就是一个普通的目录,无需执行Mark Directory As

二、创建jsp

1、指定web资源目录

在spring boot工程中若要创建jsp文件,一般是需要在src/main下创建webapp目录,然后在该目录下创建jsp文件。但通过Alt + Insert发现没有创建jsp文件的选项。此时,需要打开Project Structrue窗口,将webapp目录指定为web资源目录,然后才可以创建jsp文件。

指定后便可看到下面的窗口情况。

此时,便可在webapp中找到jsp的创建选项了。


2、创建index.jsp页面与welcome.jsp页面

三、添加jasper依赖

在pom中添加一个Tomcat内嵌的jsp引擎jasper依赖。

  1. <dependency>
  2. <groupId>org.apache.tomcat.embed</groupId>
  3. <artifactId>tomcat-embed-jasper</artifactId>
  4. </dependency>

四、注册资源目录

在pom文件中将webapp目录注册为资源目录

  1. <build>
  2. <resources>
  3. <!--注册webapp目录为资源目录-->
  4. <resource>
  5. <directory>src/main/webapp</directory>
  6. <targetPath>META-INF/resources</targetPath>
  7. <includes>
  8. <include>**/*.*</include>
  9. </includes>
  10. </resource>
  11. </resources>
  12. <plugins>
  13. <plugin>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-maven-plugin</artifactId>
  16. </plugin>
  17. </plugins>
  18. </build>

不过,我们一般会添加两个资源目录:

  1. <resources>
  2. <!--注册Dao包目录下Mybatis映射文件资源目录-->
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.xml</include>
  7. </includes>
  8. </resource>
  9. <!--注册webapp目录为资源目录-->
  10. <resource>
  11. <directory>src/main/webapp</directory>
  12. <targetPath>META-INF/resources</targetPath>
  13. <includes>
  14. <include>**/*.*</include>
  15. </includes>
  16. </resource>
  17. </resources>

四、创建Controller

五、逻辑视图配置

六、访问

原文地址:https://www.cnblogs.com/edda/p/13261560.html