我无法使用 Struts 2 和 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>
您没有在操作中设置
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);
好吧,您的问题是,当您单击成功时的更新链接时,您将打开
TraineeRegistration.jsp
并且在此 jsp 中您已定义 <s:form action="traineeReg">
,因此在填写表单后,当您单击提交按钮时,它将触发与 action="traineeReg"
相关的新插入操作
它会将数据作为新行插入。
您可以做的是您可以再创建一个操作
EDIT
,并在该操作中编写代码来获取所选ID的数据,然后将该数据传递给jsp并使用现有值填充表单,而不是提交编写代码来更新数据:以下代码将帮助您获取ID的特定数据:
编辑操作.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 并迭代它
编辑.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。
希望这有帮助!!!