0

Context

Legacy system written in Java Server pages 2.

Problem

I have a managed bean that provides a list of students (StudentListBean) to a page (student-list.xhtml). This bean does not provide access to a single student, since its goal is to generate a student listing and it's working correctly for that purpose. Each student is shown in a data table, that iterates along the students collection.

Some students have a certain condition that may require to show some details. In these cases, a link is provided so the user can click and see those details.

The link is correctly built when it has to be shown, but since those details can be very large, those should be shown in another tab in the browser, not in the same one showing the listing. So, the new page will show data from a single student.

Because StudentListBean does not expose a single student, it cannot be used to show data from a single student. Changing its behavior is considered a bad choice because the bean intends to manage a collection of students, not a single one. A new bean (StudentBean) is to be created to provide access to all the details of a single student.

The question here is: what to do to pass a student's Id (the current one in the listing) to the StudentBean so it can load the student from its Id and so its data can be shown in another page (student.xhtml)?

Since no interaction is need with StudentListBean, the link was build using <h:link/> tag, because some online sources suggested that it's a more efficient choice to <h:commandLink/>.

Thanks in advance!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AlexSC
  • 1,823
  • 3
  • 28
  • 54

1 Answers1

0

You can add a <f:param> to the <h:link> component, which will hold the id of a Student. (assuming that your dataTable component has a set var="student" attribute)

<h:link value="Your link text" outcome="the address you want to open" target="_new">
    <f:param name="studentId" value="#{student.id}" />
</h:link>

Then, in order to retrieve the passed id in the managed bean, you have to do:

String id = FacesContext.getExternalContext()
                        .getRequestParameterMap()
                        .get("studentId");
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Thanks for your answer. However, I didn't have to use `FaceContext` to get the parameter value. Instead, a created a get/set pair for the property id and JSF was smart enough to use them. Anyways, it's good to know about a different approuch form the same problem! – AlexSC Jan 06 '15 at 13:35