8

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?

Community
  • 1
  • 1
Arthur Ronald
  • 33,349
  • 20
  • 110
  • 136
  • To start, your question was fairly confusing. It look like that the Seam story is completely irrelevant here. In the future please post the JSF code you **actually** have and tell what exactly you want to do with **your** code. – BalusC Dec 21 '09 at 14:08
  • This makes also no sense. In plain JSP/Servlet you could have used `` for that. – BalusC Dec 21 '09 at 14:21
  • I strongly recommend to get yourself through a decent JSF book/tutorial to understand how JSF works "under the hoods". Also learning "plain vanilla" JSP/Servlet would help a lot in understanding it. You're making assumptions and are trying to break JSF's ideology by taking all its work over in your own hands. Sorry if I sound harsh, but that's the truth. – BalusC Dec 21 '09 at 14:24
  • @BalusC. You said: You're making assumptions and are trying to break JSF's ideology by taking all its work over in your own hands. Sorry but i am not making assumption. If Seam supports provides parameterized method-binding and i can use this feature, what is wrong ? – Arthur Ronald Dec 21 '09 at 14:35
  • Oh you **are** using Seam? I provided an answer which does the Seam way but then you were commenting that you need `FacesContext.getApplication().SOMETHING`? This is just confusion galore. – BalusC Dec 21 '09 at 14:43
  • As you know BalusC, There is an implicit facesContext object: The FacesContext instance for the current request. Ok. So i think i can retrieve a Component value by using this implicit object. Nothing else. – Arthur Ronald Dec 21 '09 at 14:46
  • Yes I know that. But the whole approach is in my eyes plain flawed and your question wasn't formulated very clear. – BalusC Dec 21 '09 at 15:02
  • @BalusC. Although i read english very well, i have some limitation when writing in english language - I am a non-native english speaker. So feel free to edit my own question/answer when necessary - you have more than 10k reputation. Sorry any mis­under­standing. – Arthur Ronald Dec 21 '09 at 15:42

3 Answers3

19

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']}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hm, this was your own answer and this was supposed to work? Well, I can only hint to check the generated HTML output to see if the names are right and/or if they are available in the request parameter map. – BalusC Dec 21 '09 at 13:32
  • So you're saying that you *actually* have a `h:inputText` in your JSF page and not ``? This way you need to give it (and the parent form) an `id` and get the param with the generated client ID as key. I'll edit my answer soon. – BalusC Dec 21 '09 at 13:53
  • They are in the request parameter map. Have you for instance debugged its contents? `System.out.println(requestParameterMap);` so that you can see them and the patterns? – BalusC Dec 21 '09 at 13:58
  • @BalusC - I think it is worth pointing out that: __A)__ the parameter used by a component is an implementation detail of the renderer; __B)__ the exact form of the `clientId` is an implementation detail (JSF 2 lets you change the delimiter, for example). – McDowell Dec 21 '09 at 14:18
  • @BalusC If possible, see my own answer – Arthur Ronald Dec 27 '09 at 03:19
1

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.

McDowell
  • 107,573
  • 31
  • 204
  • 267
0

Although i can retrieve JSF component value through its client identifier, There are some drawback as follows:

  1. According to McDowell's answer, The client identifier is an implementation detail
  2. The client identifier can differ from the component identifier when there is more than one naming container in the control hierarchy. (JSF in Action book)

So if you do not want rely on client identifier, you may implement your own ElResolver as follows:

public class ComponentIdResolver extends ELResolver {

    public Object getValue(ELContext context, Object base, Object property) {
        if (base instanceof UIComponent && property instanceof String) {
            UIComponent r = ((UIComponent) base).findComponent((String) property);

            if (r != null) {
                context.setPropertyResolved(true);

                return r;
            }
        }

        return null;
    }

}

Now i can use something like (Notice withdraw method)

<h:inputText id="accountId" />
<h:inputText id="amount" />
<h:commandButton value="Withdraw" action="#{accountService.withdraw(view.accountId.value, view.amount.value)}"/>

view is a implicit JSF object (similar to FacesContext.getCurrentInstance().getViewRoot())

Then register your ELResolver as shown below (faces-config.xml)

<application>
    <el-resolver>br.com.view.resolver.ComponentIdResolver</el-resolver>
</application>

If you want to know a good insight about ElResolver see Extending the Java EE Unified Expression Language with a Custom ELResolver

regards,

Arthur Ronald
  • 33,349
  • 20
  • 110
  • 136