SSM框架下的项目前后端的结合

SSM框架下的项目前后端的结合

对于前后端结合莫非两个步骤:

  • 编写对应的Controller层
  • 编写前端

查询书籍功能的实现

编写controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.dwx.controller;

import com.dwx.pojo.Books;
import com.dwx.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

//此处我们使用注解开发
@Controller
@RequestMapping("/books")
public class BooksController {

@Autowired
private BooksService booksService;


//对于Controller层 他的工作是调用Service层,因此将service作为参数
@RequestMapping("allBooks")
public String queryAllBooks(Model model){
List<Books> books = booksService.queryBooks();
model.addAttribute("books",books);
return "books";

}

}

编写前端代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2022/2/22
Time: 20:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查询书籍页面</title>
<!-- 引入 Bootstrap -->

</head>
<body>
<div>
<div>
<table>
<thead>
<tr>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍数目</th>
<th>书籍细节</th>
</tr>

</thead>
<tbody>
<c:forEach var="book" items="${books}">
<tr>
<td>${book.bookID}</td>
<td>${book.bookName}</td>
<td>${book.bookCounts}</td>
<td>${book.detail}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</body>
</html>

其他的功能都是类似操作,不再进行展示

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信