这篇博客是学习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等一些其他常用框架。