Spring MVC 前后台传两个相同对象,同样的属性
程序员文章站
2022-06-10 17:47:57
...
package com.ruipengkj.dgxtos.setting.entity; import java.util.ArrayList; import java.util.List; public class SystemSettings { private List<SystemSetting> systemSetting = new ArrayList<SystemSetting>(2); public List<SystemSetting> getSystemSetting() { return systemSetting; } public void setSystemSetting(List<SystemSetting> systemSetting) { this.systemSetting = systemSetting; } }
package com.ruipengkj.dgxtos.setting.web.controller; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.ruipengkj.dgxtos.setting.entity.SystemSetting; import com.ruipengkj.dgxtos.setting.entity.SystemSettings; import com.ruipengkj.dgxtos.setting.service.SettingService; @Controller @RequestMapping(value = "/setting") public class SettingController { @Autowired SettingService settingService; @RequestMapping(value = "/system_setting" ,method = RequestMethod.GET) public void systemSetting(Model model){//把List对象拆了传给页面 List<SystemSetting> itemList = settingService.getSettingList(); for (int i = 0; i < itemList.size(); i++) { SystemSetting systemSetting = itemList.get(i); if(systemSetting.getAttributeName().equals("welcomes")){ model.addAttribute("welcomSetting",systemSetting); } else if(systemSetting.getAttributeName().equals("tablet_password")){ model.addAttribute("passwordSetting",systemSetting); } }} @RequestMapping(value = "/system_setting" ,method = RequestMethod.POST) @ResponseBody public Map<String, Integer> systemSetting(SystemSettings systemSettings){//传入的对象就是新建的类对象 for (SystemSetting systemSetting : systemSettings.getSystemSetting()) {//遍历即可得到原本的对象 this.settingService.editSetting(systemSetting,systemSetting.getId()); } Map<String, Integer> map = new HashMap<String, Integer>(); map.put("result", 1); return map; } }
<form id="submitForm" class="form-horizontal" role="form" action="${contextPath}/setting/system_setting.htm" method="post"> <div class="form-group"> <label class="col-sm-3 control-label">欢迎语</label> <div class="col-sm-5"> <input type="hidden" class="form-control" id="1" name="SystemSetting[0].id" value="${welcomSetting.id}"> <input type="hidden" class="form-control" id="1" name="SystemSetting[0].attributeName" value="welcomes"> <input type="text" class="form-control" id="1" name="SystemSetting[0].attributeValue" value="${welcomSetting.attributeValue}"/> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">平板密码</label> <div class="col-sm-5"> <input type="hidden" class="form-control" id="2" name="SystemSetting[1].id" value="${passwordSetting.id}"> <input type="hidden" class="form-control" id="2" name="SystemSetting[1].attributeName" value="tablet_password"> <input type="text" class="form-control" id="2" name="SystemSetting[1].attributeValue" value="${passwordSetting.attributeValue}" /> </div> <div class="form-group"> <div class="col-sm-offset-6 col-sm-1 return"> <a href="#" class="ajax-link btn btn-primary" style="margin: 0px">返回</a> </div> <div class="col-sm-1 submit"> <button type="submit" class="btn btn-primary btn-label-left">保存</button></div> </div> </form>
上一篇: Map和Select
下一篇: 程序员应该养成的代码习惯