正确保存数据库中标记的选定值java Spring MVC

lfapxunr  于 2022-11-15  发布在  Spring
关注(0)|答案(1)|浏览(132)

我需要在Postgres DB中保存每个配置文件的邮件选项。但是,当我尝试这样做时,它会保存选项,当我重新加载页面时,它会回到默认设置。每次我只更改一个选项,其他选项会回到默认设置。据我所知,它每次都会重写所有的值。我该如何修复它?将非常感谢任何帮助。

JSP

<form class="col-12 ${flexwrap} justify-content-center mb-1 black p-0" action="<c:url value="/mailing/save"/>"
     <c:forEach items="${listOfProfiles}" var="profile">
         <div>
             <p>${profile.getName()}<br/> </p>
             <p>Current setting ${profile.getMailOption()}</p>
         </div>
         <div>
             <select name="${profile.id}" onchange="this.form.submit()">
                 <option id="${profile.id}" value="daily">Daily</option>
                 <option  id="${profile.id}" value="every3days">Every 3 Days</option>
                 <option  id="${profile.id}" value="weekly">Weekly</option>
             </select>
         </div>
         <br/>
     </c:forEach>
 </form>

控制器

@Controller
@RequestMapping("/mailing")
public class MailingController {

    @GetMapping("")
    public ModelAndView main() {
        CustomUser user = AuthenticationUtil.getCurrentUser();

        ModelAndView modelAndView = new ModelAndView("mailing");
        System.out.println(user.getProfiles());

        List<String> options = CollectionFactory.createList();
        options.add("Daily");
        options.add("Every 3 days");
        options.add("Weekly");
        modelAndView.addObject("listOfProfiles", user.getProfiles());
        modelAndView.addObject("optionsList", options);
        return modelAndView;
    }

    @GetMapping("/save")
    public String save(HttpServletRequest request){
        Map map = request.getParameterMap();

        for (Object key: map.keySet()) {
            String keyStr = (String)key;
            String[] value = (String[])map.get(keyStr);
            System.out.println("Key" + (String)key + "   :   " + Arrays.toString(value));
            Profile profile = ProfileDao.findById(key);
            profile.updateMail(value[0]);

        }
        return "redirect:/mailing";
    }
}

已更改一个

更改了另一个,首先返回默认值

mzmfm0qo

mzmfm0qo1#

我只是将form标记移到forEach循环中,它就成功了!

相关问题