ajax behaviour in jsf 1.2 - ajax

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 (e.g. 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.

I think better option is migrate to JSF 2. If you want to do it with JSF 1.2, use some JS libraries like JQuery or prototype and call your JS function on the event and call AJAX updater with the URL to the template page with panel grid and the logic (eg: /mypanaelGrid.jsf).

Related

Why my PrimeFaces's controls do not work after migration?

Just migrated from jboss 7.1 to wildfly 16 and PrimeFaces 4.0 to 6.2.
As I know PrimeFaces uses ajax with its elements such p:commandLink, p:commandButton, etc.
I have:
<p:commandLink action="#{exampleBean.redirrectSomewhere}">
labelName
</p:commandLink>
After tapping at the commandLink nothing is happening, but if I add ajax='false' as an attribute, it is going to work fine.
Is there some way to get redirected using ajax?
Ask for extra information.
This is a known issue (at least to me). Use process="#this" to make sure that the ajax updates current component instead of the whole view. You can then remove ajax="false".

Which event is fired when completeMethod is executed on inputTextArea in PrimeFaces?

I need to use InputTextarea PrimeFaces component, wich allow use autocomplete mechanism between text. All works great, except for p:statusAjax, when I'm typing, my loading var animation blocks screen.
I resolve this for AutoComplete PrimeFaces component by adding next lines between p:autoComplete:
<p:ajax event="query" global="false"/>
But when I try put this line between p:inputTextArea JSF shows error:
javax.servlet.ServletException: /notificaciones/edit.xhtml #162,50 Event:query is not supported.
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)...
I've read PrimeFaces User Guide but for inputTextArea component there is no Ajax Behavior Section.
So, how can I know which event is firing when autocomplete method executed on p:inputTextArea
Most of the time, the documentation is up-to-date so most likely there is no ajax event. But to be sure, you can always check the javascript source and/or the java source of the component (the source is open and freely accessible).
You can always try without an event name to see if ajax is supported at all since all (most?) components have a default event.
I'm in a good mood and A quick check for you in the java source and the forms.js javascript source of 6.1 (which contains the js for this component and which is split in its own forms.inputtextarea.js in the 6.1.3 Elite and upcoming 6.2 release) does not show any ajax events being used, so I think you are out of luck on this and need to file an enhancement request in github

Equivalent of <f:ajax render="#form" />

Recently I started using prettyfaces with my web application which uses jsf1.x. While testing I realized that action methods for h:commandButton and h:commandLink is not getting called. Upon searching on the net, I found a link on this site where some suggested to use <f:ajax render="#form" /> it should be ok. Since f:ajax is available in JSF2.x, so I was thinking how can I find the equivalent of <f:ajax render="#form" /> in Jsf1.x
Any help be greatly appreciated as I am completely stuck due to this.
Thanks,
Neeraj
There is something seriously wrong if the actions of the h:commandButton and h:commandLink are not being called anymore. Maybe it's something easy like failed validation, which cancels the call of the action (try immediate="true" in your button/links). If it's not, then you should try to find and fix what causes this instead of switching to Ajax functionality.
As already pointed out, you may use a4j which is delivered with RichFaces 3 and works also with JSF 1.
BUT you cannot throw away this basic functionality. Using Ajax only you will run into a lot of problems later on. a4j:ajax might be the equivalent of f:ajax, but it's certainly not equivalent to the standard post-action functionality.
One common usecase would be a PDF to be displayed. This isn't possible with an Ajax partial-update.

jsf primefaces fileupload on submit

Does anyone know how I can upload multiple files with primefaces (http://www.primefaces.org/showcase/ui/fileUploadMultiple.jsf) when I click my own submit button? The upload-button there should not be visible, because I want to chose the upload-time on my own.
I have multiple pm-fileupload components in my document, so I want to upload them all together.
is there a way? Or must I use the old browser-upload?
Use multiple <p:fileUpload mode="simple"> generated by a <ui:repeat> or <h:dataTable>.
The <p:fileUpload mode="advanced"> indeed isn't suitable for this requirement, or you must turn on automatic immediate upload on select by adding auto="true".

Why does ajax and jsf work nicely together? How do Ajax lifecycle plug in with normal JSF lifecycle?

I have used JSF 1.2 but new to JSF 2.0 . It seems like JSF 2.0 seamlessly supports ajax functionality via <f:ajax> but I am not clear yet how ? I would like to understand what makes these two powerful technologies to work so nicely together ? How does the two lifecycles interact ?
P.S: I am familiar with ajax and javascript. So you can base your answer on that premise.
The question is really broad and I would suggest to search for the appropriate tags to understand its usage in real situations. BalusC has contributed much here on stackoverflow and has also written excellent tutorials that Xtreme Biker made a reference to.
Due to abscence of answers I would offer a basic vision of how ajax works within JSF. There is a special Javascript library in JSF which makes it possible to perform ajax calls to the server with jsf.ajax.request(...). To ease development, there are components that you may attach ajax behaviour to. Typically you will use <f:ajax> tag on the component of your choice, like <h:commandButton>, to add ajax functionality to it.
In the old times we would send an asynchronous XMLHttpRequest via get or post to the server and wait until server sends us postback data which we will get most typically in JSON or XML format for client'side processing and update the view via document.getElementById(...) or by more convenient methods introduced by modern Javascript libraries. I think that in the end this is what JSF does behind the scenes.
In JSF 2.0 <f:ajax> tag was introduced which helps partially submit data, process it on server and partially update your view. For this the ajax tag has the following most important features/attributes: <f:ajax execute="..." render="..." event="..." listener="..." onevent="..." />. Let's take a closer look on all of them.
execute attribute tells JSF about what elements should be updated/processed on the server during this request by specifying a list of element ids;
render attribute tells JSF which components shall be replaced after ajax call is finished - the new elements that were rendered on the server shall replace the old ones with specified ids after partial page update;
event attribute defines events on which an ajax call shall take place, for instance, in case of a command button the event may be a click event, in case of an input text field the event may be a keyup, or blur event;
listener attribute defines a binding to a managed bean method of type public void processAjaxRequest (AjaxBehaviorEvent event) { } that will be triggered on ajax request and executed on server, before partial page update is done;
onevent attribute defines a javascript function to call during different phases of ajax request.
You may consult another excellent tutorial written by Marty Hall on ajax here.
I didn't intend to make an overview of ajax features in JSF 2.0, but rather to make a short introduction to get a basic grasp of ajax functionality.

Resources