【问题标题】:Binding child object on submit spring mvc在提交spring mvc上绑定子对象
【发布时间】:2018-01-20 10:50:04
【问题描述】:

我是 Java 新手,所以这个问题看起来很简单。我有一个像这样的模型:

@Entity(name="website")
public class Website {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="websiteId", nullable=false, unique=true)
private long websiteId;
@ManyToOne
@JoinColumn(name = "publisherId")
private Publisher publisher;

public Website() {
}

//... all getter and setter....
}

你看,在 Website 类中,我有一个 Publisher 类型的对象:

@Entity(name="publisher")
public class Publisher {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="publisherId", nullable=false, unique=true)
private long publisherId;
private String publisherName;

@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="publisherId")
private List<Website> listWebsite;

public Publisher() {
}

//...all getter and setter...
}

好的,现在我有一个表单来提交网站类型的整个对象。我刚刚创建了一个下拉列表并让用户选择发布者: 形式:

<form:form action="/LineJavaTest1/website/add" commandName="websiteForm" method="post">
                <form:input path="websiteName" size="30" placeholder="Website Name"/>
                <form:input path="websiteUrl" size="30" placeholder="Website Url"/>
                <form:select path="publisher" multiple="false" size="1">
                    <%--<form:options itemValue="publisherId" itemLabel="publisherName"/>--%>
                    <form:option value="NONE" label="--- Select ---" />
                    <form:options items="${publishers}" itemValue="publisherId" itemLabel="publisherName"/>
                </form:select>
                <form:hidden path="websiteId" size="30" placeholder="Website Id"/>
                <input type="submit" class="btn btn-default" value="Save" />
            </form:form>

您看,我将表单的路径:select 标记设置为“publisher”,itemValue 设置为“publisherId”。发布时,它将publisherId(长类型值)发布到发布对象的发布者属性中。验证将失败,因为它需要 Publisher 类型,而不是 long 类型。

我的问题:如何以正确的方式发布 Website.Publisher.publisherId?

更新:

我在控制器中的添加操作:

@RequestMapping(value = "/add", method = RequestMethod.GET)
public String showForm(ModelMap mm, @ModelAttribute("websiteForm") Website websiteForm) {
    mm.put("websiteForm", new Website());
    mm.put("publishers", publisherService.getAll());
    return "website/add";
}

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(@ModelAttribute("websiteForm") Website websiteForm, BindingResult result, ModelMap mm) {
    websiteValidator.validate(websiteForm, result);
    if (result.hasErrors()) {
        return "error";
    }

    if (websiteForm.getWebsiteId()>0) {
        websiteService.edit(websiteForm);
    }else
    {
        websiteService.add(websiteForm);
    }
    return "redirect:index";
}

我已将 select 标记的路径从 publisher 更改为 publisher.publisherId,如下 Shantaram Tupe 的建议,但表单无法生成为 html。我从publisher更改为publisher.publisherName,一切看起来都很好,表单可以在html中查看,值在publisher.publisherName中回传到服务器。我怎样才能把它发回publisher.publisherId?我的 publisherId 字段的配置似乎有问题?

更新 2

我的问题是只生成 HTML 表单。我尝试在浏览器上编辑生成的 html 表单,例如:

<form.....>
<select id="publisher.publisherName" name="publisher.publisherName" size="1">......</select>
</form>

类似于:

<form.....>
<select id="publisher.publisherId" name="publisher.publisherId" size="1">......</select>
</form>

一切正常。现在,如何使用publisher.publisherId 生成表单?

最后更新

终于找到了。它适用于 PublisherName 但不适用于 PublisherId,因为 PublisherName 是字符串类型。我使用的选择标记中的转储项 (&lt;form:option value="NONE" label="--- Select ---" /&gt;) 具有值 NONE - 它不能是长类型值。改成0,表单生成成功。

【问题讨论】:

  • publisherId 除了NONE 之外还有哪些可能的值?如果都是long 类型,请考虑将NONE 更改为long 类型的其他值。 .

标签: java spring jsp spring-mvc


【解决方案1】:

只需将 &lt;form:select&gt; 标签中的路径从 publisher 更改为 publisher.publisherId

我身边的一些其他事情:

  1. 你不需要使用@Column(name="websiteId", nullable=false, unique=true)

    • 由于数据库中的列名与实体类中的字段名相同
    • 由于它是用@Id注解的,它永远不会null,默认情况下是uniqe
  2. 您不需要在两边都使用@JoinColumn,在@OneToMany 边使用ma​​ppedBy 属性,例如@OneToMany(mappedBy="publisher")

【讨论】:

  • 感谢您在旁边提出的建议,我标记了投票,但更改路径无济于事。起初,我按照您所说的放置路径(publisher.publisherId),但表单甚至无法生成为 html。我将其更改为发布者并成功生成表单。我将使用控制器功能更新我的问题。
  • 嗯....我可能会提供更多的东西。如果我将路径更改为“publisher.publisherName”,则表单将成功生成,数据可以使用 publisherName 的长类型值回传到服务器 - 太棒了。但是,如果我将其改回“publisher.publisherId”,仍然无法生成表单。
  • 我已经用很多东西更新了这个问题,使它更清楚。真的希望你能帮助我摆脱困境。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 2013-01-17
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多