簡單使用springboot建立工程html
版本:1.5.8java
開發工具:idea 2017.1.4git
spring-boot 對模板引擎的支持有:github
可是官方不建議實用jsp:web
![]() |
JSPs should be avoided if possible, there are several known limitations when using them with embedded servlet containers.spring |
而且實用jsp時會有限制,須要特殊配置。可是既然支持jsp,因此就專門針對jsp作一下demo。apache
個人項目結構以下圖:tomcat
而後看一下pom文件:springboot
<?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> <parent> <groupId>com.songhaiqiang.springboot-example</groupId> <artifactId>springboot-parent</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.songhaiqiang.springboot.web.jsp</groupId> <artifactId>springboot-web-jsp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot-web-jsp</name> <description>Demo project for Spring Boot</description> <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> <!-- Compile --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- Provided --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!--<scope>provided</scope>--> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <useSystemClassLoader>false</useSystemClassLoader> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>java8</id> <activation> <jdk>8</jdk> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
這裏須要注意的是加上如下依賴:mvc
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
而且package個格式不能爲jar 必定要爲war.
二、application.properties須要加入的是
server.port=80 server.address=localhost spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp application.hello=Hello JACK
版本不同的話可能寫法不同,因此我在本文開頭就強調了spring-boot的版本爲1.5.8(老版本寫法不太同樣)。
三、用用程序入口代碼
package com.songhaiqiang.springboot.web.jsp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; @SpringBootApplication public class SpringbootWebJspApplication /*extends SpringBootServletInitializer*/{ // @Override // protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { // return application.sources(SpringbootWebJspApplication.class); // } public static void main(String[] args) { SpringApplication.run(SpringbootWebJspApplication.class, args); } }
這裏若是繼承了SpringBootServletInitializer並重寫其配置方法。這將能使用Spring框架的servlet 3支持,並容許您在servlet容器啓動時配置應用程序。
四、後臺代碼
package com.songhaiqiang.springboot.web.jsp.web; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; /** * Created by songhaiqiang on 2017/10/23. */ @Controller public class LoginController { @Value("${application.hello:Hello MYB}") private String hello; @RequestMapping(value = {"/index"}) public ModelAndView index(){ ModelAndView mv = new ModelAndView("welcome"); mv.addObject("welcome" , this.hello); return mv; } }
這裏不作贅述。
四、頁面代碼
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Title</title> </head> <body> ${welcome} </body> </html>
作到這一步,已經大功告成。理想下應該能啓動了。
but當我在main方法啓動程序後,訪問頁面的時候卻報404錯誤。更加詭異的是在後臺打上斷點,能夠進去。可是返回頁面的時候報404。以後我用@RestController註釋類的話,是能夠用的,惟一使用@Controller時會返回404。我仔細看下後臺返回路勁是根本沒有問題的。百思不得其解以後,用maven裏面的spring-boot:run啓動。居然能訪問了。本人對此不是很明白。有哪位大神知道能夠告知一下,在下灰常感謝。