Spring Boot学习总结(2)——Spring Boot整合Jsp

怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功

整合jsp,于是决定将整合过程记载下来。

无论使用的是那种ide,基本在maven的使用上都是相同的,本文使用的是myeclipse,创建maven web工程,pom中依赖如下:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>cn.com</groupId>  
  5.   <artifactId>springboot-Web</artifactId>  
  6.   <packaging>jar</packaging>  
  7.   <version>1.0-SNAPSHOT</version>  
  8.   <name>springboot-Web Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   
  11.   <parent>  
  12.     <groupId>org.springframework.boot</groupId>  
  13.     <artifactId>spring-boot-starter-parent</artifactId>  
  14.     <version>1.2.5.RELEASE</version>  
  15.     <relativePath/>  
  16.   </parent>  
  17.   
  18.   <properties>  
  19.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  20.     <java.version>1.7</java.version>  
  21.     <version.spring>3.2.9.RELEASE</version.spring>  
  22.     <version.jackson>2.4.4</version.jackson>  
  23.   </properties>  
  24.   
  25.   <dependencies>  
  26.   
  27.     <dependency>  
  28.       <groupId>javax.servlet</groupId>  
  29.       <artifactId>jstl</artifactId>  
  30.       <version>1.2</version>  
  31.     </dependency>  
  32.     <dependency>  
  33.       <groupId>org.springframework.boot</groupId>  
  34.       <artifactId>spring-boot-starter-web</artifactId>  
  35.     </dependency>  
  36.     <!--WebJars-->  
  37.   
  38.     <dependency>  
  39.       <groupId>org.springframework.boot</groupId>  
  40.       <artifactId>spring-boot-starter-test</artifactId>  
  41.       <scope>test</scope>  
  42.     </dependency>  
  43.   
  44.     <dependency>  
  45.       <groupId>org.apache.tomcat.embed</groupId>  
  46.       <artifactId>tomcat-embed-jasper</artifactId>  
  47.       <scope>provided</scope>  
  48.     </dependency>  
  49.   
  50.   </dependencies>  
  51.   
  52.   <build>  
  53.     <plugins>  
  54.       <plugin>  
  55.         <groupId>org.springframework.boot</groupId>  
  56.         <artifactId>spring-boot-maven-plugin</artifactId>  
  57.         <!--<version>${spring.boot.version}</version>-->  
  58.         <configuration>  
  59.           <mainClass>cn.com.SpringBootWebApplication</mainClass>  
  60.           <layout>ZIP</layout>  
  61.         </configuration>  
  62.         <executions>  
  63.           <execution>  
  64.             <goals>  
  65.               <goal>  
  66.                 repackage  
  67.               </goal>  
  68.             </goals>  
  69.           </execution>  
  70.         </executions>  
  71.       </plugin>  
  72.     </plugins>  
  73.   </build>  
  74.   
  75.   <repositories>  
  76.     <repository>  
  77.       <id>spring-snapshots</id>  
  78.       <url>http://repo.spring.io/snapshot</url>  
  79.       <snapshots><enabled>true</enabled></snapshots>  
  80.     </repository>  
  81.     <repository>  
  82.       <id>spring-milestones</id>  
  83.       <url>http://repo.spring.io/milestone</url>  
  84.       <snapshots><enabled>true</enabled></snapshots>  
  85.     </repository>  
  86.   </repositories>  
  87.   <pluginRepositories>  
  88.     <pluginRepository>  
  89.       <id>spring-snapshots</id>  
  90.       <url>http://repo.spring.io/snapshot</url>  
  91.     </pluginRepository>  
  92.     <pluginRepository>  
  93.       <id>spring-milestones</id>  
  94.       <url>http://repo.spring.io/milestone</url>  
  95.     </pluginRepository>  
  96.   </pluginRepositories>  
  97. </project>  
其中最需注意的如下这个依赖,少了这一个不能使用jsp。

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <dependency>  
  2.   <groupId>org.apache.tomcat.embed</groupId>  
  3.   <artifactId>tomcat-embed-jasper</artifactId>  
  4.   <scope>provided</scope>  
  5. </dependency>  
同时在resources中的application.properties中配置如下:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. spring.view.prefix=/WEB-INF/jsp/  
  2. spring.view.suffix=.jsp  
  3. application.message: Hello Phil  
  4. server.port=8282  
最下两个配置可有可无,上面的两个配置中,是配置jsp所在目录,在这里和官网略有不同,官网配的都是spring.mvc.view的配置,但这样

似乎不起作用,有了解的可以与本人探讨一下。

下面就是controller编写,具体不再讲解,仅贴出代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.com.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. import java.util.Date;  
  8. import java.util.Map;  
  9.   
  10. /** 
  11.  * Created by Administrator on 2016/5/6. 
  12.  */  
  13. @Controller  
  14. public class IndexController {  
  15.   
  16.     @Value("${application.message:Hello World}")  
  17.     private String message = "Hello World";  
  18.   
  19.     @RequestMapping("/")  
  20.     public String welcome(Map<String, Object> model) {  
  21.         model.put("time"new Date());  
  22.         model.put("message"this.message);  
  23.         return "welcome";  
  24.     }  
  25. }  
同时在启动类的编写如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.com;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.builder.SpringApplicationBuilder;  
  6. import org.springframework.boot.context.web.SpringBootServletInitializer;  
  7.   
  8. /** 
  9.  * Created by Administrator on 2016/5/6. 
  10.  */  
  11. @SpringBootApplication  
  12. public class SpringBootWebApplication extends SpringBootServletInitializer {  
  13.   
  14.     @Override  
  15.     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
  16.         return application.sources(SpringBootWebApplication.class);  
  17.     }  
  18.   
  19.     public static void main(String[] args) throws Exception {  
  20.         SpringApplication.run(SpringBootWebApplication.class, args);  
  21.     }  
  22.   
  23. }  
在这里,使用jsp必须继承SpringBootServletInitializer类,在这使用其他文章的原文:

对于需要部署到传统servlet容器之中的应用,Boot提供了一种方式以编码的方式初始化Web配置。为了使用这一点,Boot提供了可选的WebApplicationInitializer,

它会使用servlet容器来注册应用,这会通过Servlet 3.0 API以编码的方式注册servlet并且会用到ServletContext。通过提供SpringBootServletInitializer的子类,

Boot应用能够使用嵌入的Spring上下文来注册配置,这个Spring上下文是在容器初始化的时候创建的。

如此之后,就可以启动springboot应用使用jsp了。

原文地址:https://www.cnblogs.com/zhanghaiyang/p/7212981.html