JSP Spring Boot官方不推荐使用,建议使用Thymeleaf。
创建Maven web project 使用STS新建一个Maven Web Project ,项目取名为:spring-boot-jsp
在pom.xml文件添加依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- servlet依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <!-- JSTL--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <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> Jdk编译版本: <build> <finalName>spring-boot-jsp</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> 配置application.properties支持jsp 添加src/main/resources/application.properties内容 # 页面默认前缀目录 spring.mvc.view.prefix=/WEB-INF/jsp/ # 响应页面默认后缀 spring.mvc.view.suffix=.jsp # 自定义属性,可以在Controller中读取 application.hello=Hello cpit From application 编写Controller @Controller public class HelloController { @Value("${application.hello:Hello Angel}") private String hello; @RequestMapping("/helloJsp") public String helloJsp(Map<String, Object> map) { System.out.println("HelloController.helloJsp().hello=" + hello); map.put("hello", hello); return "helloJsp"; } } 编写JSP页面 在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面:hello.jsp <%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${hello} </body> </html> 编写启动类:App.java @SpringBootApplication public class App extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(App.class, args); } }右键Run As Java Application访问:http://127.0.0.1:8080/hello 可以访问到:
热部署: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> 加入siteMesh 引入jar包 <dependency> <groupId>org.sitemesh</groupId> <artifactId>sitemesh</artifactId> <version>3.0.1</version> </dependency> 配置化: @Configuration public class WebConfig extends WebMvcConfigurerAdapter{ @Bean public FilterRegistrationBean siteMeshFilter(){ FilterRegistrationBean fitler = new FilterRegistrationBean(); WebSiteMeshFilter siteMeshFilter = new WebSiteMeshFilter(); fitler.setFilter(siteMeshFilter); return fitler; } } 引入静态资源(css,js,图片….) 添加application.properties内容 spring.resources.static-locations=classpath:/static/ 1.png路径:src/main/resource/static/images/ <img src="/images/1.png" />代码下载:http://download.csdn.net/detail/qq_30364013/9775039