2013-10-15 101 views
0

我想单击home.jsp中相应的编辑按钮来更新用户详细信息。 edit.jsp中的字段可以由检索到的值填充。但点击保存按钮edit.jsp时,它显示空指针异常。通过Struts 2更新数据,休眠

我认为更新操作没有被调用。任何帮助表示赞赏。

针对home.jsp

<s:property value="#session.userid"/> 
      <tr> 
       <th>Name</th> 
       <th>Email</th> 
       <th>Address</th> 
       <th>Phone No</th> 
       <th>Actions</th> 
      </tr> 
      <s:iterator value="users"> 
       <tr> 
        <td><a href=""><s:property value="name"/></a></td> 
        <td><s:property value="email"/></td> 
        <td><s:property value="address"/></td> 
        <td><s:property value="phno"/></td> 
        <td><a href="edit?id=<s:property value="id"/>">Edit</a>&nbsp;|&nbsp; 
         <a href="delete?id=<s:property value="id"/>">Delete</a></td> 
       </tr> 
      </s:iterator> 

     </table> 

edit.jsp文件

 <s:form action="update"> 
       <table> 
        <s:hidden name="id" value="%{u.id}"/> 
        <s:textfield name="name" label="Name" value="%{u.name}"/> 
        <s:textfield name="pwd" label="Password" value="%{u.pwd}"/> 

        <s:textfield name="email" label="Email" value="%{u.email}"/> 
        <s:textarea name="address" label="Address" rows="3" cols="14" value="%{u.address}"/> 
        <s:textfield name="phno" label="Mobile" value="%{u.phno}"/> 
        <%--<s:select label="Select Date of Month" name="months" headerKey="0" headerValue="--Select--" 
    list="allMonths" listKey="id" listValue="name"/>--%> 
        <s:select label="Select Date of Month" name="mid" headerKey="0" 
           headerValue="--Select--" list="months1" value="%{u.mid}"/> 
        <s:submit value="Save"/> 
       </table> 
      </s:form> 

UserAction

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; 
     } 
    } 

用户

@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; 
     } 
    } 

userDAO的

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(); 
      } 
     } 

struts.xml的

<action name="update" class="action.UserAction" method="updateUser"> 
       <result name="success" type="redirect">listUsers</result> 
    </action> 
+1

后的堆栈跟踪。 –

+0

java.lang.NullPointerException –

+0

FULL异常栈跟踪怎么样? –

回答

0

我得到了ambswer。这是因为我没有在动作方法中创建模型对象。

Action类必须改变以

public String updateUser() 
     { 
      User u = new User(); 
      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; 
     } 

谢谢您的时间A和帮助亚历山大·中号.. :)