如何使用 Struts2 和 Hibernate 更新数据库中的数据
How to update data in database using Struts2 and Hibernate
您好,我正在使用 Struts2 和休眠更新数据库中的数据。我在 url 中获取 Id 有困难,请帮帮我好吗?
这些是我的代码:
updateUser.jsp
<s:form action="updateList" method="POST">
<s:submit value="Update User"/>
</s:form>
<s:iterator value="contactList" var="contacts">
<s:property value="firstName"/> <s:property value="lastName"/>
 <a href="updateHere.jsp?id=<s:property value="id"</s:property>">Update</a><br/>
</s:iterator>
当我点击 link 更新时。这就是我所看到的。我想在那里获取 ID。
http://localhost:8080/BiddingStrutsEx/updateHere.jsp?id=3
updateHere.jsp
<s:actionerror />
<s:form action="updateU" method="POST">
<s:hidden value="%{id}" name="id"/>
<s:textfield name="contacts.firstName" label="First Name"/>
<s:textfield name="contacts.lastName" label="Last Name"/>
<s:select label="Company" headerValue="Select Your Company"
list="{'Aboitiz Equity Ventures','Aboitz Power','Aboitiz Land'}" name="contacts.company" value="Aboitiz Equity Ventures"/>
<s:select label="Department"
list="{'Computer Service Department','INFRA'}" name="contacts.department" value="Computer Service Department"/>
<s:textfield label="Enter Your Username" name="contacts.username"/>
<s:password label="Enter Your Password" name="contacts.password"/>
<s:textfield label="Enter Your Email" name="contacts.email"/>
<s:checkbox name="contacts.notify" label="Do you want to notify you if someone gets higher bid than your bid?" fieldValue="true"/>
<s:radio label="What kind are you?" name="contacts.kindOfPerson" list="#{'1':'Buyer','2':'Seller','3':'Both'}" value="1"/>
<s:submit value="Update"/>
</s:form>
struts.xml
<action name="updateU" class="net.viralpatel.contact.view.RegistrationAction" method="updateUserEx">
<result name="success">/updateUser.jsp</result>
<result name="error">/updateHere.jsp</result>
<result name="input">/updateHere.jsp</result>
</action>
RegistrationAction.java
假设我已经声明了Id等。
public String updateUserEx() {
if(this.getId().equals(null)) {
addActionError("Fill All!");
return ERROR;
} else {
try {
contacts.setId(this.getId());
contactManager.updateUserEx(contacts);
} catch (Exception e) {
e.printStackTrace();
addActionError("Error brad!");
return ERROR;
}
this.contactList = contactManager.getListContacts();
return SUCCESS;
}
ContactManager.java
public Contacts updateUserEx(Contacts contactsList) {
Session session = HibernateUtil.getSession();
session.beginTransaction();
Contacts contacts = (Contacts) session.get(Contacts.class, contactsList.getId());
contacts.setFirstName(contactsList.getFirstName());
contacts.setLastName(contactsList.getLastName());
contacts.setEmail(contactsList.getEmail());
contacts.setCompany(contactsList.getCompany());
contacts.setDepartment(contactsList.getDepartment());
contacts.setUsername(contactsList.getUsername());
contacts.setPassword(contactsList.getPasswords());
contacts.setNotify(contactsList.isNotify());
contacts.setKindOfPerson(contactsList.getKindOfPerson());
session.update(contacts);
session.getTransaction().commit();
return contacts;
}
您不应该直接调用 JSP。而是创建一个 returns JSP 的操作。在操作 bean 中创建一个 属性 id
来保存由 Struts 填充的参数值。
<action name="updateHere" class="net.viralpatel.contact.view.RegistrationAction" method="updateHere">
<result>updateHere.jsp</result>
</action>
活动中class
private String id;
//public getter and setter
public String updateHere(){
return Aciton.SUCCESS;
}
在JSP中你可以使用Struts锚标签。
<s:property value="firstName"/> <s:property value="lastName"/> <s:a action="updateHere"><s:param name="id" value="%{id}"/>Update</s:a><br/>
href属性中的URL是动作映射生成的,像这样
/BiddingStrutsEx/updateHere.action?id=3
您好,我正在使用 Struts2 和休眠更新数据库中的数据。我在 url 中获取 Id 有困难,请帮帮我好吗?
这些是我的代码:
updateUser.jsp
<s:form action="updateList" method="POST">
<s:submit value="Update User"/>
</s:form>
<s:iterator value="contactList" var="contacts">
<s:property value="firstName"/> <s:property value="lastName"/>
 <a href="updateHere.jsp?id=<s:property value="id"</s:property>">Update</a><br/>
</s:iterator>
当我点击 link 更新时。这就是我所看到的。我想在那里获取 ID。
http://localhost:8080/BiddingStrutsEx/updateHere.jsp?id=3
updateHere.jsp
<s:actionerror />
<s:form action="updateU" method="POST">
<s:hidden value="%{id}" name="id"/>
<s:textfield name="contacts.firstName" label="First Name"/>
<s:textfield name="contacts.lastName" label="Last Name"/>
<s:select label="Company" headerValue="Select Your Company"
list="{'Aboitiz Equity Ventures','Aboitz Power','Aboitiz Land'}" name="contacts.company" value="Aboitiz Equity Ventures"/>
<s:select label="Department"
list="{'Computer Service Department','INFRA'}" name="contacts.department" value="Computer Service Department"/>
<s:textfield label="Enter Your Username" name="contacts.username"/>
<s:password label="Enter Your Password" name="contacts.password"/>
<s:textfield label="Enter Your Email" name="contacts.email"/>
<s:checkbox name="contacts.notify" label="Do you want to notify you if someone gets higher bid than your bid?" fieldValue="true"/>
<s:radio label="What kind are you?" name="contacts.kindOfPerson" list="#{'1':'Buyer','2':'Seller','3':'Both'}" value="1"/>
<s:submit value="Update"/>
</s:form>
struts.xml
<action name="updateU" class="net.viralpatel.contact.view.RegistrationAction" method="updateUserEx">
<result name="success">/updateUser.jsp</result>
<result name="error">/updateHere.jsp</result>
<result name="input">/updateHere.jsp</result>
</action>
RegistrationAction.java
假设我已经声明了Id等。
public String updateUserEx() {
if(this.getId().equals(null)) {
addActionError("Fill All!");
return ERROR;
} else {
try {
contacts.setId(this.getId());
contactManager.updateUserEx(contacts);
} catch (Exception e) {
e.printStackTrace();
addActionError("Error brad!");
return ERROR;
}
this.contactList = contactManager.getListContacts();
return SUCCESS;
}
ContactManager.java
public Contacts updateUserEx(Contacts contactsList) {
Session session = HibernateUtil.getSession();
session.beginTransaction();
Contacts contacts = (Contacts) session.get(Contacts.class, contactsList.getId());
contacts.setFirstName(contactsList.getFirstName());
contacts.setLastName(contactsList.getLastName());
contacts.setEmail(contactsList.getEmail());
contacts.setCompany(contactsList.getCompany());
contacts.setDepartment(contactsList.getDepartment());
contacts.setUsername(contactsList.getUsername());
contacts.setPassword(contactsList.getPasswords());
contacts.setNotify(contactsList.isNotify());
contacts.setKindOfPerson(contactsList.getKindOfPerson());
session.update(contacts);
session.getTransaction().commit();
return contacts;
}
您不应该直接调用 JSP。而是创建一个 returns JSP 的操作。在操作 bean 中创建一个 属性 id
来保存由 Struts 填充的参数值。
<action name="updateHere" class="net.viralpatel.contact.view.RegistrationAction" method="updateHere">
<result>updateHere.jsp</result>
</action>
活动中class
private String id;
//public getter and setter
public String updateHere(){
return Aciton.SUCCESS;
}
在JSP中你可以使用Struts锚标签。
<s:property value="firstName"/> <s:property value="lastName"/> <s:a action="updateHere"><s:param name="id" value="%{id}"/>Update</s:a><br/>
href属性中的URL是动作映射生成的,像这样
/BiddingStrutsEx/updateHere.action?id=3