在struts 2中获取所选项目文本

时间:2014-06-12 08:52:09

标签: struts2

我有一个ui标签选择

<s:select list="studentList" name="selectedstudent" listKey="studentID"
    listValue="studentName" headerKey="0" 
    headerValue="--All Student--" theme="simple"></s:select>

在java代码中我有selectedstudent返回让我们说4如何在java中获取学生的名字。

3 个答案:

答案 0 :(得分:0)

您可以在selectedstudent中按ID获取名称。为此目的,如果您的动作中没有学生列表,则应从数据库中加载它们。否则,如果为对象提供getter,则按id搜索Student对象将返回一个名称。请注意,您已将listValue指定为studentName。此值应取自Student,因此列表也应为List<Student>

答案 1 :(得分:0)

如果您的studentList在会话中,您可以检索学生姓名,如下所示(您的代码可能不同):

List<Student> studentList = (List<Student>)ActionContext.getContext().getSession().get("studentList");
//You should ovveride equals(and hashCode) to point the Student equality to studentID
//You should also check if studentList.indexOf(selectedstudent) != -1
Student student = studentList.get(studentList.indexOf(selectedstudent));
String studentName = student.getStudentName();

答案 2 :(得分:0)

业务层

Student.java(模特)

public class Student {
   //Getter and Setter Method.
    private String id;
    private String name;
    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }
}

struts.xml中

  <action name="list_action" class="controller.ListAction">
        <result name="success">struts_select.jsp</result>
  </action>

包裹控制器;

ListAction.java

   private List<Student> student_list = new ArrayList<Student>();//Getter and Setter
   @Override
   public String execute() throws Exception {
      //do your stuff
      //list collect from database.
      student_list=(List<Student>) criteria.list();
    }

struts_select.jsp

    <s:select label="Student List" 
              listKey="id"  //use your Student.java member variable of id signature
              listValue="name"  //use your Student.java member variable of name signature
      headerKey="0" headerValue="Select student Name"
      list="s" 
      name="student_info" />