0

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

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

主页.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>

编辑.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>

用户操作

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

用户道

    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>
4

1 回答 1

0

我得到了答案。这是因为我没有在 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;
        }

感谢您抽出宝贵时间帮助 Aleksandr M.. :)

于 2013-10-19T16:27:47.630 回答