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

2019-02-26 06:14发布

问题:

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.