如何在spring boot中实现和存储multiselect下拉值到mysql数据库?

kulphzqa  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(250)

我创建了一个spring引导应用程序,并应用了多选下拉jsp(html页面)。在这里,我想选择多个值,并在表单提交后将这些值存储到mysql数据库中。在实体中,我将该列定义如下:

@Column(columnDefinition = "text")
    private String deliveryslot;

                <div class="col-md-6 col-12">
                  <h6>Standard Delivery Slots</h6>
                    <select class="js-example-basic-multiple" name="deliveryslot" multiple="multiple" id="deliveryslot" 
                        style="width:75%">
                        <option value="9 a.m. - 5 p.m.">9 a.m. - 5 p.m.</option>
                        <option value="5 p.m. - 1 a.m.">5 p.m. - 1 a.m.</option>
                    </select>
                </div>

下面是javascript函数,我在表单提交时将此参数发送给控制器。

<script>
        function abc(){
        var deliveryslot = [], slotlist;
        slotlist = document.getElementById('deliveryslot');
        for(var i = 0 ; i < slotlist.options.length ; i++){
            if(slotlist.options[i].selected ==true){
                deliveryslot.push(slotlist.options[i].value);
            }
        }
        document.getElementById('addpincodeform').submit();
     }
  </script>

下面是我在数据库中设置值的控制器代码。

@RequestMapping(value = { WebUrl.adminaddpincode }, method = { RequestMethod.GET, RequestMethod.POST })
            public ModelAndView adminaddpincode(HttpServletRequest request, HttpSession session, @RequestParam("deliveryslot") String deliveryslot) {
                logger.info("View pincode page is called...");
                try {
                    User loginuser = (User) session.getAttribute("user");
                    Pincode addpincode = new Pincode();
                    addpincode.setDeliveryslot(deliveryslot);
                    addpincode.setCreatedby(loginuser);
                    pincodedao.save(addpincode);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ModelAndView modelAndView = new ModelAndView("redirect:.." +WebUrl.adminviewpincode);;
                return modelAndView;
            }

现在,我只想将这些多个选定的值存储到数据库中,并从数据库中获取这些值,并将它们显示在一个div中。请,任何可能和更快的方法都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题