2011-10-04 66 views
1

我需要在多選表單中選擇多個值:select標籤。我的JSP代碼是Spring MVC 3 <form:Select>標籤多選

<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,渠道(包含元數據)和ServiceChannel。 ServiceChannel包含具有服務註冊和通道表的外鍵引用。所以,一個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; 
    } 

通道實體類... 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.我能夠看到2條記錄在「渠道參與」中。但是,當我將serRegistration設置爲jsp的模型屬性時,在選擇標記中沒有選中。

幫助表示感謝。

回答

4

您需要在實體中使用正確的equals實現(特別是在Channels)。

+0

非常感謝。有用! – Surez

+0

@Surez:隨時接受答案 – Ralph

+0

當我刪除任何服務時如何刪除服務通道條目?到目前爲止,當我刪除服務記錄時,它將刪除通道表記錄,但不應該是因爲它是META表。請幫助。謝謝! – Surez