我无法使用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>
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
<?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
<%@ 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>
</body>
</html>
答案 0 :(得分:1)
您未在操作中设置id
属性。一旦使用您在hibernate会话中获得的对象的值设置属性,您就可以使用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>
当您致电u.setId(id);
时,该值应该已经存在。
您还应修改代码以插入新记录。
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);
答案 1 :(得分:0)
好的,您的问题是当您点击成功时点击更新链接TraineeRegistration.jsp
并且在此jsp中您定义了<s:form action="traineeReg">
所以在填写表单后单击提交按钮时它将触发新的插入与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";
}
要使用namedquery
,您需要在课程代码name.hbm.xml
结束后在</class>
中添加以下代码。
<query name="findbyid">
<![CDATA[from User u where u.id = :id]]>
</query>
将此列表传递给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>
而不是你已经写过的这个更新动作火更新代码。您还需要在struts.xml
文件中添加编辑操作条目:
<action name="edit" class="com.EditAction" method="edit">
<result name="success">/edit.jsp</result>
</action>
你也可以使用TraineeRegistration.jsp
而不是创建新的jsp edit.jsp
,但为了简单起见,创建新的jsp。
希望这有帮助!!!