mardi 20 janvier 2015

How to insert multiple values of checkboxes in a database?

I want to insert checked values into my db, and when I checked ONE checkbox, the inserting is work, but when I checked SOME checkboxes, the inserting is work for first checkbox, others checked values isn't inserting. I think, my solve is create into model List of stores, but I can't do this, because I need a model that I've shown below, can it's possible to do another methods(maybe to do something in controller)? Thank You.


Model: Store



@Entity
@Table(name = "store")
public class Store {
private int id;
private String name;

public Store(){
name = null;
}

public Store(Store store){
name = store.getName();
}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")

@Column(name = "id")
public int getId(){
return id;
}

@Column(name = "name")
public String getName(){
return name;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

private Set<CatalogStore> catalogStores = new HashSet<CatalogStore>(0);
@OneToMany
public Set<CatalogStore> getCatalogStore(){
return this.catalogStores;
}
public void setCatalogStore(Set<CatalogStore> catalogStores){
this.catalogStores = catalogStores;
}
}


Model: CatalogStore



@Entity
@Table(name = "store_catalog")
public class CatalogStore {
private int id;
private String name;

public CatalogStore(){}

public CatalogStore(CatalogStore catalogStore){name = catalogStore.getName();}

@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")

@Column(name = "id")
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Column(name = "name")
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

private Store store;
@ManyToOne
@JoinColumn(name = "idStore")
public Store getStore(){
return store;
}
public void setStore(Store store){
this.store = store;
}
}


Controller:



@RequestMapping(value = "/addGoods", method = RequestMethod.POST)
public ModelAndView addGoodsPost(@ModelAttribute("store") Store store) throws SQLException {
ModelAndView modelAndView = new ModelAndView("redirect:body");

//....

catalogStoreService.addGoods(catalogStore);
return modelAndView;
}


View:



<form:form method="POST" action="/addGoods" commandName="store">
<table>
<tr>
<td>
<form:checkboxes items="${storeList}" itemLabel="name" itemValue="id"
path="id" /></td>
</tr>
<tr>
<td>
<input type="submit" value="Add"/>
</td>
</tr>
</table>
</form:form>




Aucun commentaire:

Enregistrer un commentaire