Thymeleaf + Spring Boot:使用相关对象创建对象(Form& Return)

时间:2017-07-07 18:06:10

标签: java spring thymeleaf

我正在使用Spring Boot和Thymeleaf开发一个应用程序,其中我有一个Package类和一个User类。

套餐类:

@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>

在我将它们之间的对象关联之前,我使用JavaScript的自动完成功能在&#39; user-search&#39;上写入名称,将值插入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?

我一直在看着吸气剂和制定者,但无法找到问题。我也很好奇应该如何在我的控制器上处理返回,但我认为将在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;
}

1 个答案:

答案 0 :(得分:0)

您需要使用班级的正确字段名称,因此请为user切换package_owner或者与评论中的名称相同,您可以执行相反的操作。