简体   繁体   中英

How to communicate back and forth between client and server-side in JSF?

Most of my problems in JSF, so far, seem to boil down to this - communication from (static) client-side to (dynamic) server-side , and vice-versa; for instance, for re-rendering components.

An example: enabling/disabling a button ( commandButton ) that depends on the selection of a selectoneradio .

What is the correct way to communicate the selection of the selectoneradio (client to server) and then ajaxingly updating the commandButton (server to client)?

By using <f:ajax> .

Here's an example which enables the button when the second item is selected.

<h:selectOneRadio value="#{bean.selectedItem}">
    <f:selectItem itemValue="1" itemLabel="First item" />
    <f:selectItem itemValue="2" itemLabel="Second item" />
    <f:ajax render="button" />
</h:selectOneRadio>
<h:commandButton id="button" disabled="#{bean.selectedItem != 2}" />

Make sure that the #{bean} is a @ViewScoped one so that the state is remembered across postbacks. Else it'll fall back to default values when you press the submit button.


That said, I strongly recommend to go through a decent JSF book. The above is usually already covered in 1st chapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM