• springmvc循环列表提交问题


    jsp部分form表单

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

    <c:set var="ctx" value="${pageContext.request.contextPath}"/>          //获取项目根目录并取名为ctx   值为${ctx}

    <script src="${mygd}/static/js/jquery-1.7.2.js" type="text/javascript"></script>    //jquery
    <script src="${mygd}/static/js/jquery.html5uploader.js"
    type="text/javascript"></script>                                                                  //上传文件

    <form method="post" action="${ctx }/supplie/AddSupplie" class="pageForm required-validate" onsubmit="return validateCallback(this, navTabAjaxDone);">
    <div class="pageFormContent" layoutH="56">
    <input type="hidden" id="id" name="a" value="${device.id }"/>       //隐藏id作为插入数据的条件
    <p>
    <label style="text-align: right; 140px;">设备类型:${device.devicetypename }</label>
    <label style="text-align: right; 220px;">上次操作时间:<fmt:formatDate value='${device.materialupdatetime }' type='date' pattern='yyyy-MM-dd HH:mm:ss'/></label>             //取出后台时间并格式化
    </p>
    <table width="400px" height="50px" border="1px" style="margin-left: 50px;">
    <tr>
    <th width="100px" align="center">耗材</th>
    <th width="150px" align="center">重置数量</th>
    <th align="center">使用数量</th>
    <th align="center">剩余数量</th>
    </tr>
    <c:forEach items="${material}" var="ma">       //循环表单部分
    <tr class="materTr">                                         //给tr一个class,js取这个tr并进行循环提交     循环数据部分用each方法
    <td align="center"><input type="hidden" id="consumptivematerialname" name="consumptivematerialname" value="${ma.id}"/>${ma.consumptivematerialname }</td>
    <td align="center"><input type="text" style="100px;height:15px" id="resetnum" name="resetnum"/></td>
    <td align="center">${ma.usedQuantity}</td>
    <td align="center">${ma.quantity-ma.usedQuantity}</td>
    </tr>                                       
    </c:forEach>
    </table>
    <p>
    <label style="text-align: right; 80px;">备注:</label>
    <textarea id="add_text" name="text" style=" 280px; height: 60px; border- 1px;"></textarea>
    </p>
    </div>
    <div class="formBar">
    <ul>
    <li><a class="buttonActive" href="javascript:onclick=saveAdd();" id="btnsumbit"><span>提交</span></a></li>
    <li>
    <div class="button"><div class="buttonContent"><button type="button" class="close">取消</button></div></div>
    </li>
    </ul>
    </div>
    </form>

    jquery部分

    <script type="text/javascript">
    //保存用户信息
    function saveAdd(){
    var saveDataAry=[];    //定义一个数组
    var deviceid=$("#id").val();
    $(".materTr").each(function(){
    var consumptivematerialname = $(this).find("input[name='consumptivematerialname']").val();  //id是唯一的,所以用name来取循环的值
    var resetnum = $(this).find("input[name='resetnum']").val();

    if(resetnum==null||resetnum==""){
    return;
    }
    var jsondata ={"deviceid":deviceid,"materialid":consumptivematerialname,"quantity":resetnum};       //拼接json
    saveDataAry.push(jsondata);          //把循环出来的值放到数组里

    });
    var data = JSON.stringify(saveDataAry);        // //转化为JSON字符串, 与上边参数对应一致 ,可以被@requestbody接收
    $.ajax({                   //循环完成后再用ajax提交,就不会出现多次提交的现象,之前循环一次,提交一次!!!! 
    url:'${ctx}/equipment/addSupplies',
    type:'post',
    cache:false,
    data: data,
    contentType : 'application/json',
    error:function(){
    alertMsg.warn('请按照提示正确填写!');
    },
    success:function(){
    $.pdialog.close("addSupplies");
    navTab.reloadFlag("equipment");
    }
    });
    }
    </script>

    ---------------------------------------------------------------------------------------------------------------------------------------------------------

    controller部分

    // 提交添加耗材
    @RequestMapping(value = "/addSupplies", method = RequestMethod.POST)
    public String addSupplies(HttpServletRequest request, @RequestBody List<DeviceMaterial> list         //list数组来接收循环完成后提交过来的数据  是一个数组
    ) {                                                     //@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象.在前台封装好一个类似SearchDomain的JSON对象,然后调用JSON.stringify(data)将对象转化为JSON字符串,传递至后台即可

    for (DeviceMaterial devicematerial : list) {              //遍历数组    并保存数据
    devicematerial.setUpdatetime(new Date());    
    devicematerialService.Save(devicematerial);

    // 更新时间
    Device device = new Device();
    device.setId(devicematerial.getDeviceid());
    device.setMaterialupdatetime(new Date());
    deviceService.updateDevice(device);
    }

    return "basic/Equipment";
    }

    -----------------------------------------------------------------------------------------------------------------------------------------------

    sql语句显示多表连接

    SELECT
    consumptivematerialName ,m.id
    ,(SELECT SUM(quantity) FROM t_device_material WHERE t_device_material.materialId=m.id) quantity,
    IFNULL((
    SELECT SUM(sumNum) FROM t_detail WHERE t_detail.consumptivematerialId =b.materialId AND t_detail.date >b.updateTime) ,0)usedQuantity
    FROM t_con_material m
    INNER JOIN t_device_type t ON m.devicetypeId=t.id
    INNER JOIN t_device d ON t.id=d.devicetypeId
    LEFT JOIN t_detail a ON a.consumptivematerialId=m.id
    LEFT JOIN t_device_material b ON b.materialId=a.consumptivematerialId AND a.date>b.updateTime
    WHERE d.id=#{id,jdbcType=INTEGER} AND m.status=1
    GROUP BY id

    -------------------------------------------------------------------------------------------------------------------------------------------------

    Service部分除了调用Mapper增删改查方法外,还可以处理业务逻辑

    ---------------------------------------------------------------------------------------------------------------------------------

    更改后的sql语句         子查询

    <select id="selectByid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    SELECT a.id materialid,
    a.consumptivematerialName ,
    IFNULL((SELECT quantity FROM t_device_material WHERE deviceid=#{id,jdbcType=INTEGER} AND t_device_material.materialId = a.id),0) quantity
    ,
    IFNULL(( SELECT SUM(sumNum) FROM t_detail WHERE t_detail.consumptivematerialId =a.id AND t_detail.date >
    (SELECT t_device_material.updateTime FROM t_device_material WHERE deviceid=#{id,jdbcType=INTEGER} AND t_device_material.materialId = a.id)
    ) ,0)usedQuantity

    FROM t_con_material a LEFT JOIN t_device_type b ON a.devicetypeId = b.id AND a.status =1

    WHERE b.id = (SELECT t_device.devicetypeId FROM t_device WHERE t_device.id= #{id,jdbcType=INTEGER} )
    </select>

    当能力支撑不了野心时,就该静下心来学习!
  • 相关阅读:
    QT QT程序初练
    Linux Shell编程三
    Linux Shell编程二
    git操作
    git push命令
    Zabbix全方位告警接入-电话/微信/短信都支持
    CentOS 7安装TigerVNC Server
    MySQL各版本的区别
    MariaDB Galera Cluster 部署
    MySQL高可用方案MHA的部署和原理
  • 原文地址:https://www.cnblogs.com/1234cjq/p/5754201.html
Copyright © 2020-2023  润新知