Spring Nested对象在提交表单时变为null

时间:2018-07-22 18:09:43

标签: spring spring-mvc spring-data-jpa thymeleaf spring-form

我正在尝试使用百里香叶发布数据,命令模型具有两个嵌套对象,一个嵌套对象正在获取数据,而另一个嵌套对象是null。我很困惑,尽管使用相同的概念和代码,但幕后发生了什么。

请注意,尽管使用相同的代码,但我正在日志中获取People Object的数据,但没有获取Project Object的数据。请帮忙。

表单摘要。

<form th:fragment="form" method="post" th:object="${expense}" th:action="@{/createexpense}">

<input type="hidden" th:field="*{expenseId}" />
<div class="form-group">
  <label class="col-md-4 control-label" for="textarea">Expense Description</label>
  <div class="col-md-4">                     
    <textarea class="form-control" id="textarea" th:field="*{description}" name="textarea">Expense Description</textarea>
  </div>
</div>

<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">Amount</label>  
  <div class="col-md-4">
  <input id="textinput" name="textinput" type="text" placeholder="Enter Amount" th:field="*{amount}" class="form-control input-md"> 
 </div>
</div>


<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">Project</label>  
  <div class="col-md-4">
  <select class="form-control" id="sel1" th:field="*{project}">
    <option th:each="project: ${projects}" th:value="${{project.projectId}}" th:text="${project.name}">1</option>
  </select>
  </div>
</div>

<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">People</label>  
  <div class="col-md-4">
  <select class="form-control" id="sel1" th:field="*{people}" >
   <option th:each="people: ${peoples}" th:value="${people.id}" th:text="${people.name}">1</option>
  </select>
  </div>
</div>

<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">Expense Type</label>  
  <div class="col-md-4">

  <select class="form-control" id="sel1" th:field="*{expenseType}">
    <option th:each="exp:  ${expenseTypes}" th:value="${exp}" th:text="${exp}">1</option>
  </select>
  </div>
</div>

<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">Create 
Expense</label>  
  <div class="col-md-4">
      <input type="submit" class="btn btn- danger" value="Submit"/
 </div>
</div>

模型Bean类

@Data
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
        allowGetters = true)
@Entity
public class Expense {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long expenseId;

    @Lob
    private String description;

    @OneToOne
    @Cascade(CascadeType.ALL)
    private People people;

    private Double amount;

    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    private Date createdAt;

    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date updatedAt;


    @ManyToOne
    @Cascade(CascadeType.ALL)
    private Project project;

    @Enumerated(EnumType.STRING)
    private ExpenseType expenseType;

    public Expense(String description, Double expense) {
        this.description = description;
        this.amount = expense;
    }

    public Expense() {
    }


    public void setCreatedAt(Date createdAt) {
        this.createdAt = new Date();
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    @PreUpdate
    public void setUpdatedAt() {
        this.updatedAt = new Date();
    }

}

控制器代码段

@PostMapping(path="/createexpense")
    public Expense addExpense(@ModelAttribute("expense") Expense expense ,BindingResult result){

    log.debug(expense.toString());
    expense.setPeople(peopleService.getPeople(expense.getPeople().getId()));
    expense.setProject(flatProjectService.getFlatProjectById(expense.getProject().getProjectId()));
    return expenseService.createExpense(expense);
}

@GetMapping(path="/createexpense")
public ModelAndView addExpense(ModelAndView modal){
    //adding projects list and people list in expense view
    modal.addObject("projects", flatProjectService.getAllProjects());
    modal.addObject("peoples", peopleService.getAllPeoples());
    modal.setViewName("createexpense");
    modal.addObject("expense", new Expense());
    modal.addObject("expenseTypes", ExpenseType.values());
    return modal;
}

错误日志

2018-07-22 18:22:44.010 DEBUG 25500 --- [nio-8080-exec-5] c.d.b.controller.HomeController          : Expense(expenseId=null, description=hello, people=People(id=2, name=Avinash, peopleType=null, phoneNo=9504974665, address=Beside Maa Clinic, expense=[], createdDate=2009-06-01 13:45:30.0, updatedDate=2009-06-01 13:45:30.0), amount=5000.0, createdAt=null, updatedAt=null, project=null, expenseType=PAID)
2018-07-22 18:22:44.090 ERROR 25500 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null

1 个答案:

答案 0 :(得分:0)

查看您的HTML逻辑。我怀疑问题出在Project Loop

您正在定义th:field="*{project}"
但在选项中用作pro

理想情况下应该是这样

<div class="form-group">
  <label class="col-md-4 control-label" for="textinput">Project</label>  
  <div class="col-md-4">
  <select class="form-control" id="sel1" th:field="*{project}">
    <option th:each="project: ${projects}" th:value="${project.projectId}" 
   th:text="${pro.name}">1</option>
  </select>
</div>
</div>
相关问题