如何使用Struts2和Hibernate更新数据库中的数据

时间:2016-06-14 02:13:27

标签: hibernate jsp struts2

您好我正在使用Struts2和hibernate更新数据库中的数据。我很难在网址中获取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"/>&emsp;<s:property value="lastName"/>
&emsp;<a href="updateHere.jsp?id=<s:property value="id"</s:property>">Update</a><br/>
</s:iterator>

当我点击链接更新时。这就是我所看到的。我想在那里得到身份。

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;

}

1 个答案:

答案 0 :(得分:0)

你不应该直接调用JSP。而是创建一个返回该JSP的动作。在操作bean中创建一个属性id来保存由Struts填充的参数值。

<action name="updateHere" class="net.viralpatel.contact.view.RegistrationAction" method="updateHere">
  <result>updateHere.jsp</result>
</action>

在动作类

private String id;
//public getter and setter

public String updateHere(){
  return Aciton.SUCCESS;
}

在JSP中,您可以使用Struts锚标记。

<s:property value="firstName"/>&emsp;<s:property value="lastName"/>&emsp;<s:a action="updateHere"><s:param name="id" value="%{id}"/>Update</s:a><br/>

href属性中的URL是从动作映射生成的,类似这样的

/BiddingStrutsEx/updateHere.action?id=3