On 5/1/06, 王曾wang_zeng <[EMAIL PROTECTED]> wrote:

2006/4/28, Craig McClanahan <[EMAIL PROTECTED]>:
>
> You are correct.  There is special handling defined in the Restore View
> phase.  If that phase discovers that there is no state to be restored
> , then JSF will immediately forward to Render Response phase.
>
> So, how do you make sure that the right dynamic data gets loaded so that
> the
> page displays the right stuff?  That's where Shale comes in handy.  If
> your
> backing bean implements the ViewController interface, then prerender()
> will
> get called just before the JSP page is invoked.  This is the perfect
place
> to grab any data you need from your database to display the requested
> page.
>
> Craig
>
>
Craig, When the page is first viewed,If I want to dynamically set the
inital
value of the property  of  the UI component (for exsample, text of a
textbox), how can I set the value of it. Even if I crab the data in
prerender() method, how can I get the reference to the component without a
tree in session, when Restore the View phase is skiped.


Two approaches to this are fairly common.

* Use the "binding" attribute to bind a component instance into your backing
 bean, which will get used when the component tree is actually constructed:

 <h:outputText binding="#{backing.dynamicText}" .../>

 public class Backing implements ViewController {

   private HtmlOutputText dynamicText = new HtmlOutputText();
   public HtmlOutputText getDynamicText() { return this.dynamicText; }
   public void setDynamicText(HtmlOutputText dynamicText) {
this.dynamicText = dynamicText; }
   ...
   public void prerender() {
       dynamicText.setVaue("Dynamically calculated value");
   }

 }

* Use the "value" attribute to bind the component's value property to
 a string property of your backing bean

 <h:outputText value="#{backing.dynamicValue}" .../>

 public class Backing implements ViewController {

   private String dynamicValue;
   public String getDynamicValue() { return this.dynamicValue; }
   public void setDynamicValue(String dynamicValue) { this.dynamicValue =
dynamicValue; }
   ...
   public void prerender() {
       dynamicVaue = "Dynamically calculated value";
   }

 }

The first technique is convenient when you need to dynamically calculate
more than one property of the component (just make sure you don't also try
to set the corresponding attributes in the JSP page).  The second technique
is simpler when you just want to dynamically bind the "value" property, and
also makes it a bit easier to build unit tests for the backing bean class.

--
Wang Zeng



Craig

Reply via email to