我有一个基本表单,想提交它。但是,我无法实现发布bean的属性,即类的类型。
html:
<!DOCTYPE html>
<html lang="en">
<head th:replace="common/header :: common-header" />
<div th:replace="common/header :: navbar"></div>
<body >
<div class="container" style="margin-top:5%">
<!-- Page Features -->
<form class="form-vertical" th:action="@{/sendDeposit}" method="post" th:object="${deposit}">
<div class="form-group">
<label class="col-md-2 control-label" for="userCredit">Your Credit</label>
<div class="col-md-8">
<input type="text" name="userCredit" class="form-control" id="userCredit"
th:value="${userCredit.amount}" readonly placeholder="Amount" />
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="username">Amount</label>
<div class="col-md-8">
<input type="text" name="amount" class="form-control" id="amount"
th:value="${deposit.amount}" required="required" placeholder="Amount" />
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="username">Sent To</label>
<div class="col-md-8">
<input type="text" name="sentTo" class="form-control" id="depositUsername" th:field="*{sentTo.username}"
th:if="${deposit.sentTo}!=null" th:value="${deposit.sentTo.username}"
required="required" placeholder="username" />
<input type="text" name="sentTo" class="form-control" id="depositUsername" th:field="*{sentTo.username}"
th:if="${deposit.sentTo}==null"
required="required" placeholder="username" />
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="username">Currency</label>
<div class="col-md-8">
<select class="form-control" th:field="*{id}" id="currency">
<option value="-1">Select Currency</option>
<option th:each="currency : ${currency}" th:value="${currency.id}" th:text="${currency.name}"></option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-2"></div>
<div class="col-md-8">
<button type="submit" id="send" class="btn btn-primary">Send</button>
<a class="btn btn-warning" th:href="@{/}">Cancel</a>
</div>
</div>
</form>
</div>
<!-- /.container -->
<div th:replace="common/header :: body-bottom-scripts"></div>
<script>
$("#send").on("click",function(){
$("#depositUsername").val($("#depositUsername").text);
})
</script>
</body>
</html>
域类(bean):
package com.downpayment.domain;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Deposit {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id", nullable = false, updatable = false)
private int id;
private String additionalNote;
private int amount;
private String currency;
private User sentBy;
private User sentTo;
@OneToOne(mappedBy="deposit",cascade=CascadeType.ALL)
private Credit userCurrentCredit;
public String getAdditionalNote() {
return additionalNote;
}
public void setAdditionalNote(String additionalNote) {
this.additionalNote = additionalNote;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public User getSentBy() {
return sentBy;
}
public void setSentBy(User sentBy) {
this.sentBy = sentBy;
}
public User getSentTo() {
return sentTo;
}
public void setSentTo(User sentTo) {
this.sentTo = sentTo;
}
public int getId() {
return id;
}
}
控制器方法:
@RequestMapping(value="/sendDeposit", method=RequestMethod.POST)
public String saveUser(@ModelAttribute Deposit deposit, HttpServletRequest request) {
User sentByUser=userService.findByUsername(deposit.getSentBy().getUsername());
User sentToUser=userService.findByUsername(deposit.getSentTo().getUsername());
Optional<Credit> crSentBy=creditService.findByUser(sentByUser);
Optional<Credit> crSentTo=creditService.findByUser(sentToUser);
float delta=deposit.getAmount();
crSentBy.get().setAmount(crSentBy.get().getAmount()-delta);
crSentTo.get().setAmount(crSentTo.get().getAmount()+delta);
return "send";
}
我该如何绑定到用户类类型的Deposit的sendTo属性?似乎在准备在我的html上制作百里香叶模板时却做错了,但无法意识到我做错了什么。