简体   繁体   中英

ajax behaviour in jsf 1.2

I have a selectonlistbox and I want to reload a panelgrid everytime the selectbox selection is made/changed. I am try to use just jsf 1.2.

I know it can be done easily using a4j or faces 2, but if anyone knows any good way to achieve this in jsf 1.2, I would really appreciate it.

A simple example would help a lot too.

Thanks

You basically need to create a custom Ajax aware ViewHandler and a custom XML ResponseWriter and custom tags which generates the desired JavaScript code to perform the XMLHttpRequest works and HTML DOM manipulation. This is also what most of those 3rd party JSF 1.x component libraries have done. They are all open source, so you could just take a peek in its source code to learn-by-example how they did it. It's for a starter after all however not a trivial job as it requires a solid knowledge of how exactly HTTP, HTML DOM, JavaScript and XMLHttpRequest works. A little mistake is quickly made and it would for a starter take ages to find and really understand it.

Your best bet is really either adopting an ajax capable component library for JSF 1.2 (eg RichFaces 3.x which includes Ajax4jsf), or just migrating to JSF 2.0 (JSF 1.2 is over 6 years old already and JSF 2.0 was introduced almost 3 years ago already; JSF 1.x is basically history).

This particular functional requirement can however also be done without ajax, albeit somewhat clumsy, certainly if form validation plays a role. Here's a basic kickoff example, it's a matter of just instructing JavaScript to submit the parent form on change of the dropdown list:

<h:form>
    <h:selectOneMenu value="#{bean.selected}" onchange="this.form.submit()">
        <f:selectItem itemValue="one" />
        <f:selectItem itemValue="two" />
        <f:selectItem itemValue="three" />
    </h:selectOneMenu>
    <h:panelGroup rendered="#{bean.selected == 'one'}">
        One was selected.
    </h:panelGroup>
    <h:panelGroup rendered="#{bean.selected == 'two'}">
        Two was selected.
    </h:panelGroup>
    <h:panelGroup rendered="#{bean.selected == 'three'}">
        Trree was selected.
    </h:panelGroup>
</h:form>

This will submit the whole form synchronously on change of the dropdown list, as if you pressed a submit button. However, as said, if you're performing validation elsewhere in the same form, you'd have to bring in quite some hacks with immediate="true" and valueChangeListener to prevent validation of all other form elements. See also Populate child menus for some detailed examples.

A completely different alternative is to just render everything to the HTML response and hide it using CSS display:none and then toggle the display using JavaScript element.style.display in some JS function which is referenced in onchange attribute.

我认为更好的选择是迁移到JSF2。如果要使用JSF 1.2,请使用一些JS库(例如JQuery或prototype),并在事件上调用JS函数,然后使用带有面板网格的模板页面的URL调用AJAX更新器和逻辑(例如:/mypanaelGrid.jsf)。

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