Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Struts 2和Hibernate编辑行不起作用_Java_Hibernate_Jsp_Struts2_Ognl - Fatal编程技术网

Java 使用Struts 2和Hibernate编辑行不起作用

Java 使用Struts 2和Hibernate编辑行不起作用,java,hibernate,jsp,struts2,ognl,Java,Hibernate,Jsp,Struts2,Ognl,我无法使用struts2 hibernate编辑表。在Apache控制台中,我可以看到执行了更新查询,但从表中,它只是删除了数据,并在尝试插入数据时插入到新行中,而不是在同一行中。有人能帮我解释一下这个代码有什么问题吗 要编辑的DAO: public static boolean update(trainee p) { Boolean a=false; Session session=new Configuration().configure("hibernate.cfg.xml

我无法使用struts2 hibernate编辑表。在Apache控制台中,我可以看到执行了更新查询,但从表中,它只是删除了数据,并在尝试插入数据时插入到新行中,而不是在同一行中。有人能帮我解释一下这个代码有什么问题吗

要编辑的DAO:

public static boolean update(trainee p)
{
    Boolean a=false;
    Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession(); 
     Transaction tp=session.beginTransaction();  
     trainee u=(trainee) session.get(trainee.class, p.getId());
     if(u!=null)
     {
     u.setAddress(p.getAddress());
     u.setPhone(p.getPhone());
     u.setAge(p.getAge());
     u.setTname(p.getTname());
     u.setGender(p.getGender());
     u.setTechnology(p.getTechnology());
     u.setEmail(p.getEmail());
     session.update(u);
     }
     tp.commit();
     System.out.println("Command successfully executed....");
     session.close();
     if(tp != null){
        a=true;
    }
           return a;
}    
行动类:

public String update()
{
    String x="input";
    trainee u=new trainee();
    u.setId(id);
    u.setAddress(address);
    u.setPhone(phone);
    u.setAge(age);
    u.setTname(tname);
    u.setGender(gender);
    u.setTechnology(technology);
    u.setEmail(email);
    if(RegisterDao.update(u))
        {
        x="success";
        }
    return x;
}
Struts.xml

<action name="update" class="com.ilp.action.taineeAction" method="update">
            <result name="success" type="redirect">TraineeRegistration.jsp</result>
            <result name="input">ViewTrainees.jsp</result> 
</action>

您没有在操作中设置
id
属性。属性设置为hibernate会话中获取的对象的值后,可以使用OGNL将隐藏字段绑定到此属性


当你调用
u.setId(id)该值应该已经存在

您还应该修改代码,以便能够插入新记录

if(p.getId()!=null){
培训生u=(培训生)会话.get(培训生.class,p.getId());
u、 setAddress(p.getAddress());
u、 设置电话(p.getPhone());
u、 设置(p.getAge());
u、 setTname(p.getTname());
u、 setGender(p.getGender());
u、 setTechnology(p.getTechnology());
u、 setEmail(p.getEmail());
更新(u);
}else会话。保存(p);

好的,您的问题是当您在成功打开
TraineerRegistration.jsp时单击更新链接时,您正在打开
TraineerRegistration.jsp
,并且在此jsp中您已经定义了
,所以在填写表单后,当您单击提交按钮时,它将启动与
action=“traineeReg”
相关的新插入操作,并将数据作为新行插入

您可以做的是,您可以再创建一个操作
EDIT
,并在该操作中编写代码以获取所选id的数据,然后将该数据传递给jsp并用现有值填充表单,而不是提交编写代码以更新数据:以下代码将帮助您获取id的特定数据:

EditAction.java

public String edit()
    {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();

        Query q = s.getNamedQuery("findbyid").setInteger("id", getId());
        al = (ArrayList<User>)q.list();

        return"success";
    }
然后将此列表传递给jsp并进行迭代

edit.jsp

<s:form action="update">
    <s:iterator id="lis" value="al">
        <s:hidden name="id" value="%{id}"></s:hidden>
        <s:textfield name="name" label="Name" value="%{name}"></s:textfield>
        <s:textfield name="city" label="City" value="%{city}"></s:textfield>
        <s:textfield name="pin" label="Pincode" value="%{pin}"></s:textfield>
        <s:submit value="Save"/>
    </s:iterator>
</s:form>
您也可以使用
traineregistration.jsp
而不是创建新的jsp
edit.jsp
,但为了简单起见,可以创建新的jsp


希望这有帮助

在这里,我同意您的Id属性,但如果Id为null,那么它应该创建一个新记录,因为她添加了如下的notnull条件。培训生u=(培训生)会话.get(培训生.class,p.getId());如果(u!=null)@Ranjitsinh更新以插入新记录。非常感谢您的回复。我试图根据上面更新的代码修改traineregistration.jsp和DAO类,但仍然得到相同的错误。当我在表格中单击“编辑”时,它将删除所有留下空行的数据,并将插入的新数据添加为新行。我无法找出问题所在。@Neelam Baruah您能提供hibernate cfg配置的示例代码吗?@Ranjitsinh我添加了hibernate.cfg.xml您能显示
view培训生.jsp
?@NaMaN我添加了view培训生.jsp吗
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List Of Trainees Registered</title>
</head>
<body>
<table border=1px>
<tr>
<th>Trainee Name</th>
<th>Email</th>
<th>Gender</th>
<th>Age</th>
<th>Ph No</th>
<th>Address</th>
<th>Technology</th>
<th>Edit</th>
<th>Delete</th>
<s:iterator value="contactList">
    <tr>
        <td><s:property value="tname" /></td>
        <td><s:property value="email" /></td>
        <td><s:property value="gender" /></td>
        <td><s:property value="age" /></td>
        <td><s:property value="phone" /></td>
        <td><s:property value="address" /></td>
        <td><s:property value="technology" /></td>
        <td><a href="update?id=<s:property value="id"/>">edit</a></td>
        <td><a href="delete?id=<s:property value="id"/>">delete</a></td>
    </tr>
</s:iterator>
</table>

<a href="TraineeRegistration.jsp">Add Trainee</a> &nbsp;&nbsp;
</body>
</html>
public String edit()
    {
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();

        Query q = s.getNamedQuery("findbyid").setInteger("id", getId());
        al = (ArrayList<User>)q.list();

        return"success";
    }
<query name="findbyid">
    <![CDATA[from User u where u.id = :id]]>
</query>
<s:form action="update">
    <s:iterator id="lis" value="al">
        <s:hidden name="id" value="%{id}"></s:hidden>
        <s:textfield name="name" label="Name" value="%{name}"></s:textfield>
        <s:textfield name="city" label="City" value="%{city}"></s:textfield>
        <s:textfield name="pin" label="Pincode" value="%{pin}"></s:textfield>
        <s:submit value="Save"/>
    </s:iterator>
</s:form>
<action name="edit" class="com.EditAction" method="edit">
    <result name="success">/edit.jsp</result>
</action>