关于登录逻辑

登录逻辑的实现,具体流程图如下:

在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
33
34
package com.hu.controller;

import com.hu.pojo.admin;
import com.hu.pojo.book_info;
import com.hu.service.BookService;
import com.hu.service.adminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
@SuppressWarnings("all")
@Controller
@RequestMapping("/book")
public class adminController {
@Resource
private adminService adminService;
@Resource
private BookService bookService;

//check user
@RequestMapping("/book_manage_2")
public String login(admin admin, Model model) {
admin = adminService.checkLogin(admin.getAdmin_id(), admin.getPassword());
if (admin != null) {
List<book_info> list = bookService.queryAllBook();
model.addAttribute("list", list);
return "book_manage";
} else {
return "login";
}
}
}

在Service层的核心代码:

1
2
3
4
5
6
7
8
9
10
   @Override
public admin checkLogin(int admin_id, String password) {
admin admin = adminMapper.queryadminById(admin_id);
if(admin!=null&&admin.getPassword().equals(password)){
return admin;
}else{
return null;
}
}
}

Service层删减了部分代码,其余具体代码请看点我白嫖

在login.jsp页面:

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
<form action="${pageContext.request.contextPath}/book/book_manage_2" method="post" class="form-horizontal " role="form">
<div class="form-group">
<label class="col-sm-2 control-label">账号:</label>
<div class="col-sm-10">
<input id="zh" type="text" class="form-control" placeholder="账号" name="admin_id">
</div>
</div>
<div class="form-group">
<label id="pwd" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input id="pwd_real" type="password" class="form-control" placeholder="密码" name="password">
</div>
</div>
<div class="form-group">
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-5">
<button id="login" type="submit" class="btn btn-success"> 登录 </button>
</div>
</div>
</form>
<script type="text/javascript">
$("#login").click(function checkpwdAndzh(){
var name = $("#zh").val();
var pass = $("#pwd_real").val();
if (name == "") {
alert("用户名不能为空");
return false;
} else if (pass == "") {
alert("密码不能为空");
return false;
} else{
return true;
}
});

</script>

login.jsp层删减了部分代码,只留下核心代码。

逻辑如代码所示,只是在实现的过程中碰到了一些棘手的问题。比如,通过ModelAndView向前端传递参数来实现登录成功与否的提示弹窗存在一些目前难以解决的逻辑bug。例如:登陆成功后,依然会弹出登陆失败的提示。碍于技术水平,此问题暂未得到解决,索性移除了弹窗提示。

关于上架图书逻辑

首先来看登陆后,form表单提交的地址:

1
<form action="${pageContext.request.contextPath}/book/book_manage_2" method="post" class="form-horizontal " role="form">

再来看一下配置了/book/book_manage_2的Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Controller
@RequestMapping("/book")
public class adminController {
@Resource
private adminService adminService;
@Resource
private BookService bookService;

//check user
@RequestMapping("/book_manage_2")
public String login(admin admin, Model model) {
admin = adminService.checkLogin(admin.getAdmin_id(), admin.getPassword());
if (admin != null) {
List<book_info> list = bookService.queryAllBook();
model.addAttribute("list", list);
return "book_manage";
} else {
return "login";
}
}
}

登陆成功后,请求地址为:

http://localhost:8080/ssm_build_book_war_exploded/book/book_manage_2

当我们选择上架图书模块时:

请求地址如图所示。

而此路径的配置在book_manage这个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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@Controller
@RequestMapping("/book")

public class Book_manage {
@Resource
private BookService bookService;
@RequestMapping("/book_manage")
public String get_Book_manage(){

return "book_manage";
}
// just return add_book page
@RequestMapping("/add_book")
public String add_book_page(){
return "add_book";
}
// just return delete_book page
@RequestMapping("/delete_book")
public String delete_book_page(){
return "delete_book";
}
//return admin book manage page
@RequestMapping("/admin_overview_book")
public String back_overview(Model model){
List<book_info> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "book_manage";
}
//implement add_book function
@RequestMapping("/real_add_book")
public String add_book(book_info book,Model model){
int check = bookService.addbook(book);
if (check>0){
List<book_info> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "book_manage";
}
return "add_book";
}
//implement delete_book function
@RequestMapping("/real_delete_book")
public String delete_book(String book_id,Model model){
int check = bookService.deleteBookById(Integer.parseInt(book_id));
if (check>0){
List<book_info> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "book_manage";
}
model.addAttribute("check","fail");
return "delete_book";
}
}

上述为图书管理的整体配置(此时更新图书功能还未实现),我们摘出与上架图书有关的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//implement add_book function
@RequestMapping("/real_add_book")
public String add_book(book_info book,Model model){
int check = bookService.addbook(book);
if (check>0){
List<book_info> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "book_manage";
}
return "add_book";
}
// just return add_book page
@RequestMapping("/add_book")
public String add_book_page(){
return "add_book";
}

可以看到,跳转到add_book.jsp的方法有两种。但是这两种方法的作用并不相同。对于第二种方法,其主要的作用就是实现页面跳转,并不包含传参。之所以这么设计,是因为单纯的页面跳转并不需要传递参数,而上架图书功能需要传参,需要通过Service访问数据库。如果将两者统一为一个方法,那么在页面跳转的时候会因为没有携带参数而出现错误。什么时候需要单纯的跳转页面呢?很简单,我们的第一次访问上架图书功能模块,这个跳转,并不需要参数。

上架图书逻辑图如下:

关于下架图书逻辑

下架图书与上架图书的Controller构造基本相同(本身他们也在同一个Controller内)。同样的,需要两个方法,一个实现传参,一个单纯的用于跳转。过多细节不再赘述,具体内容请看代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 //implement delete_book function
@RequestMapping("/real_delete_book")
public String delete_book(String book_id,Model model){
int check = bookService.deleteBookById(Integer.parseInt(book_id));
if (check>0){
List<book_info> list = bookService.queryAllBook();
model.addAttribute("list",list);
return "book_manage";
}
model.addAttribute("check","fail");
return "delete_book";
}
// just return delete_book page
@RequestMapping("/delete_book")
public String delete_book_page(){
return "delete_book";
}