要通过指定的输入条件搜索学生,您需要将所需的输入从页面传递到操作类。并且需要修改查询,该查询将通过提及的姓名或姓氏或所需的输入来搜索学生记录。
例如,假设学生姓名是输入条件
从页面传递学生姓名:
<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;
}
}