Spring Boot整合檢視層技術之JSP
阿新 • • 發佈:2018-12-10
Spring Boot整合JSP
環境:Eclipse Photon Release (4.8.0)
Maven3.3.3
jdk1.8
Spring Boot 1.5.15.RELEASE
1. 搭建專案 新建一個maven專案,打包型別為jar,在專案的結構的src\main中新建目錄webapp\WEB-INF,用來存放JSP檔案。其中jsp檔案存放位置為WEB-INF\jsp\目錄下。 2. pom檔案匯入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.15.RELEASE</version> </parent> <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> <!-- spring boot的web啟動器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- jstl標籤依賴 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- jsp引擎 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> </dependencies>
3. 編寫Controller和實體POJO
/** * 實體類User */ public class User implements Serializable { private static final long serialVersionUID = 5549535290224286007L; private Integer userid; private String username; private Integer userage; public User() { super(); } public User(Integer userid, String username, Integer userage) { super(); this.userid = userid; this.username = username; this.userage = userage; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getUserage() { return userage; } public void setUserage(Integer userage) { this.userage = userage; } } /** * 控制器 */ @Controller public class UserController { @RequestMapping("/showUser") public String showUser(Model model) { List<User> list = new ArrayList<>(); list.add(new User(1, "xiaoming", 19)); list.add(new User(2, "xiaohong", 18)); list.add(new User(3, "xiaoli", 17)); model.addAttribute("list", list); return "userList"; } }
4. 新增SpringBoot配置檔案application.properties 在配置檔案中加入SpringMVC檢視解析器的字首和字尾路徑
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
5. 編寫啟動類,並啟動測試
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
6. 測試結果展示