如何在delete()中设置Student的id值?

时间:2014-01-01 11:21:05

标签: java jsp struts2 ognl model-driven

我想在Student动作中初始化id的{​​{1}}

StudentAction.java:

delete

Student.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
}

在JSP中:

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

在将值从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> 操作时,我希望使用此代码初始化delete的{​​{1}}。怎么做?

1 个答案:

答案 0 :(得分:1)

使用操作字段设置参数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;
    }
}

如果您想实施ModelDriven,还应该添加模型代码

private Student model = new Student();

public Object getModel() {
  return model;
}

代码类似于文档example