开发者

JSF Ajax question

first of all i want to excuse me for my last question. Didn`t know how the system works here :-)

I have a question cocerning JSF and Ajax. My webapplication is dynamic. The whole page loads just one time at the beginning. When the user navigates through the menus, just the center table changes it output.

I have some components like primeface`s datatable or just simple buttons which execute functions in my bean. My webapplication connects to a backend server and informations are shared between JSF and backend and will then be displayed to the users.

When a user clicks e.g. on the "Search" button function, the results should be displayed in my datatable. The method {#searchBean.doSearch} will be executed. A message is sent to the backend with user`s informations. After some seconds JSF receives those informations. T开发者_开发知识库his is my principe.

Now i want to render manually the datatable and tell him "all search results have been received. please update the results" Is it possible to render manually components via JSF? Or is there another solution.

And my second question. When a message is sent to the backend it may take some seconds to receive the results, what is the best way to display a "Waiting" message to the user (with a dialog)? When i receive the results from my backend i want to update manually my components so the received informations will be displayed and then i need a way to remove this "Waiting" dialog.

I hope.. you understand what I want because my english is not the best :-) I dont know how to start solving my problems... i think i just need a way to update HTML elements via JSF and render components manually. But i didnt find anything on the internet. Maybe I looked for the wrong things?

Regards, Johnny


Since you are using Primefaces (which was built with pre-2.0 jsf in mind), you can use a more specific way of handling Ajax (than the one described by BalusC).

You will notice that most components have an attribute called "update". You use this attribute to tell JSF which components should be updated after something happens. For example if you had a button marked as:

<p:commandButton value='test' action='#{bean.search}' update='panelContainingDatatable' />

It would always refresh a component with client id of 'panelContainingDatatable' after executing bean.search() method.

Also, if you want to notify your users about an ajax request pending, use p:ajaxStatus (http://www.primefaces.org/showcase/ui/pprAjaxStatus.jsf)

Be careful, there are caveats:

  • we are talking about client id here, not component id (best look it up in the page sources)

  • there are edge cases when mixing ajax with ui:repeat and h:dataTable; frankly, it's best that you try to avoid such cases than solve them. They come from bugs in implementations and from suboptimal design decisions concerning ui:repeat.


You can use <f:ajax> for this. It has a render attribute which should point to (relative or absolute) client ID of the component you'd like to re-render. Starting with : means absolute (as it is in generated HTML), otherwise it's relative to the current naming container (like as <h:form>).

Here's a simple basic example:

<h:form>
    <h:inputText value="#{bean.query}" />
    <h:commandButton value="Search" action="#{bean.search}">
        <f:ajax execute="@form" render=":results" />
    </h:commandButton>
</h:form>

<h:panelGroup id="results">
    <h:dataTable value="#{bean.results}" var="result" rendered="#{not empty bean.results}">
        <h:column>#{result}</h:column>
    </h:dataTable>
</h:panelGroup>

Where the Bean look like this:

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private String query;
    private List<Result> results;

    public void search() {
        results = resultDAO.list(query);
    }

    // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜