复杂的物体从百里香和弹簧靴中丢失

时间:2016-10-29 17:09:53

标签: spring templates spring-boot thymeleaf

我有一个带有nesten复杂对象的Object:

public class MonitoringSystem {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;
    private String url;
    private String username;
    private String password;
    @Transient
    private String passwordConfirm;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name ="anonymization_id")
    private Anonymization anonymization;

当我创建一个新对象或编辑一个现有对象时,我想保留我的匿名对象。为此,我尝试将其保存在<input type="hidden">中,就像我一样保存我的身份。

控制器:

@Controller
public class MonitoringSystemController {

    // some Code

    @RequestMapping("monitoringsystem/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        model.addAttribute("monitoringsystem", monitoringSystemRepository.findOne(id));
        return "monitoringsystem/form";
    }

    @RequestMapping("/monitoringsystem/new")
    public String newMonitoringSystem(Model model) {
        model.addAttribute("monitoringsystem", new MonitoringSystem());

        return "monitoringsystem/form";
    }

    @RequestMapping(value = "/monitoringsystem/save", method = RequestMethod.POST)
    public String save(MonitoringSystem monitoringSystem) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        User user = userRepository.findByUsername(auth.getName());
        long id = user.getCloudProvider().getId();

        anonymizationRepository.save(monitoringSystem.getAnonymization());

        // more code
    }
}

形式:

<form class="form-horizontal" th:modelAttribute="monitoringsystem"
      th:object="${monitoringsystem}" th:action="@{/monitoringsystem/save}" method="post">
  <input type="hidden" th:field="*{id}"/>
  <input type="hidden" th:field="*{anonymization}"/>
  <fieldset>
    <legend>New Monitoring-System</legend>
    <div class="form-group">
      <label class="col-md-4 control-label" for="textinput">Systemname</label>
      <div class="col-md-5">
        <input th:field="*{name}" class="form-control input-md" type="text"/>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-4 control-label" for="textinput">URL</label>
      <div class="col-md-5">
        <input th:field="*{url}" class="form-control input-md" type="text"/>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-4 control-label" for="textinput">Username</label>
      <div class="col-md-5">
        <input th:field="*{username}" class="form-control input-md" type="text"/>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-4 control-label" for="textinput">Password</label>
      <div class="col-md-5">
        <input th:field="*{password}" class="form-control input-md" type="password"/>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-4 control-label" for="textinput">Confirm Password</label>
      <div class="col-md-5">
        <input th:field="*{passwordConfirm}" class="form-control input-md" type="password"/>
      </div>
    </div>
    <div class="form-group">
      <label class="col-md-4 control-label" for="singlebutton"></label>
      <div class="col-md-4">
        <a th:href="@{/monitoringsystem}" class="btn btn-default btn-small">Cancel</a>
        <button id="singlebutton" name="singlebutton" class="btn btn-primary btn-small">
          Submit
        </button>
      </div>
    </div>
  </fieldset>
</form>

不幸的是,这个工作没有用。当我尝试使用monitoringSystem.getAnonymization()在保存方法中获取匿名化对象时,我获得了Nullpointerexception。所以我猜这个对象没有正确地存储在隐藏字段中。如何正确传递对象,以便在创建或编辑过程中不会丢失?

2 个答案:

答案 0 :(得分:1)

您将整个对象绑定到该字段。它应该是

MLSD

答案 1 :(得分:0)

我现在以不同的方式解决了我的问题。我离开了这个隐藏的字段并将我的对象存储在Session中。在save-method中将它们拉出会话并再次将它添加到我的monitoringSystem-object中。这很有效,没有数据丢失。

此外,它更省钱,因为隐藏的字段可以被用户轻松操作。

但是谢谢你的帮助。