Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate Data via Struts 2, Hibernate
    primarykey
    data
    text
    <p>I want to update user details on clicking the corresponding edit button in <code>home.jsp</code>. The fields in <code>edit.jsp</code> can be populated by the retrieved values. But on clicking save button in <code>edit.jsp</code>, it shows null pointer exception. </p> <p>I think the update action is not being called. Any help is appreciated.</p> <p>home.jsp</p> <pre><code> &lt;s:property value="#session.userid"/&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Email&lt;/th&gt; &lt;th&gt;Address&lt;/th&gt; &lt;th&gt;Phone No&lt;/th&gt; &lt;th&gt;Actions&lt;/th&gt; &lt;/tr&gt; &lt;s:iterator value="users"&gt; &lt;tr&gt; &lt;td&gt;&lt;a href=""&gt;&lt;s:property value="name"/&gt;&lt;/a&gt;&lt;/td&gt; &lt;td&gt;&lt;s:property value="email"/&gt;&lt;/td&gt; &lt;td&gt;&lt;s:property value="address"/&gt;&lt;/td&gt; &lt;td&gt;&lt;s:property value="phno"/&gt;&lt;/td&gt; &lt;td&gt;&lt;a href="edit?id=&lt;s:property value="id"/&gt;"&gt;Edit&lt;/a&gt;&amp;nbsp;|&amp;nbsp; &lt;a href="delete?id=&lt;s:property value="id"/&gt;"&gt;Delete&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/s:iterator&gt; &lt;/table&gt; </code></pre> <p>edit.jsp</p> <pre><code> &lt;s:form action="update"&gt; &lt;table&gt; &lt;s:hidden name="id" value="%{u.id}"/&gt; &lt;s:textfield name="name" label="Name" value="%{u.name}"/&gt; &lt;s:textfield name="pwd" label="Password" value="%{u.pwd}"/&gt; &lt;s:textfield name="email" label="Email" value="%{u.email}"/&gt; &lt;s:textarea name="address" label="Address" rows="3" cols="14" value="%{u.address}"/&gt; &lt;s:textfield name="phno" label="Mobile" value="%{u.phno}"/&gt; &lt;%--&lt;s:select label="Select Date of Month" name="months" headerKey="0" headerValue="--Select--" list="allMonths" listKey="id" listValue="name"/&gt;--%&gt; &lt;s:select label="Select Date of Month" name="mid" headerKey="0" headerValue="--Select--" list="months1" value="%{u.mid}"/&gt; &lt;s:submit value="Save"/&gt; &lt;/table&gt; &lt;/s:form&gt; </code></pre> <p>UserAction</p> <pre><code> public class UserAction extends ActionSupport{ String name, pwd, email, address;//, months; int phno, id; UserDao udao = new UserDao(); public void setId(int id) { this.id = id; } public int getId() { return id; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPhno() { return phno; } public void setPhno(int phno) { this.phno = phno; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String updateUser() { u.setId(id); u.setName(name); u.setEmail(email); u.setAddress(address); u.setPhno(phno); u.setPwd(pwd); u.setMonths(mid); udao.updateUserInfo(u); return SUCCESS; } } </code></pre> <p>User</p> <pre><code> @Entity @Table(name="tbl_user") public class User { @Id @GeneratedValue @Column(name="user_id") private int id; public void setId(int id) { this.id = id; } public int getId() { return id; } @Column(name="user_phno") int phno; @Column(name="user_name") private String name; @Column(name="user_pwd") private String pwd; @Column(name="user_email") private String email; @Column(name="user_address") private String address; @Column(name="user_bmnth") private int months; public int getMonths() { return months; } public void setMonths(int months) { this.months = months; } public User(){} public User(String name,String pwd,String email,String address,int phno,int months){ this.name = name; this.pwd = pwd; this.email = email; this.address =address; this.phno = phno; this.months= months; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPhno() { return phno; } public void setPhno(int phno) { this.phno = phno; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } } </code></pre> <p>UserDao</p> <pre><code> public class UserDao { public Session getSession() { return HibernateUtil.getSession(); } public void closeSession() { HibernateUtil.closeSession(); } public void updateUserInfo(User user) { Session s = getSession(); Transaction t = s.beginTransaction(); User u = (User) s.load(User.class, user.getId()); u.setAddress(user.getAddress()); u.setEmail(user.getEmail()); u.setMonths(user.getMonths()); u.setName(user.getName()); u.setPhno(user.getPhno()); u.setPwd(user.getPwd()); s.update(u); t.commit(); closeSession(); } } </code></pre> <p>struts.xml</p> <pre><code> &lt;action name="update" class="action.UserAction" method="updateUser"&gt; &lt;result name="success" type="redirect"&gt;listUsers&lt;/result&gt; &lt;/action&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload