简体   繁体   English

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

[英]Editing a row using Struts 2 and Hibernate is not working

I am not able to edit table using struts2 hibernate. 我无法使用struts2休眠模式编辑表格。 In Apache console I can see that the update query is executed but from table its just deleting the data and while trying to insert it getting inserted in a new row not in the same row. 在Apache控制台中,我可以看到执行了更新查询,但是从表中只是删除了数据,并试图将其插入到新行而不是同一行中。 Can anyone please help what is wrong in this code 谁能帮忙这段代码有什么问题

DAO for edit: 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;
}    

Action class: 动作类:

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 在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>

TraineeRegistration.jsp TraineeRegistration.jsp

<s:form action="traineeReg">
<s:textfield label="TraineeName" name="tname"></s:textfield>
<s:textfield label="Email Id" name="email"></s:textfield>
<s:radio name="gender" list="{'Male','Female'}"></s:radio>
<s:textfield label="Age" name="age"></s:textfield>
<s:textfield label="Phone Number" name="phone"></s:textfield>
<s:textarea label="Address" name="address"></s:textarea>
<s:select name="technology" list="{'Java','J2ee','Dot Net','Oracle'}" headerKey="" headerValue="Select" label="SelectTechnology" />
<s:submit/>
</s:form>

Hibernate.cfg.xml 的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC  
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
<hibernate-configuration>  

<session-factory>  
    <property name="hbm2ddl.auto">update</property>  
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/javadb</property>
    <property name="connection.username">root</property>  
    <property name="connection.password">cis@123</property>  
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
    <property name="show_sql">true</property>
<mapping class="com.ilp.bean.trainee"/>  
</session-factory>  

viewtrainee.jsp viewtrainee.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>

You didn't set the id property in the action. 您没有在操作中设置id属性。 Once property is set with the value of the object you get in the hibernate session you can bind the hidden field to this property using OGNL. 将属性设置为您在休眠会话中获得的对象的值后,即可使用OGNL将隐藏字段绑定到此属性。

<s:form action="traineeReg">
<s:hidden name="id" value="%{id}"/>
<s:textfield label="TraineeName" name="tname"></s:textfield>
<s:textfield label="Email Id" name="email"></s:textfield>
<s:radio name="gender" list="{'Male','Female'}"></s:radio>
<s:textfield label="Age" name="age"></s:textfield>
<s:textfield label="Phone Number" name="phone"></s:textfield>
<s:textarea label="Address" name="address"></s:textarea>
<s:select name="technology" list="{'Java','J2ee','Dot Net','Oracle'}" headerKey="" headerValue="Select" label="SelectTechnology" />
<s:submit/>
</s:form>

When you call u.setId(id); 当您调用u.setId(id); the value should be already there. 该值应该已经存在。

You should also modify the code to able to insert a new record. 您还应该修改代码以能够插入新记录。

 if (p.getId() != null) {
  trainee u=(trainee) session.get(trainee.class, p.getId());
  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);
 } else session.save(p);

Okay, Your problem is when you click on update link on success you are opening TraineeRegistration.jsp and in this jsp you have defined <s:form action="traineeReg"> so after filling form when you click on submit button it will fire new insert action associated with action="traineeReg" and it will insert data as new row. 好的,您的问题是,当您单击成功更新链接时,您正在打开TraineeRegistration.jsp并在此jsp中定义了<s:form action="traineeReg">因此在填写表单后单击“提交”按钮将触发新的插入与action="traineeReg"相关联的action="traineeReg" ,它将数据插入为新行。

What you can do is you can create one more action EDIT and in that action write code to fetch data of selected id and than pass that data to jsp and fill form with existing values than on submit write code to update data: Following code will help you to get particular data of ID: 您可以做的是再创建一个动作EDIT并在该动作中编写代码以提取选定ID的数据,然后将该数据传递给jsp并用现有值填充表单,而不是提交写入代码以更新数据:您获得ID的特定数据:

EditAction.java 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";
    }

To use namedquery you need to add following code into your name.hbm.xml after end of class tag </class> . 要使用namedquery您需要在</class>标记后添加以下代码到name.hbm.xml

<query name="findbyid">
    <![CDATA[from User u where u.id = :id]]>
</query>

Than pass this list to jsp and iterate it 比将此列表传递给jsp并对其进行迭代

edit.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>

And than on this update action fire update code which you have already wrote. 并且比此更新操作触发您已经编写的更新代码。 You also need to add edit action entry in struts.xml file: 您还需要在struts.xml文件中添加编辑操作条目:

<action name="edit" class="com.EditAction" method="edit">
    <result name="success">/edit.jsp</result>
</action>

You can also use TraineeRegistration.jsp instead of creating new jsp edit.jsp but sake of simplicity create new jsp. 您也可以使用TraineeRegistration.jsp来代替创建新的jsp edit.jsp但是为了简单起见,可以创建新的jsp。

Hope this helps!!! 希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM