I have a h:datatable that display employees data. I want the user when click the employee name to navigate to a new page which URL looks like
employees?id=<some id>
I've tried to combine JSP EL with JSF EL, but no way out.
I have a h:datatable that display employees data. I want the user when click the employee name to navigate to a new page which URL looks like
employees?id=<some id>
I've tried to combine JSP EL with JSF EL, but no way out.
There are two possible solutions I can think of:
If it's possible to switch to a Java EE 6 server, I highly recommend option number 1.
Edit: There are 2 tags that were added in JSF 2: <h:link />
and <h:button />
. These use GET instead of POST. Also, look into <f:viewparam />
. On top of this, there are many other wonderful additions in JSF 2. For a brief overview, see this article.
If you're not on JSF 2.0 yet, then you can just make use of h:outputLink
in combination with <f:param>
, #{param}
and faces-config.xml
.
Basic table example:
<h:dataTable value="#{bean.employees}" var="employee">
<h:column>
<h:outputLink value="employees.jsf">
<f:param name="id" value="#{employee.id}" />
<h:outputText value="View employee #{employee.name}" />
</h:outputLink>
</h:column>
</h:dataTable>
Basic faces-config.xml
example:
<managed-bean>
<managed-bean-name>employeeManager</managed-bean-name>
<managed-bean-class>com.example.EmployeeManager</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>id</property-name>
<value>#{param.id}</value>
</managed-property>
</managed-bean>
Basic com.example.EmployeeManager
example:
public class EmployeeManager {
private Long id;
private Employee employee;
@PostConstruct
public void init() {
this.employee = employeeDAO.find(this.id);
}
}
The @PostConstuct
annotated methods will be invoked after bean construction and all of injection and managed property setting. Also see this article for more info and examples.