两节课合起来做了Springmvc的查询和删除,首先是创建包的结构:
UserController中的代码:
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll.do")
public ModelAndView findAll() {
List users = userService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(“allUser.jsp”);
modelAndView.addObject(“users”, users);
return modelAndView;
}
@RequestMapping("/login.do")
public String login(User user) {
Boolean flag = userService.login(user.getUsername(), user.getPassword());
if (flag) {
return “redirect:/findAll.do”;
} else {
return “failure.jsp”;
}
}
@RequestMapping("/delete.do")
public String deleteById(int id) {
userService.deleteById(id);
return “redirect:/findAll.do”;
}
@RequestMapping("/addUser.do")
public String addById(User user) {
userService.addById(user);
return “redirect:/findAll.do”;
}
@RequestMapping("/update.do")
public String update(User user) {
userService.updateById(user);
return “redirect:/findAll.do”;
}
@RequestMapping("/toupdate.do")
public ModelAndView toupdate(User user) {
int id = user.getId();
User users = userService.findById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(“updateUser.jsp”);
modelAndView.addObject(“users”, users);
return modelAndView;
}
}
IUserDao中的代码:
public interface IUserDao {
List findAll();
User selectByUserName(String name);
void deleteById(int id);
void addById(User user);
void updateById(User user);
User findById(int id);
}
UserService中的代码:
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List findAll() {
return userDao.findAll();
}
@Override
public Boolean login(String username, String password) {
User user = userDao.selectByUserName(username);
if (user != null && password.equals(user.getPassword())) {
return true;
}
return false;
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void addById(User user) {
userDao.addById(user);
}
@Override
public void updateById(User user) {
userDao.updateById(user);
}
@Override
public User findById(int id) {
return userDao.findById(id);
}
}
IUserService中的代码:
public interface IUserService {
List findAll();
Boolean login(String username, String password);
void deleteById(int id);
void addById(User user);
void updateById(User user);
User findById(int id);
}
adduser中的代码
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8” isELIgnored=“false”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
基于servlet+jsp框架的管理系统:简单实现增、删、改、查。
iv class=“row clearfix”>
新增用户
用户密码:
创建登录成功和失败还有更新的jsp文件