如何向action中传一个对象,求帮助!

问题描述

用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<实体类>

时间: 2024-08-01 02:57:17

如何向action中传一个对象,求帮助!的相关文章

使用Java代码如何上传文件到Stuts Action中??

问题描述 使用Java代码如何上传文件到Stuts Action中??求大神..... 问题补充:这里不是使用浏览器上传文件,而是使用Java代码上传... 解决方案 刚好写过直接上代码了调用的地方的代码URL url = new URL(imageHttp); BufferedImage img = ImageIO.read(url); File file = new File(System.currentTimeMillis()+".jpg"); ImageIO.write(img

struts2中文件上传拦截器 是不是不能在多个action中引用???

问题描述 <global-results> <result name="input">/message.jsp</result></global-results><!--定义全局的返回视图--><action name="updateHead" class="updateHead"> <interceptor-ref name="fileUpload"

struct2框架jsp页面传对象参数到action中失败

问题描述 struct2框架jsp页面传对象参数到action中失败 jsp页面对象方式传参进action中,在action中定义了getter.setter方法,但是还是取不到值!结果: 解决方案 set和get方法是你自己写的还是自动生成的,,写的有问题,,setFlower()和getFlower(),,这样才行 解决方案二: 你的name和action中的字段对不上啊页面上写这个flower.goodsId前面有一个子对象是flower 所以action中要有一个子对象 是flower和

java-报错!!!找不到action中welcome方法,求大神们赐教!

问题描述 报错!!!找不到action中welcome方法,求大神们赐教! HTTP Status 500 - The welcome() is not defined in action class com.opensymphony.xwork2.ActionSupport type Exception report message The welcome() is not defined in action class com.opensymphony.xwork2.ActionSuppor

在struts2的action中如何获得上传文件在客户端的路径?

问题描述 在struts2的action中如何获得上传文件在客户端的路径? 解决方案 解决方案二:ServletActionContext.getServletContext().getRealPath("/UploadImages")解决方案三:用js拿到值,然后放到隐藏域里

FileUploadField 选择了一个文件,传到struts2的Action中,显示为null, 是什么问题?

问题描述 我有个formpanel, 里面有个FileUploadField,选择一个文件后,传到struts2的Action中,但是从调试的显示中看到参数没有传过去,响应的结果为null. 我想请大家帮我看看代码怎么改,多谢了 MyDesktop.ScriptWindow = Ext.extend(Ext.app.Module, {    id:'script-win',    init : function(){        this.launcher = {            tex

代码-SSH中Action中向jsp传的list为空值

问题描述 SSH中Action中向jsp传的list为空值 Action代码如下: public List list; public List<Info> getList() { return list; } public void setList(List<Info> list) { this.list = list; } @Resource(name="infoservice") public IInfoService service; public Str

strus2文件上传时遇到的问题,在action中实现了img的set get 方法

问题描述 strus2文件上传时遇到的问题,在action中实现了img的set get 方法 17:10:13,821 WARN OgnlValueStack:60 - Error setting expression 'img' with value '[Ljava.lang.String;@7601fcbd' ognl.MethodFailedException: Method "setImg" failed for object com.action.UploadAction@

s2sh中struts2的页面的值无法传的action中,相当头痛……

问题描述 代码我给大家贴过来,希望大家多费心帮忙看看,谢谢了--===========================================[color=#FF0000]Things.java[/color]___________________________________________package com.lg.bean;import java.util.Date;public class Things { private Integer id; private String