问题描述
用struts和mybatis做了一个小工程,就是很简单的增删改查,现在要从jsp页面输入Students类的对象student,把这个对象的几个数据添加到mysql的表里。现在的错误集中在insert()这个action里,以下是源代码:jsp主要代码:<body><h3>请依次填入相应信息:</h3><formaction="insert.action"><h4>姓名:<inputname="Name"/></h4><h4>性别:<inputname="Gender"/></h4><h4>专业:<inputname="Major"/></h4><h4>年龄:<inputname="Grade"/></h4><h4>地址:<inputname="Address"/></h4><h4>电话:<inputname="Telephone"/></h4><inputtype="submit"value="提交"/></form><inputtype="button"value="返回主界面"onclick="window.location.href='index.jsp'"/></body>action的代码:publicclassOperationAction{staticStudentsstudent=newStudents();intid;publicstaticStudentsgetStudent(){returnstudent;}publicstaticvoidsetStudent(Studentsstudent){OperationAction.student=student;}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicSqlSessionFactorygetSessionFactory(){Stringresource="yuhantao/configuration.xml";Readerreader=null;try{reader=Resources.getResourceAsReader(resource);}catch(Exceptione){e.printStackTrace();}SqlSessionFactorysessionFactory=newSqlSessionFactoryBuilder().build(reader);returnsessionFactory;}publicStringinsert(Studentsstudent)throwsException{//问题在这里,这个方法不对SqlSessionFactorysessionFactory=getSessionFactory();SqlSessionsession=sessionFactory.openSession();try{session.insert("yuhantao.insertStudent",student);ServletActionContext.getRequest().getSession().setAttribute("student",student);session.close();}catch(Exceptione){e.printStackTrace();}if(select(student.id)=="success"){return"success";}else{return"fail";}}publicStringselect(intid)throwsException{SqlSessionFactorysessionFactory=getSessionFactory();SqlSessionsession=sessionFactory.openSession();try{Studentsstudent=(Students)session.selectOne("yuhantao.selectStudent",id);ServletActionContext.getRequest().getSession().setAttribute("student",student);session.close();}catch(Exceptione){e.printStackTrace();}return"success";}}struts的代码:<struts><packagename="yht"extends="struts-default"><actionname="insert"class="yuhantao.OperationAction"method="insert"><paramname="Name">Name</param><paramname="Gender">Gender</param><paramname="Major">Major</param><paramname="Grade">Grade</param><paramname="Address">Address</param><paramname="Telephone">Telephone</param><resultname="success">/insert_success.jsp</result><resultname="fail">/insertFail.jsp</result></action><actionname="select"class="yuhantao.OperationAction"method="select"><paramname="id">id</param><resultname="success">/select_success.jsp</result><resultname="fail">/selectFail.jsp</result></action></package></struts>求大神给看看,谢谢!
解决方案
解决方案二:
还有mapper:<mappernamespace="yuhantao"><insertid="insertStudent"parameterType="yuhantao.Students"useGeneratedKeys="true"keyProperty="id">insertintostudents_information(name,Gender,Major,Grade,Address,Telephone,time,create_date)values(#{name},#{Gender},#{Major},#{Grade},#{Address},#{Telephone},#{time},#{create_date})</insert><selectid="selectStudent"parameterType="int"resultType="Students">select*fromstudents_informationwhereid=#{id}</select></mapper>
解决方案三:
代码看了一半:提出我的疑问:1、你的问题是什么?错误又是什么?是Student没有传入值?还是某个对象为空?2、如果是对象为空,那就一步步调试。如果是没有传入值,或者Student某个字段传入报错,那就看看是那个字段,如果是int,改为Integer
解决方案四:
关键是报错信息啊这都不贴,不是每个人只看代码都能知道错误的
解决方案五:
action里用的是对象,页面就应该是student.name,student.gender之类的属性。
解决方案六:
如果你用的是struts2框架,可以参考他的ognl机制。http://blog.csdn.net/jelly_8090/article/details/8129529这个例子不知道可否满足你的需求。
解决方案七:
不是很懂mybatis,支持一下
解决方案八:
converter
解决方案九:
用struts的ModelDriven就可以实现了,在input里面的name="student.(这里输入你的名称就行)",action继承ModelDriven<实体类>