【问题标题】:how to get the index of data from data Table in jsf? [duplicate]如何从jsf中的数据表中获取数据的索引? [复制]
【发布时间】:2015-10-02 07:47:03
【问题描述】:

我是 java ee 的新手,对 jsf 和所有东西都不太了解。我正在制作一个简单的 java web 应用程序,它从数据库中获取数据并显示在 dataTable 中。我需要编辑用户从 dataTable 中选择的数据,我需要为其获取选择/单击的行的值。但我没能做到。任何人都可以帮我解决我的代码吗?我希望有人能告诉我如何使用以下代码来做到这一点。

showRecords.xhtml

h:dataTable  value="#{studentList.studentL()}" var="student" styleClass="studentTable"
                         columnClasses=",,,fixedWidth">
                <h:column>
                    <f:facet name="header">Student ID</f:facet>
                    <h:outputText value="#{student.studentId}"></h:outputText>
                </h:column>

            <h:column>
                <f:facet name="header">Name</f:facet>
                <h:outputText value="#{student.fname}"></h:outputText>
            </h:column>

学生.java

@ManagedBean(name="student")

public class student {
 @Id  private String StudentId;
   private String Fname, Lname, Mname="noname";
/*******getters and setters** and database transaction****/

}

studentList.java

@ManagedBean(name="studentList")
@SessionScoped

public class studentList {
    public List<student> studentL(){

        List<student> list = new ArrayList<student>();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = null;

        try{
           Class.forName("org.apache.derby.jdbc.ClientDriver");
           con = DriverManager.getConnection("jdbc:derby://localhost:1527/tourManager","administrator","pass");
           String sql = "Select * from student";
           ps = con.prepareStatement(sql);
           rs = ps.executeQuery();

           while(rs.next()){
           student student1 = new student();
           student1.setFname(rs.getString("FNAME"));
           student1.setLname(rs.getString("LNAME"));
           student1.setStudentId(rs.getString("STUDENTID"));
 list.add(student1);
         // return list;
           }

        }catch(Exception e){
            e.printStackTrace();
        }


        return list;

    }
public void editStudent() throws IOException{
        int index = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("index").toString());
        System.out.println(" the selected row is "+index);
    }  


}

【问题讨论】:

  • @BalusC:如果您使用“选择”一词而不是“当前行”,则此副本不会直接显示。也许让 Q 更通用?我想试一试,但不确定
  • @Kukeltje:点。

标签: jsf datatable


【解决方案1】:

您可以使用表达式语言传递参数。

在你的 bean 中,有一个类似的函数

public void editStudent(String studentId)
{
  // do something with id
}

现在,使用表达式语言,您可以使用 #{yourbean.editStudent('id')} 调用该方法

由于您使用数据表遍历数据,因此您可以从 var 变量访问学生的 ID。

<h:dataTable  value="#{studentList.studentL()}" var="student" ...>
...
    <h:commandButton value="Edit" action="#{studentList.editStudent(student.studentId)}" />
...
</h:dataTable>

表达式语言将访问特定学生的getStudentId()(studentId 的getter 方法)方法。

【讨论】:

  • 这个有多个重复。请搜索它们并标记为这样。我知道这不会给你任何声誉,但它保持如此“干净”
猜你喜欢
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2010-11-02
  • 2011-02-13
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多