简单使用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启动。居然能访问了。本人对此不是很明白。有哪位大神知道能够告知一下,在下灰常感谢。