page1.getChildren().size() = 0

Hi all,

I did not find a suitable answer to this question.

I want to dynamically disable some components on my page. So I iterate

over all elements.

The first time the page is displayed page1.getChildren().size() is 0.

The second time it is - correctly - 1.

I tried using prerender and beforeRenderResponse with the same result.

In afterRenderResponse the value is 1. But then - I guess - I cannot change

anything.

One more problem occurs: I have a redirection. And then the children

are 0, even the second time the page is called. So, going through all my

pages at Server Start Time does not help.

Is there a solution for this problem?

Thanks for any hints,

Heike

[756 byte] By [hfra] at [2007-11-26 8:40:30]
# 1
Hi Heike,Use getBody1().getChildren() call on preprocess (not prerender) stage.Thanks, Misha(Creator team)
Mikhail_Matveev at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 2

I ran into the same problem. Is that a bug or intended behavior. I found that getChildren.size() returns 0 on preRender(), the first time the page is loaded on the browser. However, it is available on all subsequent post backs.

Why would we not have access to the children elements on the first prerender()? Bug?

I can use preProcess(), but its not called the first time a page is loaded. So I don't have any way to loop through children of an element without a post back :(( I found another post in the forum with the same problem reported but no solution/work around.

Message was edited by:

Sabir

Sabir at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 3

Looks like no one knows about it. I did some more testing and found out that:

1. When the page is loaded the first time, getChildren.size always returns 0, regardless of the component. You can even do it on the very top-level and still get the same.

2. On all subsequent access to the page, such as post backs, or even going back to the page from a hyper link, returns the real size (>0) for getChildren.

I tried booth preRender() and preProcess() and got the same results. That means, I can't rely on dynamically looping through all children and change properties.

Sounds like a BUG to me. Gurus, where are you?

Sabir at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 4

Hello,

I am no guru, but I am pretty good at researching, so I decided to research this issue.

I found the following information in JavaServer Faces by Hans Bergsten

Restore View

"The requested view is restored from state sent with the request or available on the server, or created if no view state is available. The specification leaves the details up to the implementation, but the components' restoreState() method is called in this phase if the saveState() method was called earlier to save the state. If a new view is created in this phase, at least the UIViewRoot component is created and the processing continues in the Render Response phase."

Render Response

"If this is the first time the view is rendered, the JSF action elements in the page creates and configures the components they represent and ask them to render themselves by calling the encoding methods; otherwise the components are just asked to render themselves."

At the end of the Render Response phase the app saves the state of the response for subsequent requests.

I then referred to Figure 21 in http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/app_ model.html

Figure 21 shows that Restore View preceeds the call to init() while Render Response is not called until after the call to prerender().

When a person enters a page in the web app for the first time, there is no view to restore, so probably only the UIViewRoot component is created. The rest of the component tree is not created until after prerender() is called.

When a person returns to the page, the state has been saved and thus, during the Restore View phase, the component tree is restored and is available for the preprocess() and prerender() methods.

I have never ttried this, but perhaps you can add the afterRestoreView() method to your page bean to do the logic that that requires use of childCount(). See http://developers.sun.com/prodtech/javatools/jscreator/reference/docs/apis/appb ase/com/sun/rave/web/ui/appbase/AbstractPageBean.html

jetsons at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 5

Christine, you are absolutely right,

UIViewRoot object is created but not filled in neither in beforeRenderResponse() nor prerender() methods.

This process of filling in with components tree is initiated in ViewHandlerImpl.class, line 311 (latest JSC2.1) that is taking place after before-mentioned methods but before afterRenderResponse method is invoked.

BTW, do you have an idea how to change components properties dynamically than? In afterRenderResponse() there is no control over the rendering process (renderComplete=true) already and nothing happens when you navigate to the page first time.

Kind regards,

Alex

AKlimenko at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 6

> BTW, do you have an idea how to change components

> properties dynamically than? In afterRenderResponse()

> there is no control over the rendering process

> (renderComplete=true) already and nothing happens

> when you navigate to the page first time.

I do not think I am understanding your question, because most of our tutorials show how to change component properties dynamically. So I think you are asking something else.

But to answer the question of how to change component properties dynamically upon the first visit of the page, make sure the property is not set in the JSP (that is, if you look at the property in the Properties window, the property does not have a value, and if you look at the JSP tag the attribute is not being set), and set the property in the prerender method.

See http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/abo ut_components.html#general, especially the note under "Setting Component Values"

jetsons at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 7
Mikhail, can you pay attention to my post? http://forum.sun.com/jive/thread.jspa?threadID=107478Spasibo!
AChervov at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 8

> > BTW, do you have an idea how to change components

> > properties dynamically than? In

> afterRenderResponse()

> > there is no control over the rendering process

> > (renderComplete=true) already and nothing happens

> > when you navigate to the page first time.

>

> I do not think I am understanding your question,

> because most of our tutorials show how to change

> component properties dynamically. So I think you are

> asking something else.

>

> But to answer the question of how to change component

> properties dynamically upon the first visit of the

> page, make sure the property is not set in the JSP

> (that is, if you look at the property in the

> Properties window, the property does not have a

> value, and if you look at the JSP tag the attribute

> is not being set), and set the property in the

> prerender method.

>

> See

> http://developers.sun.com/prodtech/javatools/jscreator

> /learning/tutorials/2/about_components.html#general,

> especially the note under "Setting Component Values"

Yes, you got me totaly wrong, sorry it is my fault. By dynamically I've meant setting page component properties generically instead of specifying each property name.

For instance, I have LayoutPanel with a set of components on top of it and I want to make all the components read only. So, approach given by Mr. David Botterill (http://blogs.sun.com/david/entry/generically_setting_ui_component_properties) won't work.

Kind regards,

Alex

AKlimenko at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 9

Alex,

I've done some research based on this thread, my original blog solution, and your comments to my blog. First, thanks for doing so much digging.

I've talked to a couple of JSF gurus on the Creator engineering team about this JSF anomaly. I've tried many ways to get around the problem including writing my own phaselistener. The bottom line for JSF 1.1 is the UIViewRoot component tree is not accessible until after the first render. This means any component tree walking will not work unless it's done during a postback (button push, etc.)

I've blogged on how to accomplish the same type of thing a different way(http://blogs.sun.com/david/date/20061003). This solution was suggested by Jayashri Visvanathan (http://blogs.sun.com/jayashri) and Matt Bohm.

Thanks!

-David Botterill

Creator Evangelist

DavidBotterill at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 10
David,thank you for your response and the work you've done investigating the problem. It is really helpful information!!!Kind regards,Alex
AKlimenko at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 11

Thanks David. I am using your technique to Read/Write a form. When the form is opened first, all text field are ReadOnly. Then user clicks on "Edit" button. All it does is changes readOnly property of Session bean to false and all the fields (which have ReadOnly binded to #{SessionBean1.readOnly} ) become write-able.

This is a great technique. Before this, I was manually looping through all fields to set ReadOnly to true or false. Now I can re-use my code across many forms. Also, I am using the same technique for visible property of "Edit" and "Save" buttons. Visible property for Edit is bound to session's readOnly as well -- so its hidden when the form is already in edit mode.

Message was edited by:

Sabir

Sabir at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 12

I got the whole point of what you guys been explaining.

Thanks a lot for the research.

I am only sad that people who believed that Sun Java Studio Creator is an RAD IDE are the same people setting the deadlines and also happen to know nothing about these kind of technical problems this whole JSF technology hides within the depts of Sun Forums.

Then I am even more sad that one only comes to realize and really understand these details after a long time experience with JSF.

I happen to have reached the point where nothing about Sun Creator and whole JSF technology is shocking me anymore nor making my nose bleed.

I only wish Sun would have been more straight an honest and would not pretend things are RAD when they're not.

I know countless project manager from all around my job's sector that had a hard time working with "workarrounds"( MacGyver Solutions as I call them. for example: putting an Edit Button on a page because you can't set component states at first page load), disappointed, confused having it hard to keep up with their plannings and deadlines.

Don't get me wrong I appreciate the research you people did, but I just want to underline that if Sun wants to pretend these products / technologies are RAD and professional solutions they are gravely mistaken.

Vector at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 13
Hi there.Putting some force-render-children tags to be the first tag in JSP (i.e. <panelGroup>) helps - component tree is created whole in createView()
romanrezac at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...
# 14

To the original question. It is posible. I have just do it. I don磘 know if it is the right way but it works.

What i did is to create my own ViewHandler. After the renderView phase it is executed the getActionURL. In that moment i have acces to the faceContext wich has the UIViewRoot with the children already in.

I am able to iterate and disable components even in the first access to the page.

I hope it helps someone

Lflores at 2007-7-6 22:17:13 > top of Java-index,Development Tools,Java Tools...