Spring MVC 3 <form:select> tag multiple selection </form:select>

时间:2011-10-04 05:29:32

标签: java spring-mvc spring-3

我需要在多选形式中选择多个值:select tag。我的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张桌子 - &gt; ServiceRegistration,Channels(包含META数据)和ServiceChannel。 ServiceChannel包含具有serviceregistration和channel表的外键引用。因此,一个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&gt;&gt; 2映射到channelid&gt;&gt; 1和2.我可以在“channelsInvolved”中看到2条记录。但是当我将serRegistration设置为jsp的模型属性时,在select标签中没有选择。

帮助表示感谢,谢谢。

1 个答案:

答案 0 :(得分:4)

您需要在实体中执行正确的equals实施(特别是在Channels中)。