0

嘿伙计们,我正在做的一个项目遇到了一些问题:/

我有这个代码

public class ListaAlumnoAction {

private List<Alumno> alumnos;

public String execute(){
    String camino="success";
    EntityManager em= Utilitario.getInstance().getEntityManager();
    Query query=em.createQuery("Select o From Alumno o");
    alumnos=query.getResultList();
    return camino;
}

public List<Alumno> getAlumnos() {
    return alumnos;
}

public void setAlumnos(List<Alumno> alumnos) {
    this.alumnos = alumnos;
}
}

这种方法给了我一个学生名单。但在这种情况下,我只想要 1 个学生,例如通过代码或姓名和姓氏搜索。问题是我不知道为此目的使用哪种工具。

先谢谢了。

4

1 回答 1

0

要通过指定的输入条件搜索学生,您需要将所需的输入从页面传递到操作类。并且需要修改查询,该查询将通过提及的姓名或姓氏或所需的输入来搜索学生记录。

例如,假设学生姓名是输入条件

从页面传递学生姓名:

 <s:form>
    ......          
    <s:textfield name="studentName" label="Student Name"></s:textfield>   
    <s:submit value="Search" name="submit" action="ListaAlumnoAction"/>
   ......
  </s:form>

这里 studentName 将是搜索操作的输入,ListaAlumnoAction 是在单击 Search 按钮时触发的操作。

要在动作类中获取学生姓名:

     public class ListaAlumnoAction {

        private List<Alumno> alumnos;

 /* Please note both the text filed name in the page and attribute should be same "studentName" */
        private String studentName;   


      public String execute(){
        String camino="success";
        EntityManager em= Utilitario.getInstance().getEntityManager();
       // "name" variable will hold the studentName      
        String name = getStudentName();
       // Modify the query, by passing the name if it is not null
        Query query=em.createQuery("Select o From Alumno o");
        alumnos=query.getResultList();
        return camino;
    }

    public List<Alumno> getAlumnos() {
        return alumnos;
    }

    public void setAlumnos(List<Alumno> alumnos) {
        this.alumnos = alumnos;
    }

    // getter and setter for the studentName
    public String getStudentName(){
      return studentName;
     }
    public void setStudentName(String studentName){
      this.studentName = studentName;
     }     

    }
于 2012-10-25T04:52:57.217 回答