springboot默认并不支持jsp模板,所以需要配置。
下面是一个可以运行的例子:
首先配置属性文件:
spring.http.encoding.force=truespring.http.encoding.charset=UTF-8spring.http.encoding.enabled=trueserver.tomcat.uri-encoding=UTF-8server.port=8080spring.mvc.view.prefix: /WEB-INF/jsp/spring.mvc.view.suffix: .jspapplication.message: Hello world
然后配置启动类:
package sample.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;@SpringBootApplicationpublic class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}
最后写一下控制层:
package sample.jsp;import java.util.Date;import java.util.Map;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class WelcomeController { @Value("${application.message}") private String message; @GetMapping("/welcome") public String welcome(Mapmodel) { model.put("time", new Date().getTime()); model.put("message", this.message); return "welcome"; } @GetMapping("/test") public String test(){ return "test"; }}
pom.xml 文件的所有配置:
4.0.0 spring-boot-sample-web-jsp ming 0.0.1-SNAPSHOT war Spring Boot Web JSP Sample Spring Boot Web JSP Sample http://projects.spring.io/spring-boot/ Pivotal Software, Inc. http://www.spring.io ${basedir}/../.. / 1.5.4.RELEASE org.springframework.boot spring-boot-starter-web ${springboot.version} javax.servlet jstl 1.2 org.springframework.boot spring-boot-starter-tomcat ${springboot.version} provided org.apache.tomcat.embed tomcat-embed-jasper 8.5.6 provided org.springframework.boot spring-boot-starter-test ${springboot.version} test org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-surefire-plugin false
例子下载: