前言
暑期线上实训的第五节课。对昨天项目学习的进一步拓展。
昨天项目建立部分的内容点这里。
在昨天项目登录功能的基础上,完善增删改查。
导入界面
将老师提供的界面部分内容全部导入webapp
就成了这样
修改index.jsp的内容
<jsp:forward page="login.jsp"></jsp:forward>
这样之后打开localhost:8080就会跳转到login.jsp了
rebuild项目
修改login.jsp界面的form路径
修改UserController.java部分的路径
import com.zhongruan.bean.User;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
boolean flag = userService.login(user.getUsername(), user.getPassword());
ModelAndView modelAndView=new ModelAndView();
if(flag){
modelAndView.setViewName("main");/*原本为../ok*/
}else {
modelAndView.setViewName("../failer");/*原本为../failure*/
}
return modelAndView;
}
}
顺便,这样修改的话昨天用来测试的ok.jsp和failure.jsp就可以删掉了。
附上此时webapp文件夹截图
试运行
登录界面
因账号密码不匹配而登陆失败
登录成功
功能实现
定位位置
原本计划在点击用户管理时弹出查询界面,所以先在框架jsp文件当中找到用户管理按钮的链接路径
将其修改为/user/findAll.do
findAll方法
在UserController.java下新增加一个名叫findAll的方法,可以通过ctrl+左键从上一步的路径跳转到该方法。
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> userList=userService.findAll();
}
因为没有findAll方法,所以alt+enter创建方法,跳转至IUserService.java接口
public interface IUserService {
boolean login(String username,String password);
List<User> findAll();
}
定位到对应的UserService,生成方法,并修改返回值为findAll,获得全部搜索信息
@Override
public List<User> findAll() {
return userDao.findAll();
}
同样alt+enter创建findAll方法。
手动跳转到UserMapper.xml界面,在这里可以新建方法findAll的实现代码
<select id="findAll" resultType="user">
select * from tb_user
</select>
回到一开始的UserController解决报错问题,修改并建立回传部分的代码
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> userList=userService.findAll();
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("userList",userList);
modelAndView.setViewName("user-list");
return modelAndView;
}
进入user-list.jsp界面,修改界面表单,连接数据库表。
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="#" class="btn bg-olive btn-xs">更新</a>
<a href="#" class="btn bg-olive btn-xs">删除</a>
<a href="#" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
</tbody>
表单界面效果如下
delete方法、add方法和update方法
因为三个方法的实现步骤基本一致,所以就一并操作了。
首先到jsp中写好相关的路径
user-list.jsp
<tbody>
<c:forEach items="${userList}" var="user">
<tr>
<td><input name="ids" type="checkbox"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td> <td class="text-center">
<a href="${pageContext.request.contextPath}/user/toUpdate.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
<a href="${pageContext.request.contextPath}/user/deleteById.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<a href="${pageContext.request.contextPath}/user/add.do?id=${user.id}" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
</tbody>
user-add.jsp
<form action="${pageContext.request.contextPath}/user/add.do"
method="post">
<!-- 正文区域 -->
<section class="content"> <!--产品信息-->
<div class="panel panel-default">
<div class="panel-heading">用户信息</div>
<div class="row data-type">
<%--<div class="col-md-2 title">id</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="id"
placeholder="id" value="">
</div>--%>
<div class="col-md-2 title">用户名称</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="username"
placeholder="用户名称" value="">
</div>
<div class="col-md-2 title">密码</div>
<div class="col-md-4 data">
<input type="password" class="form-control" name="password"
placeholder="密码" value="">
</div>
</div>
</div>
<!--订单信息/--> <!--工具栏-->
<div class="box-tools text-center">
<button type="submit" class="btn bg-maroon">保存</button>
<button type="button" class="btn bg-default"
onclick="history.back(-1);">返回</button>
</div>
<!--工具栏/--> </section>
<!-- 正文区域 /-->
</form>
user-update.jsp
<div class="panel panel-default">
<div class="panel-heading">用户信息</div>
<div class="row data-type">
<div class="col-md-2 title">id</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="id"
placeholder="id" value="${user.id}">
</div>
<div class="col-md-2 title">用户名称</div>
<div class="col-md-4 data">
<input type="text" class="form-control" name="username"
placeholder="用户名称" value="${user.username}">
</div>
<div class="col-md-2 title">密码</div>
<div class="col-md-4 data">
<input type="password" class="form-control" name="password"
placeholder="密码" value="${user.password}">
</div>
</div>
UserController.java中新增的方法
@RequestMapping("/deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("toUpdate.do")
public ModelAndView toUpdate(int id){
User user=userService.selectUserById(id);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("user-update");
modelAndView.addObject("user",user);
return modelAndView;
}
通过alt+enter自动生成在UserDao.java和IUserService.java里的方法
public interface UserDao {
User findUserByUserName(String username);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectById(int id);
}
UserService.java中新增部分
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
最后是补全UserMapper.xml内的逻辑语句,完成功能的实现。
<delete id="deleteById" parameterType="int">
delete from tb_user where id = #{id}
</delete>
<insert id="add" parameterType="user">
insert into tb_user (username,password) values (#{username},#{password})
</insert>
<select id="selectById" parameterType="int" resultType="user">
select * from tb_user where id=#{id}
</select>
至此,表单的删除、新增、更新完成。
效果图
登录后查看表单界面
新增条目(id自增长)
删除操作(“id 6,用户名 lihua“之后)
更新操作