JSF, AJAX and the rendered attribute - ajax

If you do JSF AJAX calls and change the component tree while rerendering (or between ajax calls), you'll get exceptions from Mojarra. As I understand it, it's difficult to recreate the component tree partially when the new tree is different as the one stored in the ViewState (or the actual JSF class). That's "ok". I'm thinking about using the rendered attribute and not rendering the component.
My question: How does the rendered attribute work? Does the component get restored and is the component tree, that JSF creates during restore phase, safe? We have a very dynamic XHTML page and not rendering object's instead of disabling them with css classes would really up the speed of the page.

I tried it and it works as expected. So JSF only assumes that the component is there even if it doesn't render anything.
Please consider that the view state does get restored and it's still a performance hit (but a smaller one as nothing get's sent over the wire and the output string/html doesn't need to be rendered).

Related

Force re-evaluation of rendered property on JSF component

In my application, I have a phaselistener that can conditionally hide a component by setting UIComponent.setRendered(false) .
On that page, I have ajax tabs, with tab one able to influence the display of the other. So, I go to a tab, change something, come back, but there I find that the component still remembers it's unrendered state.
I guess not doing ajax for my tab navigation would solve this, but is there any other way that I can somehow force the component tree to be build up as if it was the first time?

JSTL tag management on RichFaces 3 re-rendering

I read various examples on the web where an ajax call sent through a4j tags of ReachFaces 3 re-renders a jstl tag. In this example:
http://relation.to/Bloggers/UsingDynamicallyCreatedRichFacesTabPanelForSearchResults
an ajax call re-renders a c:foreach inside a rich:tabPanel (look at the code after "Add the next code just after the panel code:").
My question is: since the c:forEach tag is evaluated during view creation (like any other jstl tag), the portion of the view pointed by the reRender attribute is reconstructed from scratch on every ajax request?
Thanks a lot.
Nico
Looking at JSF lifecycle docs, more specifically the Restore View Phase and Render Response Phase parts, you can find what are you looking for:
Restore View Phase
If the request for the page is an initial request, the JavaServer Faces implementation creates an empty view during this phase and the life cycle advances to the render response phase. The empty view will be populated when the page is processed during a postback.
If the request for the page is a postback, a view corresponding to this page already exists. During this phase, the JavaServer Faces implementation restores the view by using the state information saved on the client or the server.
Render Response Phase
During this phase, the JavaServer Faces implementation delegates authority for rendering the page to the JSP container if the application is using JSP pages. If this is an initial request, the components represented on the page will be added to the component tree as the JSP container executes the page. If this is not an initial request, the components are already added to the tree so they needn't be added again. In either case, the components will render themselves as the JSP container traverses the tags in the page.
In short words, when you perform an Ajax request, you already have the view there. JSF will just search the components you want to rerender through it, just after their model have been updated, and update the view with their new values. In case of c:forEach, it's not a component, but a tag handler, so it's evaluated before that.
Related to the link you posted, have a look at this page about the difference between components and tag handlers. They just have different lifecycles, so take care when mixing them.

HTML tag specifically for storing arbitrary data

I am loading a Razor View via AJAX and putting the content into a div on a page.
If I want to send some arbitrary data from the view to our AJAX framework, is there a recommended HTML tag to do this with? A hidden field sounds like the wrong this to use for this. I could use an empty div with custom data- attributes, but again, a div sounds wrong.
My data isn't relevant to any element within this view, more related to the view itself.
Yes - this is simply a question of systematics and aesthetics.
I use data-* attributes, and try to find and attach them to already existing related elements. This convention is also used by asp.net mvc framework itself - when link target needs to be loaded, data-ajax-* is attached to anchors. When you need to set update target of form submit, you attach data-ajax-* to form, so in most cases it is possible to find good candidate for it. If it is not possible in any particular case, I do not see problems using body instead.

Dependent Dropdown in JSF fails validation

I have two dependent dropdowns on a JSF page that work fine. I use a valueChangeListener on the first dropdown that populates the List backing the second dropdown.
However when I try to submit my form it's failing JSF validation. From testing I think the problem is that when the page loads my dependent dropdown list is empty, then I populate it after the first dropdown has a selection made. However none of the values now in the dependent list were in the list when the page loaded so it's fails validation. I have confirmed this by using a constructor to set up the list with all the possible values when the page loads and it makes my problem go away however this isn't a possible solution as loading up all the values would kill the performance of my page.
Any ideas how I can get it working?
Regards,
Kevin.
This is EXACTLY the use case for a view scoped bean. Using a request scoped bean in such case is going against the grain of JSF (possible, but painful - like using a hedgehog as a bath sponge).
If there are any problems with such solutions, then tell us, there should be a way of mitigating them; the point is, you should use the view scope and solve any problems you might have with that, and not try to run away from it.

Delayed loading for large queries

A pleasant user experience requires a page to load very fast. This can be difficult when there is a large query taking place or when a web service is being used.
I deally, the entire page should be loaded first with loading images everywhere a cumbersome task takes place.
In ASP.NET, this can be accomplished using update panels and a 1 second trigger. The trigger would load some data and then the updatepanel will the update. This approach may be less popular if you do not require entire asp.net page life cycle events to fire. With this approach, allot of methods will fire consuming more resources and time. What if you simply want to display data?
Another aproach would be to create an .ashx httphandler that returns a representation of data in html format. A timer would go off one second after the page loads which triggers a div panel to be populated with the html returned from the .ashx handler. This would bring high performance but you would lose postbacks and viewstate. I suppose that is expected when losing the asp.net page-life cycle.
How would you go about creating a delayed task that uses viewstate and postback if possible that brings great performance?
Maybe creating an http handler to a custom extension, and in the implementation of the handler return the data (or pre-formatted html, json, or xml) that you then insert into an html element of the container page with a javascript ajax call.
Hugo

Resources