【问题标题】:Spring MVC 3 <form:Select> tag multiple selectionSpring MVC 3 <form:Select> 标签多选
【发布时间】:2011-10-04 05:29:32
【问题描述】:

我需要在多选表单中选择多个值:选择标签。我的jsp代码是

<form:form id="myForm"
action="service.do" modelAttribute="services"
method="POST">
 ....
 ....

<form:select path="channelsInvolved" items="${allChannels}" itemValue="channelid" itemLabel="channelname"> 

我的控制器是...

List<Channels> channels = dao.getAllChannels();
model.addAttribute("allChannels", channels);

ServiceRegistration serRegistration = dao.getById(2);
model.put("services", serRegistration);

实际上,我有 3 个表 -> ServiceRegistration、Channels(包含 META 数据)和 ServiceChannel。 ServiceChannel 包含与 serviceregistration 和通道表的外键引用。所以,一个serviceid可能有多个channelid映射到servicechannel表中。

我的 ServiceRegistration.java 实体类的 channelsInvolved 字段为...

@OneToMany(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
 @JoinTable(name = "ServiceChannel", joinColumns = {
 @JoinColumn(name="serviceid", unique = true) 
 },
 inverseJoinColumns = {
 @JoinColumn(name="channelid")
 }
 )

 private List<Channels> channelsInvolved;

     public List<Channels> getChannelsInvolved() {
    return channelsInvolved;
  }

public void setChannelsInvolved(List<Channels> channelsInvolved) {
    this.channelsInvolved = channelsInvolved;
  }

Channel实体类...Channels.java

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column private int channelid;
@Column private String channelname; 

ServiceChannel.java 实体类...

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column private int servicechannelid;    

@ManyToOne
@JoinColumn(name = "serviceid")
private ServiceRegistration serviceRegistration;

@ManyToOne
@JoinColumn(name = "channelid")
private Channels channels;

说,我有一个服务 id >> 2 映射到 channelid >> 1 和 2。我可以在“channelsInvolved”中看到 2 条记录。但是当我将 serRegistration 设置为 jsp 的模型属性时,在 select 标记中没有选择任何内容。

感谢您的帮助,谢谢。

【问题讨论】:

    标签: java spring-mvc spring-3


    【解决方案1】:

    您需要在实体中正确实现equals(尤其是在Channels 中)。

    【讨论】:

    • 删除任何服务时如何删除servicechannel条目?截至目前,当我删除服务记录时,它正在删除 Channel 表记录,但它不应该是因为它是 META 表。请帮忙。谢谢!
    • @Surez:创建一个新问题来询问删除的内容!
    • @Ralph 谢谢,你节省了我的时间,搜索了几个小时,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多