页面批量新增

来源:互联网 发布:被猎头找到 知乎 编辑:程序博客网 时间:2024/06/05 08:00

jsp:

      <tr>
                        <th class="center span1">
                        <label><input type="checkbox" id="zcheckbox" /><span class="lbl"></span></label>
                        </th>
                        <th class="center span1">序号 </th>                    
                        <th class="center span2 sort">姓名 <span class="sj"></span></th>
                        <th class="center span2 sort">状态  <span class="sj"></span></th>                                                                                                                
                        <th class="center span2 sort">次数 <span class="sj"></span></th>
                    </tr>



   <c:forEach items="${studentList}" var="v" varStatus="vs">
                    <tr>
                        <td class='center' style="width: 30px;">
                            <label><input type='checkbox' name='ids'  /><span class="lbl"></span></label>
                        </td>
                        <td class='center' style="width: 30px;">${vs.index+1}</td>
                        
                        <td>${v.name}</td>
                        <input type="hidden" name='name${ v.studentId}' id='name' value="${v.id }">
                        <input type="hidden" name='studentId${ v.studentId}' id='studentId' value="${v.studentId }">
                        
                        <td ><select name="status${ v.studentId}" id="status" data-placeholder="请选择状态" style="vertical-align:top;width: 100px;">
                                <option value="1" <c:if test="${pd.status == '1'}">selected="selected"</c:if>>正常出勤</option>
                                <option value="2" <c:if test="${pd.status == '2'}">selected="selected"</c:if>>早退</option>
                                <option value='3' <c:if test="${pd.status == '3'}">selected="selected"</c:if>>迟到</option>
                                <option value='4' <c:if test="${pd.status == '4'}">selected="selected"</c:if>>未到</option>
                                <option value='5' <c:if test="${pd.status == '5'}">selected="selected"</c:if>>请假</option>
                            </select>
                        </td>
                        <td><select name="number${ v.studentId}"  id="number" data-placeholder="请选择考勤次数" style="vertical-align:top;width: 100px;">
                                <option value="1" <c:if test="${pd.number == '1'}">selected="selected"</c:if>>1次</option>
                                <option value="2" <c:if test="${pd.number == '2'}">selected="selected"</c:if>>2次</option>
                                <option value='3' <c:if test="${pd.number == '3'}">selected="selected"</c:if>>3次</option>
                                
                            </select>
                        </td>                                                                                        
                    </tr>
                </c:forEach>

       因为studentId是唯一的,这样避免数据的覆盖

java :   

        /*
     * / 批量新增
     */
    @RequestMapping(value = "saveLast")
    public ModelAndView saveLast(HttpServletRequest request) {
        String classid=request.getParameter("classId");
        ModelAndView mv = this.getModelAndView();
        PageData pd = new PageData();//封装好的
        pd = this.getPageData();//获得前台的值
        try {
            List<String> list = new ArrayList<>();
            Iterator<String> iter = pd.keySet().iterator();
            while (iter.hasNext()) {

                 //获得键key

                String key = iter.next();
                list.add(key);
            }

            Map<String, String> map = new HashMap<>();
            for (String aa : list) {

               //获得其中一类key和value    name=“123123123”   name=“123123122”

                if (aa.substring(0, 4).equals("name")) {
                    map.put(aa.substring(4), "name");
                }
            }

            for(Map.Entry<String, String> m: map.entrySet()){
                String key = m.getKey();
                PageData pData = new PageData();
                //根据key获得value,一条一条插入
                pData.put("id",this.get32UUID());
                pData.put("name",pd.get("name"+key));
                pData.put("studentId",pd.get("studentId"+key));
                pData.put("datatime",datatime);
                pData.put("number",pd.get("number"+key));
                pData.put("status",pd.get("status"+key));
                attendanceService.save(pData);
            }

        } catch (Exception e) {
            // TODO: handle exception
            logger.error(e);
        }

        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }



前台值是通过表单提交传到后台的

原创粉丝点击