tags:

views:

49

answers:

2

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.

+2  A: 

There are two possible solutions I can think of:

  1. Use JSF 2 (part of Java EE 6)
  2. If you are stuck in JSF 1.x, use PrettyFaces.

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.

Zack
So, Is using JSF2 will solve this problem,as I am afraid after switching to still face the problem?
Mohammed
You don't necessarily need JSF2 for this. This makes no sense.
BalusC
You don't *need* JSF 2 for this. You can modify your view handler to accept accept query parameters. However, this is a hacky way of doing it. You can also use an output link. However, that circumvents JSF navigation.
Zack
+1  A: 

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.

BalusC
Your blog looks great, thanks :)Yes you are correct about the hint of @PostConstruct, I've reialized this note (and list it in my blog http://m-hewedy.blogspot.com/2010/01/calling-session-beans-on-jsf-managed.html )But one question here, What if I need to put the employee object in the session scope using the h:outputlink ?
Mohammed
Just make `EmployeeManager` a session scoped bean.
BalusC