我正在開發一個使用Spring Boot和Thymeleaf的應用程序,其中我有一個Package類和一個User類。Thymeleaf + Spring Boot:使用相關對象創建對象(Form&Return)
軟件包類:
@Entity
@Table(name = "table_package")
public class Package {
@Id
@GenericGenerator(name = "gen_id", strategy = "increment")
@GeneratedValue(generator = "gen_id")
private Integer id;
@ManyToOne
@JoinColumn(name = "userid")
@JsonBackReference
private User package_owner;
// private Integer userid;
private String package_name;
@OneToMany(mappedBy = "pkg")
@JsonBackReference
private Set<Article> articles;
@ManyToOne
@JoinColumn(name = "package_statusid")
@JsonBackReference
private PackageStatus packageStatus;
// private Integer package_statusid;
private String notes;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public User getPackage_owner() {
return package_owner;
}
public void setPackage_owner(User package_owner) {
this.package_owner = package_owner;
}
// public Integer getUserid() {
// return userid;
// }
//
// public void setUserid(Integer userid) {
// this.userid = userid;
// }
public String getPackage_name() {
return package_name;
}
public void setPackage_name(String package_name) {
this.package_name = package_name;
}
public Set<Article> getArticles() {
return articles;
}
public void setArticles(Set<Article> articles) {
this.articles = articles;
}
public PackageStatus getPackageStatus() {
return packageStatus;
}
public void setPackageStatus(PackageStatus packageStatus) {
this.packageStatus = packageStatus;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
@Override
public String toString() {
return "Package{" +
"id=" + id +
", package_owner=" + package_owner +
", package_name='" + package_name + '\'' +
", articles=" + articles +
", packageStatus=" + packageStatus +
", notes='" + notes + '\'' +
'}';
}
}
用戶類別:
@Entity
@Table(name = "table_user")
public class User {
@Id
@GenericGenerator(name="gen_id" , strategy="increment")
@GeneratedValue(generator="gen_id")
private Integer id;
@OneToMany(mappedBy = "package_owner")
@JsonBackReference
private Set<Package> packages = new HashSet<Package>();
}
我編碼包裝畫面中插入
包添加/插入:
<form action="#" th:action="@{/packages/add}" th:object="${Package}" method="post">
<table class="table stripped-table" id="article-table">
<tr>
<th>
User:
</th>
<td>
<input type="text" id="user-search"/>
<input type="text" th:value="*{user.id}" hidden="hidden" th:required="required"/>
</td>
</tr>
<tr>
<th>
Package:
</th>
<td>
<input type="text" th:field="*{package_name}" th:required="required"/>
</td>
</tr>
</table>
<input type="submit" id="submit" value="Add" />
</form>
在我涉及這個目標之前他們之間的CTS我正在使用JavaScript的自動完成在'user-search'上寫下名字,將值插入到user_id輸入中,並將其傳遞給我的控制器。
但現在,我已經創建了兩個對象之間的關係,每次我嘗試加載模板我得到這個錯誤時間:
Invalid property 'user' of bean class [com.app.model.Package]: Bean property 'user' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
我一直在尋找的getter和setter,但可以找不到問題。我也很好奇如何在我的控制器上處理返回值,但我認爲會在DB上的保存功能之前插入整個對象。有任何想法嗎?
在此先感謝。
編輯對不起,我出差了。
這裏的控制器:
@RequestMapping(value = "add", method = RequestMethod.GET)
public ModelAndView addPackageForm(Model model){
List<PackageStatus> packageStatuses = packageStatusRepository.findAll();
model.addAttribute("Package", new Package());
model.addAttribute("PackageStatus", packageStatuses);
ModelAndView mav = new ModelAndView("packages/add");
mav.addObject("page_title", "Test: Add Package");
mav.addObject("template_primary", "packages");
mav.addObject("template_secondary", "add");
return mav;
}
我推薦你MVC模式 –
如果你使用'package_owner'而不是用戶,它是否工作?我的推理是你沒有一個名爲'user'的包,只有'User'字段名'package_owner'。 –
你能告訴我們控制器嗎? –