1

我需要在多选表单中选择多个值:选择标签。我的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 可能在 servicechannel 表中映射了多个 channelid。

我的 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;
  }

频道实体类... 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 标记中没有选择任何内容。

帮助表示赞赏,谢谢。

4

1 回答 1

4

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

于 2011-10-04T06:09:45.653 回答