新增 spring boot 整合 servlet 用例

This commit is contained in:
luoxiang
2018-12-30 18:51:34 +08:00
parent c45d1d3b7a
commit 85f4d1a591
32 changed files with 764 additions and 22 deletions

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.heibaiying</groupId>
<artifactId>spring-boot-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-base</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--web 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--lombok 插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--测试相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,15 @@
package com.heibaiying.springbootbase;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootBaseApplication {
// 启动器默认开启包扫描,扫描与主程序所在包及其子包,对于本工程而言 默认扫描 com.heibaiying.springbootbase
public static void main(String[] args) {
SpringApplication.run(SpringBootBaseApplication.class, args);
}
}

View File

@ -0,0 +1,24 @@
package com.heibaiying.springbootbase.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
/**
* @author : heibaiying
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Programmer {
private String name;
private int age;
private float salary;
private LocalDate birthday;
}

View File

@ -0,0 +1,29 @@
package com.heibaiying.springbootbase.controller;
import com.heibaiying.springbootbase.bean.Programmer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
/**
* @author : heibaiying
* @description : 跳转渲染模板引擎 默认模板的存放位置为classpath:templates
*/
@Controller
@RequestMapping("freemarker")
public class FreeMarkerController {
@RequestMapping("show")
private String programmerShow(ModelMap modelMap){
List<Programmer> programmerList=new ArrayList<>();
programmerList.add(new Programmer("xiaoming",12,100000.00f,LocalDate.of(2019,Month.AUGUST,2)));
programmerList.add(new Programmer("xiaohong",23,900000.00f,LocalDate.of(2013,Month.FEBRUARY,2)));
modelMap.addAttribute("programmers",programmerList);
return "markerShow";
}
}

View File

@ -0,0 +1,28 @@
package com.heibaiying.springbootbase.controller;
import com.heibaiying.springbootbase.bean.Programmer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
/**
* @author : heibaiying
* @description : restful 控制器
*/
@RestController
@RequestMapping("restful")
public class RestfulController {
@GetMapping("programmers")
private List<Programmer> getProgrammers() {
List<Programmer> programmers = new ArrayList<>();
programmers.add(new Programmer("xiaoming", 12, 100000.00f, LocalDate.of(2019, Month.AUGUST, 2)));
programmers.add(new Programmer("xiaohong", 23, 900000.00f, LocalDate.of(2013, Month.FEBRUARY, 2)));
return programmers;
}
}

View File

@ -0,0 +1,29 @@
package com.heibaiying.springbootbase.controller;
import com.heibaiying.springbootbase.bean.Programmer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
/**
* @author : heibaiying
* @description : 跳转渲染模板引擎 默认模板的存放位置为classpath:templates
*/
@Controller
@RequestMapping("thymeleaf")
public class ThymeleafController {
@RequestMapping("show")
private String programmerShow(ModelMap modelMap) {
List<Programmer> programmerList = new ArrayList<>();
programmerList.add(new Programmer("xiaoming", 12, 100000.00f, LocalDate.of(2019, Month.AUGUST, 2)));
programmerList.add(new Programmer("xiaohong", 23, 900000.00f, LocalDate.of(2013, Month.FEBRUARY, 2)));
modelMap.addAttribute("programmers", programmerList);
return "leafShow";
}
}

View File

@ -0,0 +1,15 @@
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf模板引擎</title>
</head>
<body>
<ul th:each="programmer:${programmers}">
<li>
姓名:<span th:text="${programmer.name}"></span>
薪水:<span th:text="${programmer.salary}"></span>
</li>
</ul>
</body>
</html>

View File

@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>freemarker模板引擎</title>
</head>
<body>
<ul>
<#list programmers as programmer>
<li>姓名: ${programmer.name} 年龄: ${programmer.age}</li>
</#list>
</ul>
</body>
</html>

View File

@ -0,0 +1,17 @@
package com.heibaiying.springbootbase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootBaseApplicationTests {
@Test
public void contextLoads() {
}
}