现在的问题是checkbox只取一个值的时候,能插入到数据库,但取多个值的时候就插不进去了!!!
jsp页面关键代码:
<input type="hidden" value="${paper.paperId}" name="paperchoice.paperId" />
<input type="checkbox" name="paperchoice.choiceId" value="${choice.choiceId}" />
bean层实体类:
PaperChoice
package bean;import java.util.HashSet;
import java.util.Set;/**
* PaperChoice entity. @author MyEclipse Persistence Tools
*/public class PaperChoice implements java.io.Serializable { // Fields private Integer id;
private Integer choiceId;
private Integer paperId;
private Set paperChoices = new HashSet(0); // Constructors
/** default constructor*/
/** minimal constructor */
public PaperChoice(Integer choiceId,Integer paperId){
this.choiceId=choiceId;
this.paperId=paperId;
}
/** full constructor */
public PaperChoice(Integer choiceId,Integer paperId,Set paperChoices){
this.choiceId=choiceId;
this.paperId=paperId;
this.paperChoices=paperChoices;
}
// Property accessors public Integer getChoiceId() {
return choiceId;
} public void setChoiceId(Integer choiceId) {
this.choiceId = choiceId;
} public Integer getPaperId() {
return paperId;
} public void setPaperId(Integer paperId) {
this.paperId = paperId;
} /** default constructor */
public PaperChoice() {
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} }数据库操作:
public boolean addPaperChoice(PaperChoice paperchoice) {
Long i=null; Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.save(paperchoice);
tx.commit();
}
catch (HibernateException e)
{
e.printStackTrace();
if(tx!=null){
tx.rollback();
}
throw e;
}finally{
HibernateSessionFactory.closeSession();
} return true;
}
action:
public String addPaperChoice(){
}
paperService.addPaperChoice(paperchoice);
return "success";
}
jsp页面关键代码:
<input type="hidden" value="${paper.paperId}" name="paperchoice.paperId" />
<input type="checkbox" name="paperchoice.choiceId" value="${choice.choiceId}" />
bean层实体类:
PaperChoice
package bean;import java.util.HashSet;
import java.util.Set;/**
* PaperChoice entity. @author MyEclipse Persistence Tools
*/public class PaperChoice implements java.io.Serializable { // Fields private Integer id;
private Integer choiceId;
private Integer paperId;
private Set paperChoices = new HashSet(0); // Constructors
/** default constructor*/
/** minimal constructor */
public PaperChoice(Integer choiceId,Integer paperId){
this.choiceId=choiceId;
this.paperId=paperId;
}
/** full constructor */
public PaperChoice(Integer choiceId,Integer paperId,Set paperChoices){
this.choiceId=choiceId;
this.paperId=paperId;
this.paperChoices=paperChoices;
}
// Property accessors public Integer getChoiceId() {
return choiceId;
} public void setChoiceId(Integer choiceId) {
this.choiceId = choiceId;
} public Integer getPaperId() {
return paperId;
} public void setPaperId(Integer paperId) {
this.paperId = paperId;
} /** default constructor */
public PaperChoice() {
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} }数据库操作:
public boolean addPaperChoice(PaperChoice paperchoice) {
Long i=null; Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.save(paperchoice);
tx.commit();
}
catch (HibernateException e)
{
e.printStackTrace();
if(tx!=null){
tx.rollback();
}
throw e;
}finally{
HibernateSessionFactory.closeSession();
} return true;
}
action:
public String addPaperChoice(){
}
paperService.addPaperChoice(paperchoice);
return "success";
}
或看下控制台有没有报什么错。
org.hibernate.PropertyValueException: not-null property references a null or tra
nsient value: bean.PaperChoice.choiceId
取多个值时,收到的是一个数组,你再用单值的方法去处理,当然有问题以上只的我猜测你的问题,你贴的代码看不出你在哪里选择了多个checkbox
private Integer choiceId;把这个定义成数组:private Integer[] choiceId;在插入数据库的时候,就需要自己处理了,是用逗号连接还是要怎么,只能靠自己了。
String[] choiceIdArray = request.getParamters("这里放接收表单的name");
上面这句是返回一个数组,存放你checkedbox勾选的多个值。
}这个大括号是怎么回事啊?不会报红色xx么?
paperService.addPaperChoice(paperchoice);
return "success";
}