背景
从安装SpringBoot到访问本地localhost看到helloworld,分别用纯SpringBoot和Spring Boot CLI结合Groovy的方式建立一个图书列表管理页面。
安装
brew tap pivotal/tap
brew install springboot
HelloWorld
写脚本
@RestController
class HelloController {
@RequestMapping("/")
def hello() {
return "Hello World"
}
}
跑脚本
spring run HelloController.groovy
看结果
访问 http://localhost:8080/,可以看到,
在本地建立一个图书列表管理页面
纯SpringBoot方法
1. 新建项目导入依赖
选择的IDE 是 IntelliJ IDEA,新建一个项目并且选择自己需要的依赖,这里选择的是以下几个,
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
其中,thymeleaf用于视图,web用于请求,jpa用于数据持久化,h2用于数据库。
2. 开始写代码
(1)定义领域模型
@Entity
public class Book {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String reader;
private String isbn;
private String title;
private String author;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getReader() {
return reader;
}
public void setReader(String reader) {
this.reader = reader;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
(2)定义仓库接口
public interface ReadingListRepository extends JpaRepository<Book, Long> {
List<Book> findByReader(String reader);
}
(3)创建web界面
1. Spring MVC Controller
@Controller
@RequestMapping("/readingList")
public class ReadingListController {
private static final String reader = "craig";
private ReadingListRepository readingListRepository;
@Autowired
public ReadingListController(ReadingListRepository readingListRepository) {
this.readingListRepository = readingListRepository;
}
@RequestMapping(method=RequestMethod.GET)
public String readersBooks(Model model) {
List<Book> readingList = readingListRepository.findByReader(reader);
if (readingList != null) {
model.addAttribute("books", readingList);
}
return "readingList";
}
@RequestMapping(method=RequestMethod.POST)
public String addToReadingList(Book book) {
book.setReader(reader);
readingListRepository.save(book);
return "redirect:/readingList";
}
}
2. html 和 css
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Reading List</title>
<link rel="stylesheet" th:href="@{/style.css}"></link>
</head>
<body>
<h2>Your Reading List</h2>
<div th:unless="${#lists.isEmpty(books)}">
<dl th:each="book : ${books}">
<dt class="bookHeadline">
<span th:text="${book.title}">Title</span> by
<span th:text="${book.author}">Author</span>
(ISBN: <span th:text="${book.isbn}">ISBN</span>)
</dt>
<dd class="bookDescription">
<span th:if="${book.description}"
th:text="${book.description}">Description</span>
<span th:if="${book.description eq null}">
No description available</span>
</dd>
</dl>
</div>
<div th:if="${#lists.isEmpty(books)}">
<p>You have no books in your book list</p>
</div>
<hr/>
<h3>Add a book</h3>
<form method="POST">
<label for="title">Title:</label>
<input type="text" name="title" size="50"></input><br/>
<label for="author">Author:</label>
<input type="text" name="author" size="50"></input><br/>
<label for="isbn">ISBN:</label>
<input type="text" name="isbn" size="15"></input><br/>
<label for="description">Description:</label><br/>
<textarea name="description" cols="80" rows="5"></textarea><br/>
<input type="submit"></input>
</form>
</body>
</html>
body {
background-color: #cccccc;
font-family: arial,helvetica,sans-serif;
}
.bookHeadline {
font-size: 12pt;
font-weight: bold;
}
.bookDescription {
font-size: 10pt;
}
label {
font-weight: bold;
}
(4)主程序入口
@SpringBootApplication
public class ReadingListApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(ReadingListApplication.class, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/readingList");
}
}
(5)项目结构
最终的项目结构如下图所示,
运行代码即可,然后访问 http://localhost:8080/,结果如下图所示,
思维导图总结
最后以一张思维导图总结一下这章的内容。
Spring CLI 结合 Groovy 的方法
1. 设置CLI项目目录结构
将阅读列表项目的目录结构设置成如上所示的样子。
2. 写代码
(1) Book.groovy
class Book {
Long id
String reader
String isbn
String title
String author
String description
}
(2) ReadingListRepository.groovy
interface ReadingListRepository {
List<Book> findByReader(String reader)
void save(Book book)
}
(3)