SpringBoot雖然支持JSP,可是官方不推薦使用。看網上說,畢竟JSP是淘汰的技術了,淚奔,剛接觸 就淘汰。。html
SpringBoot集成JSP的方法:java
1.配置application.propertiesgit
# 頁面默認前綴目錄 spring.mvc.view.prefix=/WEB-INF/jsp/ # 響應頁面默認後綴 spring.mvc.view.suffix=.jsp
2.加入依賴spring
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency>
3.控制層建議使用@Controller,不要使用@RestController,畢竟不是每個方法都返回JSON的,有的須要跳轉到界面。apache
4.代碼實現tomcat
1)控制層代碼mvc
/** * ClassName:StudentController * Date: 2017年11月6日 下午4:27:40 * @author Joe * @version * @since JDK 1.8 */ @Controller public class StudentController { /** * view:(跳轉到JSP界面). * @author Joe * Date:2017年11月6日下午4:29:27 * * @param map * @return */ @RequestMapping(value = {"/", "/view"}) public String view(Map<String, Object> map) { map.put("name", "SpringBoot"); map.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); return "index"; } }
2)Jsp代碼app
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 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> <center> <h3> 歡迎 ${name },當前時間:${date } </h3> </center> </body> </html>
3)訪問 http://127.0.0.1:8080/view 便可跳轉到JSPjsp
5.源碼下載ide