Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve a JSF component value by using EL?

Before going on, see this question

It's JSF form is shown again as follows:

<f:view>
    <h:form>
        <div>
            <label>Id</label>
            <input type="text" name="accountId"/>
        </div>
        <div>
            <label>Amount</label>
            <input type="text" name="amount"/>
        </div>
        <h:commandButton value="Withdraw" action="#{accountService.withdraw(param.accountId, param.amount)}"/>
    </h:form>
</f:view>

Notice that I have used <input type="text" name="amount"> instead of <h:inputText id="amount">. In order to retrieve its value by using Seam EL resolver, I use param.amount.

It happens that if I use <input type="text" and something goes wrong on server side, I need to show the page again. So its submitted value is not retrieved because it is a plain html code. Because of that, I need to use a <h:inputText JSF component instead.

So the question is: How do I retrieve a <h:inputText JSF component value by using Expression Language?

like image 533
Arthur Ronald Avatar asked Dec 21 '09 06:12

Arthur Ronald


2 Answers

The JSF client ID's are prepended by the cliend ID of parent UINamingContainer components (e.g. h:form, h:dataTable, f:subview). If you check the generated HTML source in your webbrowser (rightclick, view source), then you should see them. The id and name of the generated input elements are prepended with the id of the parent form. You need to use the same name as key in the parameter map. As the separator character, the colon :, is an "illegal" character in EL, you need to use the brace notation param['foo:bar'] to retrieve them.

<f:view>
    <h:form id="account">
        <div>
            <label>Id</label>
            <h:inputText id="id" />
        </div>
        <div>
            <label>Amount</label>
            <h:inputText id="amount" />
        </div>
        <h:commandButton value="Withdraw" 
            action="#{accountService.withdraw(param['account:id'], param['account:amount'])}"/>
    </h:form>
</f:view>

Without Seam-EL-like method parameters (you apparently don't want/have it), you can also access them in the request parameter map using the client ID's as keys:

public void withDraw() {
    Map<String, String> map = FacesContext.getCurrentInstance().getRequestParameterMap();
    String id = map.get("account:id");
    String amount = map.get("account:amount");
    // ...
}

Needless to say that this is nasty. Just do it the normal JSF way, bind the values with bean properties.

Edit: as per the final question which you have edited:

So the question is: how do i retrieve a <h:inputText JSF component value by using Expression Language ?

This is already answered before. Use the JSF-generated name as parameter name. This is usually in the pattern of formId:inputId where formId is the id of the parent UIForm component and the inputId is the id of the UIInput component. Check the generated HTML output for the exact name of the generated <input type="text"> field. To obtain the parameter value, use the brace notation ['name'], because you cannot use the colon : in EL as in ${param.formId:inputId}.

Thus:

#{param['formId:inputId']}
like image 88
BalusC Avatar answered Oct 27 '22 17:10

BalusC


The parameter names/ids for inputText are encapsulated by the control renderer and are, ultimately, an implementation detail. In practice, they use the clientId. There should be no need to read these directly from the parameter map; use value binding to push them into the model and read them from there.

You can read values directly from the component using EL by binding the component to a managed bean. For simplicity, here's one bound to the request scope:

<!-- bind a UIComponent to the request map as "foo" -->
<h:inputText binding="#{requestScope.foo}" />
<!-- read value from a UIComponent that implements ValueHolder -->
#{requestScope.foo.value}

But, generally, there is no advantage to this over:

<!-- bind a value to the request map as "foo" -->
<h:inputText value="#{requestScope.foo}" />
<!-- read value from the request scope -->
#{requestScope.foo}

To avoid polluting the request map (which might result in collisions across views), use a managed bean to namespace/encapsulate your values rather than using the request scope directly.

like image 38
McDowell Avatar answered Oct 27 '22 16:10

McDowell