0

I am new to java ee and dont have much idea about jsf and all. I am making a simple java web application that fetches data from database and shows in dataTable. I need to edit data selected by user from dataTable for which i need to get value of the row selected/clicked. But i havent been able to do it. Can any one please help me with my code ? I hope someone would tell me how can i do it with my following codes.

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>

Student.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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vins
  • 449
  • 4
  • 13
  • @BalusC: This duplicate does not show up directly if you use the word 'selection' instead of 'current row'. Maybe make the Q more generic? I'd like to give it a go, but just not sure – Kukeltje Jul 14 '15 at 07:06
  • @Kukeltje: point taken. – BalusC Jul 14 '15 at 07:08

1 Answers1

0

You can pass a parameter using expression language.

In your bean, have a function like

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

Now, using expression language, you can call that method using #{yourbean.editStudent('id')}

Since you are iterating through your data using a datatable, you can access the student's id from the var variable.

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

The expression language will access the getStudentId() (getter method from studentId) method from the specific student.

kevcodez
  • 1,261
  • 12
  • 27
  • There are multiple duplicates of this. Please search for them and flag as such. I know this does not give yoh any reputation, but it keeps SO 'clean' – Kukeltje Jul 14 '15 at 06:50