這篇博客是學習springboot整合各種三方框架后基本的CRUD操作。
1,整合jsp
注意springboot默認是不支持jsp的,所以我們利用springboot集成jsp時,一定要創建成一個war類型項目,同時要引入外部tomcat。
如項目結構:
pom.xml
<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> <groupId>com.sb</groupId> <artifactId>springboot2.0-jsp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <!-- SpringBoot 對lombok 支持 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- SpringBoot web 核心組件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- SpringBoot 外部tomcat支持 --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- springboot-log4j --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.3.8.RELEASE</version> </dependency> <!-- springboot-aop 技術 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies> </project>
User實體類:
package com.springboot.pojo; import lombok.Data; import lombok.extern.slf4j.Slf4j; /** * user實體類 * @author Administrator * */ //該注解表示為當前類生產getter setter方法 是lombok的方法 @Data //該注解表示打印日志信息 @Slf4j public class User { private Long id; private String name; private Integer age; public User() { // TODO Auto-generated constructor stub } public User(Long id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } }
控制層:
package com.springboot.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.springboot.pojo.User; /** * User對象控制層 * @author Administrator * */ @Controller public class UserController { @RequestMapping("/getList") public String getList(Model model){ List<User> list = new ArrayList<User>(); list.add(new User(1L, "東東", 15)); list.add(new User(2L, "南南", 11)); list.add(new User(3L, "西西", 16)); list.add(new User(4L, "貝貝", 12)); model.addAttribute("list", list); return "index"; } @RequestMapping("/getUser") @ResponseBody public String getUser(Long id){ String message = null; if(1==id){ message = "查詢成功"; }else{ message = "找不到指定對象"; } return message; } }
App.java
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
在resources目錄下創建屬性文件application.peroperties(這是springboot默認的配置文件最好不要改名字)
###配置前后綴關系 spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
相信前面這段代碼大家都不默認。
最后
<%@ 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> ${list} </body> </html>
通過App.java中的main函數啟動程序
瀏覽器鍵入http://localhost:8080/getList
至此我們的springboot整合jsp就以及完成了,在接下來我會在此項目的基礎上整合mybatis等一些其他常用框架。