How to set the id value for Student in delete()? [closed]

让人想犯罪 __ 提交于 2019-12-20 03:48:29

问题


I want to initialize Student's id in delete action

StudentAction.java:

public class StudentAction extends ActionSupport implements ModelDriven {

    private List studentList;
    Student student;
    StudentDAO sdo = new StudentDAO();

    public String delete() {
        System.out.println("delete action");
        System.out.println(student.getId()); //not setting value of id

        sdo.delete(student.getId());

        return SUCCESS;
    }

  @Override
  public Object getModel() {
    return student;
 }
 //getter and setter
}

Student.java:

public class Student  implements java.io.Serializable {
    private Long id;
    private String name;
    private String address;
    //getter and setter
}

In JSP:

<s:iterator value="studentList" var="ss">
    <s:property value="id"/>
    <s:property value="name"/>
    <s:property value="address"/>
    <a href="delete?id= <s:property value="id"/>">delete</a><br>
</s:iterator><br>

While passing value from JSP to delete action I want to initialize Student's id by using this code. How to do this?


回答1:


Use the action field to set the parameter id:

public class StudentAction extends ActionSupport {

    private List studentList;
    Student student;
    StudentDAO sdo = new StudentDAO();
     private Long id;
    //getter and setter


    public String delete() {
        System.out.println("delete action");
        System.out.println(getId()); 

        sdo.delete(getId());

        return SUCCESS;
    }
}

One more thing if you want to implement ModelDriven you should add the code for your model

private Student model = new Student();

public Object getModel() {
  return model;
}

The code is like in the documentation example.



来源:https://stackoverflow.com/questions/20868095/how-to-set-the-id-value-for-student-in-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!