1. 程式人生 > >Spring Boot學習總結(2)——Spring Boot整合Jsp

Spring Boot學習總結(2)——Spring Boot整合Jsp

怎麼使用jsp上面起了疑問,查閱了多方資料,找到過其他人的部落格的描述,也找到了spring在github上的給出的例子,看完後稍微改動後成功

整合jsp,於是決定將整合過程記載下來。

無論使用的是那種ide,基本在maven的使用上都是相同的,本文使用的是myeclipse,建立maven web工程,pom中依賴如下:

  1. <projectxmlns="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.   <parent>
  11.     <groupId>org.springframework.boot</groupId>
  12.     <artifactId>spring-boot-starter-parent</artifactId>
  13.     <version>1.2.5.RELEASE</version>
  14.     <relativePath/>
  15.   </parent>
  16.   <properties>
  17.     <project.build.sourceEncoding
    >UTF-8</project.build.sourceEncoding>
  18.     <java.version>1.7</java.version>
  19.     <version.spring>3.2.9.RELEASE</version.spring>
  20.     <version.jackson>2.4.4</version.jackson>
  21.   </properties>
  22.   <dependencies>
  23.     <dependency>
  24.       <groupId>javax.servlet</groupId>
  25.       <artifactId>jstl</artifactId>
  26.       <version>1.2</version>
  27.     </dependency>
  28.     <dependency>
  29.       <groupId>org.springframework.boot</groupId>
  30.       <artifactId>spring-boot-starter-web</artifactId>
  31.     </dependency>
  32.     <!--WebJars-->
  33.     <dependency>
  34.       <groupId>org.springframework.boot</groupId>
  35.       <artifactId>spring-boot-starter-test</artifactId>
  36.       <scope>test</scope>
  37.     </dependency>
  38.     <dependency>
  39.       <groupId>org.apache.tomcat.embed</groupId>
  40.       <artifactId>tomcat-embed-jasper</artifactId>
  41.       <scope>provided</scope>
  42.     </dependency>
  43.   </dependencies>
  44.   <build>
  45.     <plugins>
  46.       <plugin>
  47.         <groupId>org.springframework.boot</groupId>
  48.         <artifactId>spring-boot-maven-plugin</artifactId>
  49.         <!--<version>${spring.boot.version}</version>-->
  50.         <configuration>
  51.           <mainClass>cn.com.SpringBootWebApplication</mainClass>
  52.           <layout>ZIP</layout>
  53.         </configuration>
  54.         <executions>
  55.           <execution>
  56.             <goals>
  57.               <goal>
  58.                 repackage  
  59.               </goal>
  60.             </goals>
  61.           </execution>
  62.         </executions>
  63.       </plugin>
  64.     </plugins>
  65.   </build>
  66.   <repositories>
  67.     <repository>
  68.       <id>spring-snapshots</id>
  69.       <url>http://repo.spring.io/snapshot</url>
  70.       <snapshots><enabled>true</enabled></snapshots>
  71.     </repository>
  72.     <repository>
  73.       <id>spring-milestones</id>
  74.       <url>http://repo.spring.io/milestone</url>
  75.       <snapshots><enabled>true</enabled></snapshots>
  76.     </repository>
  77.   </repositories>
  78.   <pluginRepositories>
  79.     <pluginRepository>
  80.       <id>spring-snapshots</id>
  81.       <url>http://repo.spring.io/snapshot</url>
  82.     </pluginRepository>
  83.     <pluginRepository>
  84.       <id>spring-milestones</id>
  85.       <url>http://repo.spring.io/milestone</url>
  86.     </pluginRepository>
  87.   </pluginRepositories>
  88. </project>
其中最需注意的如下這個依賴,少了這一個不能使用jsp。
  1. <dependency>
  2.   <groupId>org.apache.tomcat.embed</groupId>
  3.   <artifactId>tomcat-embed-jasper</artifactId>
  4.   <scope>provided</scope>
  5. </dependency>
同時在resources中的application.properties中配置如下:
  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編寫,具體不再講解,僅貼出程式碼:

  1. package cn.com.controller;  
  2. import org.springframework.beans.factory.annotation.Value;