【问题标题】:while updating specific attributes of an Entity other attributes getting null values在更新实体的特定属性时,其他属性获取空值
【发布时间】:2020-10-10 13:33:01
【问题描述】:

这是我的控制器类,我有新窗口和编辑窗口。在新窗口中假设我有 6 个字段,所以当我填写所有值并单击保存按钮时,值将存储在数据库中,并且在编辑窗口中我有 4 个字段中有 6 个字段我只想编辑这 4 个字段但是当我编辑这 4 个字段时,其他字段获得空值意味着我不想编辑的字段填充了空值,以前的值被空值覆盖

@RequestMapping("/newbin")
public String showNewBinForm(Model model) {
    Bin bin=new Bin();
    model.addAttribute("bin",bin);
    List<Warehouse> warehouseDetails= wservice.listAll();
     model.addAttribute("warehouseDetails",warehouseDetails);
    return "new_bin";
}

@RequestMapping(value = "/savebin", method = RequestMethod.POST)
    public String saveBin(@ModelAttribute("bin") Bin bin) {
    
        service.save(bin);
        return "redirect:binwindow";
    }

@RequestMapping("/editbin/{id}")
public String showEditBinPage(@PathVariable(name = "id") Long id,Model model) {
    Bin bin = service.get(id);
model.addAttribute("bin", bin);
return "edit_bin";
}

服务-

[public class BinService {
         
        @Autowired
        private BinRepository repo;
         
        public List<Bin> listAll() {
            return repo.findAll();
        }
         
        public void save(Bin bin) {
            repo.save(bin);
        }
         
        public Bin get(long id) {
            return repo.findById(id).get();
        }

【问题讨论】:

  • 在 "model.addAttribute("bin", bin);" 处添加断点时,您能否看到正确检索到的对象?线 。你在edit_bin查看页面做什么?我想问题在那里

标签: spring-boot hibernate thymeleaf


【解决方案1】:

我已经找到了这个问题的解决方案,但是如果我们没有更多的字段,它会很长。

解决方案-在编辑页面上,我放置了所有字段,我不想编辑的字段保留为隐藏字段,因此当我们单击新建按钮并填写所有必需的数据时,数据将存储在实体类字段(或数据库),所以如果我们点击编辑页面,我们将我们想要编辑的属性和其他属性作为隐藏字段,所以点击提交按钮,编辑的值在数据库中更新,隐藏字段包含值我们在新窗口页面上为该特定 ID 填写的内容也会被存储。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多