spring - thymeleaf 和 spring-boot 以形式丢失的复杂对象

标签 spring templates spring-boot thymeleaf

我有一个对象,里面有一个嵌套的复杂对象:

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 :

@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 .所以我猜对象没有正确存储在隐藏字段中。如何正确传递对象,使其在创建或编辑过程中不丢失?

最佳答案

您将整个对象绑定(bind)到该字段。应该是

<input type="hidden" th:field="*{anonymization.id}"/>

关于spring - thymeleaf 和 spring-boot 以形式丢失的复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40321558/

相关文章:

java - 带唯一文件名的 Spring Boot YAML 配置

java - Spring Mail生成空指针异常

java - 从数据库字段声明新路线

java - Spring @Retryable 不工作

spring - 如何重新启动失败的 Spring 批处理作业并让它从停止的地方开始?

templates - azure 资源模板: Can I assign complex variable to property?

spring-boot - Swagger 中的 @ApiOperation 与 @ApiResponse

c++ - 创建模板类的 std::vector ?

c++ - lambda capture 是否支持可变模板参数

java - 使用 Spring Data REST 的自定义 Controller 隐藏默认端点