Maven项目学习(四)SSM项目利用《form:form》标签在Mysql数据库中实现添加和修改数据

       近期自己搞好了系统的添加、修改、条件查询的功能了,折腾了几天,主要是添加修改的业务判断比较多,导致那个负责修改的控制器代码加注释都170多行,真第一次遇见这种情况。最后还是能实现对应的功能了,只是代码看起来十分难看,if、else判断特别多,自己编码水平还能提高吧。

一、添加功能

1.准备好前端的页面,我的如下:

1.users_list.jsp:

部分代码

<div id="list_mean_div">
    <form id="searchForm" name="searchForm" action="/user/search" method="post">
        <div id="list_div_top"></div>
        <a id="name_a"><b>名字:</b></a>
        <input id="name_text" type="text" name="name" maxlength="20"/>
        <a id="class_a"><b>用户类型:</b></a>
        <select id="class_select" name="userClass">
            <option value="0">无</option>
            <option value="1">超级管理员</option>
            <option value="2">房东管理员</option>
            <option value="3">现任租客</option>
            <option value="4">前任租客</option>
        </select>
        <a id="house_a"><b>房号:</b></a>
        <select id="house_select" name="houseId">
            <%--selected:默认选择该选项;--%>
            <%--disabled:该选项不能被鼠标选择;(注:选项没有被隐藏的时候)--%>
            <%--style="display:none":隐藏该选项;(注:该选项不会出现在下拉列)--%>
            <%--value="":该选项value为“”;(注:可做表单验证)--%>
            <option selected disabled style="display:none" value="">选择房号</option>
            <c:forEach items="${houseList}" var="hlist">
                <option value="${hlist.houseId}">&nbsp;&nbsp;&nbsp;&nbsp;${hlist.houseId}</option>
            </c:forEach>
        </select>
        <button id="search_button" type="submit">搜索</button>
    </form>
    <button id="insert_button" onclick="javascript:window.location.href='/user/to_add';">添加人员</button>
    <br/><br/><br/>
    <hr/>
</div>

2.users_add.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<jsp:include page="top.jsp"></jsp:include>
<section id="right">
    <a id="userAdd_local">
        基本管理&nbsp;&nbsp;&nbsp;->&nbsp;&nbsp;人员管理&nbsp;&nbsp;->&nbsp;&nbsp;添加用户
    </a>
    <div id="user_add_div">
        <form:form id="useaAddForm" name="addForm" action="/user/userAdd" method="post" modelAttribute="user">
            <a id="useradd_acc_a">登录账号</a>
            <form:input path="acc" id="useradd_acctext" placeholder="账号..." maxlength="11" onkeyup="this.value=this.value.replace(/[^\w\.\/]/ig,'')"/>
            <a id="acc_star">*</a>
            <%--<a id="acc_detail">账号不能为空!</a>--%>

            <a id="useradd_name_a">姓名</a>
            <form:input path="name" placeholder="姓名..." maxlength="20" id="useradd_nametext"/>
            <a id="name_star">*</a>
            <%--<a id="name_detail">名字不能为空!</a>--%>

            <a id="useradd_userclass_a">用户类型</a>
            <form:select path="userClass" items="${clist}" itemValue="classId" itemLabel="className" id="useradd_class_select"/>

            <a id="userclass_star">*</a>
            <%--<a id="userclass_detail">请选择用户类型!</a>--%>
            <a id="useradd_houseid_a">房间号</a>
            <form:select path="houseId" items="${hlist}" itemValue="id" itemLabel="houseId" id="useradd_houseid_select"/>

            <a id="useradd_houseid_star">*</a>
            <%--<a id="useradd_houseid_detail">请选择房间号!</a>--%>
            <a id="useradd_ruzhutime_a">入住时间</a>
            <div class="layui-input-inline" >
                <form:input path="ruzhuTime" placeholder="yyyy-MM-dd" cssClass="layui-input" id="useraddtest1"/>

            </div>
            <a id="useradd_ruzhutime_star">*</a>
            <%--<a id="useradd_ruzhutime_detail">不能为空!</a>--%>
            <a id="useradd_outtime_a">搬出时间</a>
            <div class="layui-input-inline" >
                <form:input path="outTime" placeholder="yyyy-MM-dd" cssClass="layui-input" id="useraddtest2"/>

            </div>
            <%--<a id="useradd_outtime_detail">搬出时间不能在搬入时间前!</a>--%>
            <button id="submit_btn" type="submit">添加</button>
            <c:if test="${not empty error1}">
                <a id="useradd_error1"><c:out value="${error1}" /></a>
            </c:if>
        </form:form>
        <button id="return_btn" onclick="javascript:window.history.go(-1)">返回</button>
    </div>
</section>
<jsp:include page="buttom.jsp"></jsp:include>

知识点:

<form:form>标签除了有action的路径和method请求的类型外还必需要有modelAttribute属性。modelAttribute的属性值对应的是要添加的数据的实体类,我添加的是用户这个实体类,实体类的名称是:User。在你点击正式的添加按钮提交到后台的时候,spring会自动帮你封装实体的数据的,但是需要你在输入框的属性中添加path属性,path属性中的值就是对应实体类的某个字段。不过在你没有跳转到添加用户的界面之前,你必须要在路由控制器中添加一个User对象到添加用户的界面,否则会报错!

2.控制器层让页面实现跳转

在users_list.jsp这个页面的添加按钮的onclick提交的是:/user/to_add,这个路由

onclick="javascript:window.location.href='/user/to_add';"

我的是UserController这个类进行接收请求的,对应的路由如下。

@RequestMapping(value = "/to_add")
public String toAdd(Map<String,Object> map){
    map.put("user",new User());
    map.put("clist",userClassBiz.selectAll());
    map.put("hlist",houseBiz.selectAllHouse());
    return "users_add";
}

在这个类的前面我是添加了路由的,所以路由没问题。

@RequestMapping(value = "/user")
public class UserController {

我在添加用户界面中添加了user对象,用户的类型列表和房间号列表,用于显示数据。

3.最后的添加操作

userAdd方法:

@RequestMapping(value = "/userAdd")
public ModelAndView userAdd(User user){
    ModelAndView modelAndView = new ModelAndView();
    if (!user.getAcc().equals("")&&!user.getName().equals("")&&!user.getHouseId().equals("")&&user.getUserClass()!=0&&user.getRuzhuTime()!=null){
        /**
         * 1.判断账号是否独一无二
         */
        String acc = user.getAcc();
        if (userService.selectByAcc(acc)==null){
            /**
             * 2.获取用户选择的房间号名称,如果用户类型选择的是现任租客,则不能选择已入住的房间。
             */
            //获取option的value值
            String houseId=user.getHouseId();
            //通过value值知道房号的对象,获得名称值
            House house=houseBiz.selectByHouseId(houseId);
            String houseIdName = house.getHouseId();
            user.setHouseId(houseIdName);
            //获取用户选择的房间号名称
            if(user.getUserClass()==3){
                //先判断搬出时间是否为空
                if (user.getOutTime()==null){
                    //如果用户的类型是现任租客,则获取用户选择的房间号,在数据库查找房间号是否已入住
                    if (houseBiz.searchByBookerId(user.getHouseId())!=null){
                        //1.添加用户
                        userService.add(user);
                        //2.修改房间入住的状态
                        User user1 = userService.selectByAcc(user.getAcc());
                        house.setBookerId(user1.getId());
                        houseBiz.editTheBookerId(house);
                        modelAndView.setViewName("redirect:listAll");
                        return modelAndView;
                    }else{
                        modelAndView.setViewName("redirect:to_add");
                        modelAndView.addObject("error1","该房间已被入住!");
                        return modelAndView;
                    }
                }else{
                    //搬出时间必须为空
                    modelAndView.setViewName("redirect:to_add");
                    modelAndView.addObject("error1","现任租客的搬出时间必须为空!");
                    return modelAndView;
                }
            }else if(user.getUserClass()==4){
                //如果添加的是前任租客,则搬出时间不能为空
                if (user.getRuzhuTime()!=null){
                    userService.add(user);
                    modelAndView.setViewName("redirect:listAll");
                    return modelAndView;
                }else{
                    //搬出时间不能为空
                    modelAndView.setViewName("redirect:to_add");
                    modelAndView.addObject("error1","现任租客的搬出时间不能为空!");
                    return modelAndView;
                }
            }else if (user.getUserClass()==1){
                //如果添加的是超级管理员,则暂时不开通此功能
                modelAndView.setViewName("redirect:to_add");
                modelAndView.addObject("error1","暂时不能添加超级管理员用户");
                return modelAndView;
            }
            else if (user.getUserClass()==2){
                //如果添加的是房间管理员,则房间号为“无”
                user.setHouseId("无");
                user.setOutTime(null);
                userService.add(user);
                modelAndView.setViewName("redirect:listAll");
                return modelAndView;
            }
            modelAndView.setViewName("redirect:listAll");
            return modelAndView;
        }else {
            modelAndView.setViewName("redirect:to_add");
            modelAndView.addObject("error1","该账号已存在!");
            return modelAndView;
        }
    }else{
        modelAndView.setViewName("redirect:to_add");
        modelAndView.addObject("error1","所有带*号的选项都要选择!");
        return modelAndView;
    }
}

如果你的业务逻辑正确,sql语句没写错,基本上点击添加按钮就直接添加成功,并且返回用户列表了。

二、修改功能

修改功能跟添加类似,无非就是业务处理不同,这里就不详细说了,修改的时候需要知道数据的id号,我的话是用用户的账号为标识,因为账号是唯一的。

<a href="/user/to_update?acc=${list.acc}" id="update_a">修改&nbsp;&nbsp;</a>

因为服务端接收要acc这个参数,则在控制器对应的路由添加params为acc

@RequestMapping(value = "/to_update",params = "acc")
public String toUpdate(String acc,Map<String,Object> map){
    map.put("hlist",houseBiz.selectAllHouse());
    map.put("user",userService.selectByAcc(acc));
    map.put("clist",userClassBiz.selectAll());
    return "users_update";
}

因为获取到账号了,则在修改的路由方法里获取这个账号的信息,并对接收过来的用户提交信息进行对比进行业务处理就OK啦。

update方法参数写个user对象,就能获取前端提交过来的数据了:

@RequestMapping(value = "/update")
public ModelAndView update(User user){

  .....省略.......

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章