You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Saul Qunming Yuan <yu...@dataanvil.com> on 2005/08/25 04:43:41 UTC

How to call a method in the backing bean from a JSF page

Hi,

I'm wondering what's the correct way to call a method in the backing bean from a JSF page. What I tried is to use <h:outputText value="#{theBackingBean.theMethod}" />, the backing bean method returns an empty string, so nothing gets printed out in the page. Which works, but I am not sure this is the correct way to do it. 

thanks,
Saul

Re: How to call a method in the backing bean from a JSF page

Posted by Sean Schofield <se...@gmail.com>.
Check out http://struts.apache.org/shale.  The documentation is pretty
complete and there are nightly build and examples you can download.

sean

On 8/26/05, Rick Reumann <ri...@gmail.com> wrote:
> I did it by just making the managedBean name the same as the jsp. So in your
> case:
>  
>  <managed-bean>
>        <managed-bean-name>fooBar</managed-bean-name>
> 
>  
> 
> On 8/26/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> > This sounds like it may work for me. I never used Shale, so how do I map
> > the backingbean from "fooBar" to "fooBar.jsp" as you mentioned below? in
> > face-config.xml?
> > 
> > thanks.
> > 
> > > On 8/24/05, Saul Qunming Yuan < yuan@dataanvil.com> wrote:
> > >>
> > >> Thanks for your response. I guess I didn't make me clear here. My
> > >> question
> > >> is how to call a method in the backing bean from a JSF page without 
> > >> rendering out anything to the screen.
> > >
> > >
> > >
> > > I would suggest using Shale with JSF. Use the Shale jars and then have
> > > your
> > > backing bean implement a ViewController (I just extend 
> > > AbstractViewController), and then you'll have several nice methods you
> can
> > > override. One is called prerender(). So if you map this backingbean to
> the
> > > correct path, for example "fooBar", when " fooBar.jsp" is called the
> > > prerender method is invoked. That's the ultra simplified explanation and
> > > since I'm new to this, others (Craig) will probably elaborate more. It's
> > > really useful though for exactly what you want. 
> > >
> > >
> > >
> > >
> > > --
> > > Rick
> > >
> > 
> > 
> > 
> 
> 
> 
> -- 
> Rick

Re: How to call a method in the backing bean from a JSF page

Posted by Rick Reumann <ri...@gmail.com>.
I did it by just making the managedBean name the same as the jsp. So in your 
case:

<managed-bean>
<managed-bean-name>fooBar</managed-bean-name>


On 8/26/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> 
> This sounds like it may work for me. I never used Shale, so how do I map
> the backingbean from "fooBar" to "fooBar.jsp" as you mentioned below? in
> face-config.xml?
> 
> thanks.
> 
> > On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> >>
> >> Thanks for your response. I guess I didn't make me clear here. My
> >> question
> >> is how to call a method in the backing bean from a JSF page without
> >> rendering out anything to the screen.
> >
> >
> >
> > I would suggest using Shale with JSF. Use the Shale jars and then have
> > your
> > backing bean implement a ViewController (I just extend
> > AbstractViewController), and then you'll have several nice methods you 
> can
> > override. One is called prerender(). So if you map this backingbean to 
> the
> > correct path, for example "fooBar", when "fooBar.jsp" is called the
> > prerender method is invoked. That's the ultra simplified explanation and
> > since I'm new to this, others (Craig) will probably elaborate more. It's
> > really useful though for exactly what you want.
> >
> >
> >
> >
> > --
> > Rick
> >
> 
> 
> 


-- 
Rick

Re: How to call a method in the backing bean from a JSF page

Posted by Saul Qunming Yuan <yu...@dataanvil.com>.
This sounds like it may work for me. I never used Shale, so how do I map
the backingbean from "fooBar" to "fooBar.jsp" as you mentioned below? in
face-config.xml?

thanks.

> On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
>>
>> Thanks for your response. I guess I didn't make me clear here. My
>> question
>> is how to call a method in the backing bean from a JSF page without
>> rendering out anything to the screen.
>
>
>
> I would suggest using Shale with JSF. Use the Shale jars and then have
> your
> backing bean implement a ViewController (I just extend
> AbstractViewController), and then you'll have several nice methods you can
> override. One is called prerender(). So if you map this backingbean to the
> correct path, for example "fooBar", when "fooBar.jsp" is called the
> prerender method is invoked. That's the ultra simplified explanation and
> since I'm new to this, others (Craig) will probably elaborate more. It's
> really useful though for exactly what you want.
>
>
>
>
> --
> Rick
>



Re: How to call a method in the backing bean from a JSF page

Posted by Sean Schofield <se...@gmail.com>.
I agree with Rick.  Shale is what you want if you want to do something
before rendering.  In fact, there is a prerender method in the
ViewControllr interface for just such an occassion.

sean

On 8/25/05, Rick Reumann <ri...@gmail.com> wrote:
> On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> > Thanks for your response. I guess I didn't make me clear here. My question
> > is how to call a method in the backing bean from a JSF page without
> > rendering out anything to the screen.
> 
>  
>  I would suggest using Shale with JSF. Use the Shale jars and then have your
> backing bean implement a ViewController (I just extend
> AbstractViewController), and then you'll have several nice methods you can
> override. One is called prerender(). So if you map this backingbean to the
> correct path, for example "fooBar", when "fooBar.jsp" is called the
> prerender method is invoked. That's the ultra simplified explanation and
> since I'm new to this, others (Craig) will probably elaborate more. It's
> really useful though for exactly what you want.
>  
> 
> 
> 
> -- 
> Rick

Re: How to call a method in the backing bean from a JSF page

Posted by Rick Reumann <ri...@gmail.com>.
On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> 
> Thanks for your response. I guess I didn't make me clear here. My question
> is how to call a method in the backing bean from a JSF page without
> rendering out anything to the screen.



I would suggest using Shale with JSF. Use the Shale jars and then have your 
backing bean implement a ViewController (I just extend 
AbstractViewController), and then you'll have several nice methods you can 
override. One is called prerender(). So if you map this backingbean to the 
correct path, for example "fooBar", when "fooBar.jsp" is called the 
prerender method is invoked. That's the ultra simplified explanation and 
since I'm new to this, others (Craig) will probably elaborate more. It's 
really useful though for exactly what you want.




-- 
Rick

Re: How to call a method in the backing bean from a JSF page

Posted by Ken Weiner <kw...@gmail.com>.
I am not sure if this is the best way, but one idea that could work is
to create a PhaseListener that acts after the view is restored.  In
this phase listener, you could check if the view being restored is the
view for which you want to call your backing bean method.  Then, you
could obtain a MessageBinding object for your backing bean expression
and use that to call your method.

Create YourPhaseListener and register it in faces-config.xml:
<faces-config>
    ...
    <lifecycle>
        <phase-listener>com.yourcompany.YourPhaseListener</phase-listener>
    </lifecycle>
</faces-config>

public class YourPhaseListener implements PhaseListener {

    public void beforePhase(PhaseEvent event) {
        // Nothing to do
    }

    public void afterPhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        UIViewRoot viewRoot = facesContext.getViewRoot();
        String idOfYourPage = ...
        if (viewRoot.getId().equals(idOfYourPage)) {
            Application application = facesContext.getApplication();
            String methodExpression = ...
            Class[] params = ...
            MethodBinding methodBinding =
application.getMessageBinding(methodExpression, params);
            methodBinding.invoke(facesContext, ...);
    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }

}

Disclaimer: I haven't tried this code so I don't know if it would work
or even compile.

-Ken

On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> Thanks for your response. I guess I didn't make me clear here. My question
> is how to call a method in the backing bean from a JSF page without
> rendering out anything to the screen.

Re: How to call a method in the backing bean from a JSF page

Posted by Saul Qunming Yuan <yu...@dataanvil.com>.
Thanks for your response. I guess I didn't make me clear here. My question 
is how to call a method in the backing bean from a JSF page without 
rendering out anything to the screen.


----- Original Message ----- 
From: "Sean Schofield" <se...@gmail.com>
To: "MyFaces Discussion" <us...@myfaces.apache.org>
Sent: Wednesday, August 24, 2005 10:04 PM
Subject: Re: How to call a method in the backing bean from a JSF page


> The value attribute should be a *value* binding expression.  It must
> bind to a *property*.  So you do something like this ...
>
> value=#{theBackingBean.foo}"
>
> and in your backing bean you have
>
> public String getFoo()
> { ... }
>
> public void setFoo(String fooValue)
> { ... }
>
>
> sean
>
> On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
>>
>> Hi,
>>
>> I'm wondering what's the correct way to call a method in the backing bean
>> from a JSF page. What I tried is to use <h:outputText
>> value="#{theBackingBean.theMethod}" />, the backing bean method returns 
>> an
>> empty string, so nothing gets printed out in the page. Which works, but I 
>> am
>> not sure this is the correct way to do it.
>>
>> thanks,
>> Saul 


Re: How to call a method in the backing bean from a JSF page

Posted by Sean Schofield <se...@gmail.com>.
The value attribute should be a *value* binding expression.  It must
bind to a *property*.  So you do something like this ...

value=#{theBackingBean.foo}"

and in your backing bean you have

public String getFoo()
{ ... }

public void setFoo(String fooValue)
{ ... }


sean

On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
>  
> Hi, 
>   
> I'm wondering what's the correct way to call a method in the backing bean
> from a JSF page. What I tried is to use <h:outputText
> value="#{theBackingBean.theMethod}" />, the backing bean method returns an
> empty string, so nothing gets printed out in the page. Which works, but I am
> not sure this is the correct way to do it. 
>   
> thanks, 
> Saul

Re: How to call a method in the backing bean from a JSF page

Posted by ss...@o2.pl.
but remember that getter may be called few times!

i assume that in order to see your page user must click sth (ex.
somme kind of menu) and i assume that u have action binded to that
click that returs string maped to desired page. so just add call
your method in that action.

Słąwek Sobótka


> Thanks for your reply, that makes sense. But won't h:commandLink
render a 
> link and h:commandButton render a button in the page? I don't want to 
> display anything on the screen. My purpose is to initiate a call
from the 
> JSF page to a method in the backing bean when the JSF page loads. I'm 
> returning an empty string from the method so nothing gets printed
out from 
> <h:outputText>.
> 
> 
> ----- Original Message ----- 
> From: "Ken" <ke...@gmail.com>
> To: "MyFaces Discussion" <us...@myfaces.apache.org>
> Sent: Wednesday, August 24, 2005 9:53 PM
> Subject: Re: How to call a method in the backing bean from a JSF page
> 
> 
> > Try h:commandLink or h:commandButton.
> > If your method returns a string that doesn't match any
navigation-rule
> > in your faces-confix.xml then the same page you came from will be
> > rendered again.
> >
> >
> > On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> >>
> >> Hi,
> >>
> >> I'm wondering what's the correct way to call a method in the
backing bean
> >> from a JSF page. What I tried is to use <h:outputText
> >> value="#{theBackingBean.theMethod}" />, the backing bean method
returns 
> >> an
> >> empty string, so nothing gets printed out in the page. Which
works, but I 
> >> am
> >> not sure this is the correct way to do it.
> >>
> >> thanks,
> >> Saul 
> 
> 


Re: How to call a method in the backing bean from a JSF page

Posted by Volker Weber <us...@weber-oldenburg.de>.
Hi,
i think you are looking for somthing like a PhaseListener.

You can configure such a Listener in faces.xml.

make your listener to return javax.faces.event.PhaseId.RENDER_RESPONSE
in getPhaseId() method and the beforePhase() method of your listener
is invoked just before rendering.

Saul Qunming Yuan wrote:
> I see. The outputText way is actually what I did. So, is there a way to
> call a method in the backing bean when/before loading a JSF page?
> Something like a pre-render method before loading a JSF page? I can do
> that in the backing bean methods that forward to the JSF page, but there
> are many places that could forward to that JSF page. That's why I trying
> to do it in the JSF page itself.
> 
> thanks

-- 
Don't answer to From: address!
Mail to this account are droped if not recieved via mailinglist.
To contact me direct create the mail address by
concatenating my forename to my senders domain.

Re: How to call a method in the backing bean from a JSF page

Posted by Saul Qunming Yuan <yu...@dataanvil.com>.
I see. The outputText way is actually what I did. So, is there a way to call 
a method in the backing bean when/before loading a JSF page? Something like 
a pre-render method before loading a JSF page? I can do that in the backing 
bean methods that forward to the JSF page, but there are many places that 
could forward to that JSF page. That's why I trying to do it in the JSF page 
itself.

thanks


----- Original Message ----- 
From: "Ken" <ke...@gmail.com>
To: "MyFaces Discussion" <us...@myfaces.apache.org>
Sent: Wednesday, August 24, 2005 10:17 PM
Subject: Re: How to call a method in the backing bean from a JSF page


> Yes, commandLink and commandButton will render an anchor tag.  If you
> don't want to do this you can do work in your properies getter method
> when it gets called by outputText value and return an empty string.
> However, I don't believe there is any guarante as to call order.
> Putting your target outputText in the first line of your jsp file
> doesn't mean it will get called before other getter methods further
> down in the jsp file get called.
>
>
> On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
>> Thanks for your reply, that makes sense. But won't h:commandLink render a
>> link and h:commandButton render a button in the page? I don't want to
>> display anything on the screen. My purpose is to initiate a call from the
>> JSF page to a method in the backing bean when the JSF page loads. I'm
>> returning an empty string from the method so nothing gets printed out 
>> from
>> <h:outputText>.
>>
>>
>> ----- Original Message -----
>> From: "Ken" <ke...@gmail.com>
>> To: "MyFaces Discussion" <us...@myfaces.apache.org>
>> Sent: Wednesday, August 24, 2005 9:53 PM
>> Subject: Re: How to call a method in the backing bean from a JSF page
>>
>>
>> > Try h:commandLink or h:commandButton.
>> > If your method returns a string that doesn't match any navigation-rule
>> > in your faces-confix.xml then the same page you came from will be
>> > rendered again.
>> >
>> >
>> > On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
>> >>
>> >> Hi,
>> >>
>> >> I'm wondering what's the correct way to call a method in the backing 
>> >> bean
>> >> from a JSF page. What I tried is to use <h:outputText
>> >> value="#{theBackingBean.theMethod}" />, the backing bean method 
>> >> returns
>> >> an
>> >> empty string, so nothing gets printed out in the page. Which works, 
>> >> but I
>> >> am
>> >> not sure this is the correct way to do it.
>> >>
>> >> thanks,
>> >> Saul
>>
>> 


Re: How to call a method in the backing bean from a JSF page

Posted by Ken <ke...@gmail.com>.
Yes, commandLink and commandButton will render an anchor tag.  If you
don't want to do this you can do work in your properies getter method
when it gets called by outputText value and return an empty string. 
However, I don't believe there is any guarante as to call order. 
Putting your target outputText in the first line of your jsp file
doesn't mean it will get called before other getter methods further
down in the jsp file get called.


On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> Thanks for your reply, that makes sense. But won't h:commandLink render a
> link and h:commandButton render a button in the page? I don't want to
> display anything on the screen. My purpose is to initiate a call from the
> JSF page to a method in the backing bean when the JSF page loads. I'm
> returning an empty string from the method so nothing gets printed out from
> <h:outputText>.
> 
> 
> ----- Original Message -----
> From: "Ken" <ke...@gmail.com>
> To: "MyFaces Discussion" <us...@myfaces.apache.org>
> Sent: Wednesday, August 24, 2005 9:53 PM
> Subject: Re: How to call a method in the backing bean from a JSF page
> 
> 
> > Try h:commandLink or h:commandButton.
> > If your method returns a string that doesn't match any navigation-rule
> > in your faces-confix.xml then the same page you came from will be
> > rendered again.
> >
> >
> > On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
> >>
> >> Hi,
> >>
> >> I'm wondering what's the correct way to call a method in the backing bean
> >> from a JSF page. What I tried is to use <h:outputText
> >> value="#{theBackingBean.theMethod}" />, the backing bean method returns
> >> an
> >> empty string, so nothing gets printed out in the page. Which works, but I
> >> am
> >> not sure this is the correct way to do it.
> >>
> >> thanks,
> >> Saul
> 
>

Re: How to call a method in the backing bean from a JSF page

Posted by Saul Qunming Yuan <yu...@dataanvil.com>.
Thanks for your reply, that makes sense. But won't h:commandLink render a 
link and h:commandButton render a button in the page? I don't want to 
display anything on the screen. My purpose is to initiate a call from the 
JSF page to a method in the backing bean when the JSF page loads. I'm 
returning an empty string from the method so nothing gets printed out from 
<h:outputText>.


----- Original Message ----- 
From: "Ken" <ke...@gmail.com>
To: "MyFaces Discussion" <us...@myfaces.apache.org>
Sent: Wednesday, August 24, 2005 9:53 PM
Subject: Re: How to call a method in the backing bean from a JSF page


> Try h:commandLink or h:commandButton.
> If your method returns a string that doesn't match any navigation-rule
> in your faces-confix.xml then the same page you came from will be
> rendered again.
>
>
> On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
>>
>> Hi,
>>
>> I'm wondering what's the correct way to call a method in the backing bean
>> from a JSF page. What I tried is to use <h:outputText
>> value="#{theBackingBean.theMethod}" />, the backing bean method returns 
>> an
>> empty string, so nothing gets printed out in the page. Which works, but I 
>> am
>> not sure this is the correct way to do it.
>>
>> thanks,
>> Saul 


Re: How to call a method in the backing bean from a JSF page

Posted by Ken <ke...@gmail.com>.
Try h:commandLink or h:commandButton.
If your method returns a string that doesn't match any navigation-rule
in your faces-confix.xml then the same page you came from will be
rendered again.


On 8/24/05, Saul Qunming Yuan <yu...@dataanvil.com> wrote:
>  
> Hi, 
>   
> I'm wondering what's the correct way to call a method in the backing bean
> from a JSF page. What I tried is to use <h:outputText
> value="#{theBackingBean.theMethod}" />, the backing bean method returns an
> empty string, so nothing gets printed out in the page. Which works, but I am
> not sure this is the correct way to do it. 
>   
> thanks, 
> Saul