项目结构目录
一、Book类
package com
.ctgu
.zyj
.demo
;
public class Book {
private Integer id
;
private String name
;
private String author
;
@Override
public String
toString() {
return "Book{" +
"id=" + id
+
", name='" + name
+ '\'' +
", author='" + author
+ '\'' +
'}';
}
public Integer
getId() {
return id
;
}
public void setId(Integer id
) {
this.id
= id
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public String
getAuthor() {
return author
;
}
public void setAuthor(String author
) {
this.author
= author
;
}
}
二、BookController类
package com
.ctgu
.zyj
.demo
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.GetMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.ResponseBody
;
import org
.springframework
.web
.bind
.annotation
.RestController
;
import org
.springframework
.web
.servlet
.ModelAndView
;
import java
.util
.ArrayList
;
import java
.util
.List
;
@RestController
public class BookController
{
@ResponseBody
@RequestMapping("/books")
public ModelAndView
books(){
List
<Book> books
= new ArrayList<>();
Book b1
= new Book();
b1
.setId(1);
b1
.setName("三国演义");
b1
.setAuthor("罗贯中");
Book b2
= new Book();
b2
.setId(2);
b2
.setName("红楼梦");
b2
.setAuthor("曹雪芹");
books
.add(b1
);
books
.add(b2
);
ModelAndView mv
= new ModelAndView();
mv
.addObject("books",books
);
mv
.setViewName("books");
return mv
;
}
}
三、Thymeleaf框架部分
<!DOCTYPE html
>
<html lang
="en" xmlns
:th
="http:..www.thymeleaf.org">
<head>
<meta charset
="UTF-8">
<title>图书列表
</title
>
</head
>
<body>
<table border
="1">
<tr>
<td>图书编号
</td
>
<td>图书名称
</td
>
<td>图书作者
</td
>
</tr
>
<tr th
:each
="book:${books}">
<td th
:text
="${book.id}"></td
>
<td th
:text
="${book.name}"></td
>
<td th
:text
="${book.author}"></td
>
</tr
>
</table
>
</body
>
</html
>
运行结果
转载请注明原文地址: https://lol.8miu.com/read-845.html