You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by bansi <ma...@yahoo.com> on 2008/01/24 20:13:48 UTC

Is it possible for JSF h:commandButton to execute MethodBinding and JavaScript

I have a requirement where i wanna execute both Method Binding and JavaScript
Code at the same time using JSF h:commandButton.

The Javascript will open a popup while MethodBinding will set the values in
Backing Bean

Here is the snippet
<h:commandButton id="findContact" value="Manage Contacts" immediate="true"
    onmousedown="createPopUp('contactLookup','find')" 
    onclick="return false" image="/images/contacts.gif" border="0"
alt="Click here to Manage Contacts"
    actionListener="#{contactBean.getFormVariables}">
    	 <f:attribute name="formName" value="deviceForm" />
    	<f:attribute name="fieldName" value="contact" />
    	 					 
    </h:commandButton>

Any pointers/suggestions will be highly appreciated
-- 
View this message in context: http://www.nabble.com/Is-it-possible-for-JSF-h%3AcommandButton-to-execute-MethodBinding-and-JavaScript-tp15072394p15072394.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


RE: Is it possible for JSF h:commandButton to execute MethodBinding and JavaScript

Posted by "Goda, Sunil" <Su...@Kenexa.com>.
 
Hey Bansi,

Ajax4Jsf can help with this. You can use <a4j:support> which has attribute - 'oncomplete' .This attribute can be used to invoke JavaScript you wish to and you can use other attribs like 'action','actionListener' to invoke the action methods.

Regards,
Sunil G


-----Original Message-----
From: bansi [mailto:mail2bansi@yahoo.com] 
Sent: Friday, January 25, 2008 11:10 PM
To: users@myfaces.apache.org
Subject: Re: Is it possible for JSF h:commandButton to execute MethodBinding and JavaScript


Thanks Andrew/David
I am able to do both but it doesnt happen in sequence as  expected. 
As David correctly said "It's alwasy javascript onclick, followed by an http method call"
But i wanna  do Method Binding first then Javascript OnClick.
Any pointers/suggestions will be highly appreciated Here is the snippet which does both <h:commandButton id="findContact" value="Manage Contacts" immediate="true"
onmousedown="createPopUp('contactLookup','find')" 
onclick="return false" image="/images/contacts.gif" border="0" alt="Click here to Manage Contacts"
actionListener="#{contactBean.getFormVariables}">
<f:attribute name="formName" value="deviceForm" /> <f:attribute name="fieldName" value="contact" />

</h:commandButton>

The reason i wanna do Method Binding first is i am passing parameters based on which dataTable in popup xhtml should be rendered


David Delbecq-2 wrote:
> 
> Javascript = Client side
> ActionListeners / Actions = Server Side
> 
> So, answer is "no", you can't synchronize a javascript with a server 
> code. It's alwasy javascript onclick, followed by an http method call 
> (which will trigger all JSF stuffs) bansi a écrit :
>> I have a requirement where i wanna execute both Method Binding and 
>> JavaScript Code at the same time using JSF h:commandButton.
>>
>> The Javascript will open a popup while MethodBinding will set the 
>> values in Backing Bean
>>
>> Here is the snippet
>> <h:commandButton id="findContact" value="Manage Contacts"
>> immediate="true"
>>     onmousedown="createPopUp('contactLookup','find')" 
>>     onclick="return false" image="/images/contacts.gif" border="0"
>> alt="Click here to Manage Contacts"
>>     actionListener="#{contactBean.getFormVariables}">
>>     	 <f:attribute name="formName" value="deviceForm" />
>>     	<f:attribute name="fieldName" value="contact" />
>>     	 					 
>>     </h:commandButton>
>>
>> Any pointers/suggestions will be highly appreciated
>>   
> 
> 
> 

--
View this message in context: http://www.nabble.com/Is-it-possible-for-JSF-h%3AcommandButton-to-execute-MethodBinding-and-JavaScript-tp15072394p15093039.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Is it possible for JSF h:commandButton to execute MethodBinding and JavaScript

Posted by bansi <ma...@yahoo.com>.
Thanks Andrew/David
I am able to do both but it doesnt happen in sequence as  expected. 
As David correctly said "It's alwasy javascript onclick, followed by an http
method call"
But i wanna  do Method Binding first then Javascript OnClick.
Any pointers/suggestions will be highly appreciated
Here is the snippet which does both 
<h:commandButton id="findContact" value="Manage Contacts" immediate="true"
onmousedown="createPopUp('contactLookup','find')" 
onclick="return false" image="/images/contacts.gif" border="0" alt="Click
here to Manage Contacts"
actionListener="#{contactBean.getFormVariables}">
<f:attribute name="formName" value="deviceForm" />
<f:attribute name="fieldName" value="contact" />

</h:commandButton>

The reason i wanna do Method Binding first is i am passing parameters based
on which dataTable in popup xhtml should be rendered


David Delbecq-2 wrote:
> 
> Javascript = Client side
> ActionListeners / Actions = Server Side
> 
> So, answer is "no", you can't synchronize a javascript with a server 
> code. It's alwasy javascript onclick, followed by an http method call 
> (which will trigger all JSF stuffs)
> bansi a écrit :
>> I have a requirement where i wanna execute both Method Binding and
>> JavaScript
>> Code at the same time using JSF h:commandButton.
>>
>> The Javascript will open a popup while MethodBinding will set the values
>> in
>> Backing Bean
>>
>> Here is the snippet
>> <h:commandButton id="findContact" value="Manage Contacts"
>> immediate="true"
>>     onmousedown="createPopUp('contactLookup','find')" 
>>     onclick="return false" image="/images/contacts.gif" border="0"
>> alt="Click here to Manage Contacts"
>>     actionListener="#{contactBean.getFormVariables}">
>>     	 <f:attribute name="formName" value="deviceForm" />
>>     	<f:attribute name="fieldName" value="contact" />
>>     	 					 
>>     </h:commandButton>
>>
>> Any pointers/suggestions will be highly appreciated
>>   
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-for-JSF-h%3AcommandButton-to-execute-MethodBinding-and-JavaScript-tp15072394p15093039.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.


Re: Is it possible for JSF h:commandButton to execute MethodBinding and JavaScript

Posted by David Delbecq <de...@oma.be>.
Javascript = Client side
ActionListeners / Actions = Server Side

So, answer is "no", you can't synchronize a javascript with a server 
code. It's alwasy javascript onclick, followed by an http method call 
(which will trigger all JSF stuffs)
bansi a écrit :
> I have a requirement where i wanna execute both Method Binding and JavaScript
> Code at the same time using JSF h:commandButton.
>
> The Javascript will open a popup while MethodBinding will set the values in
> Backing Bean
>
> Here is the snippet
> <h:commandButton id="findContact" value="Manage Contacts" immediate="true"
>     onmousedown="createPopUp('contactLookup','find')" 
>     onclick="return false" image="/images/contacts.gif" border="0"
> alt="Click here to Manage Contacts"
>     actionListener="#{contactBean.getFormVariables}">
>     	 <f:attribute name="formName" value="deviceForm" />
>     	<f:attribute name="fieldName" value="contact" />
>     	 					 
>     </h:commandButton>
>
> Any pointers/suggestions will be highly appreciated
>   


Re: Is it possible for JSF h:commandButton to execute MethodBinding and JavaScript

Posted by Andrew Robinson <an...@gmail.com>.
There are different approaches, but it can be done. The easiest way is to
set the target of the command button to a new window. Then the action method
can be responsible for loading a popup page in that window. I avoid popups
like the plague because of browser blocking, opening in window vs. tab, etc.
In those cases it would be better to use a light weight popup solution (see
trinidad or tomahawk popup).

If those still aren't what you want, you can use ajax to have your button
invoke a method on the server then send down some javascript that will open
the window. The AJAX will allow you to avoid refreshing the current page
when the button is clicked. Implementation details would be different based
on which AJAX library you go with.

Jenia4Faces also has an IFRAME based popup solution that may be what you
want if you don't need a new window:

http://www.jenia.org/

-Andrew

On Jan 24, 2008 12:13 PM, bansi <ma...@yahoo.com> wrote:

>
> I have a requirement where i wanna execute both Method Binding and
> JavaScript
> Code at the same time using JSF h:commandButton.
>
> The Javascript will open a popup while MethodBinding will set the values
> in
> Backing Bean
>
> Here is the snippet
> <h:commandButton id="findContact" value="Manage Contacts" immediate="true"
>    onmousedown="createPopUp('contactLookup','find')"
>    onclick="return false" image="/images/contacts.gif" border="0"
> alt="Click here to Manage Contacts"
>    actionListener="#{contactBean.getFormVariables}">
>         <f:attribute name="formName" value="deviceForm" />
>        <f:attribute name="fieldName" value="contact" />
>
>    </h:commandButton>
>
> Any pointers/suggestions will be highly appreciated
> --
> View this message in context:
> http://www.nabble.com/Is-it-possible-for-JSF-h%3AcommandButton-to-execute-MethodBinding-and-JavaScript-tp15072394p15072394.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>

Re: Is it possible for JSF h:commandButton to execute MethodBinding and JavaScript

Posted by bansi <ma...@yahoo.com>.
Thanks Everyone for your suggestions. I thought i give one more try before
looking at Trinidad etc.
Using Hidden Field i.e. passing hidden field value 
When i access the input hidden value in other bean as follows it give a
value null
hiddenId = (String)FacesContext.getCurrentInstance().getExternalContext()
        .getRequestParameterMap().get("hiddenInput");
		System.out.println("In getHiddenInput="+hiddenId);

Here is snippet in xhtml file

<h:inputHidden name="hiddenInput" value="55" />
				<h:commandButton id="findContact" value="Manage Contacts" 
        						immediate="true"  action="#{contactBean.getFormVariables}"  	  						
    	 					  onclick ="createPopUp('contactLookup','find');"  
image="/images/contacts.gif" border="0" alt="Click here to Manage Contacts"
    	 					 
    	 					 >


Tomek Gołembiewski wrote:
> 
> There is a esiest way to do that, if You use a trinidad just use a
> tr:commandButton
>   or tr:commandLink set partialSubmit to true,look with the fire bug
> at the  post parameters, put  the same  parameters  into
> javascript method fte:
> 
> var form = document.getElementById('your_form_id');
> TrPage.getInstance().sendPartialFormPost(form,{event: action, source:
> "your_component_id",partial: "true"},{});
> 
> put this method into your own javascript method with anything You like
> - fte opening popup
> set Your new method into onClick or onMouseDown property of your
> tr:button and voila.
> Your method in backing bean is fired and Your javascript is runing too.
> If You put sendPartialFormPost fisrt the method binding is fired
> before openDialog js.
> To be 100% sure that js is fired always after the backing bean method
> U can attach a
> TrPage.getInstance().getRequestQueue().addStateChangeListener(myCallback);
> to check if method binding finished before runing other js.
> 
> Regards Tomek
> 
> On Jan 25, 2008 7:24 PM, Gary VanMatre <gv...@comcast.net> wrote:
>>
>>
>> You might give shale remoting a look[1].  I have not tried to use it with
>> Trinidad but all I think you would need is the shale-core and
>> shale-remoting jars.
>>
>> [1] http://shale.apache.org/shale-remoting/index.html
>>
>> Gary
>>
>>
>> -------------- Original message --------------
>> From: bansi <ma...@yahoo.com>
>>
>> >
>> > Thanks Andrew/David
>> > I am able to do both but it doesnt happen in sequence as expected.
>> > As David correctly said "It's alwasy javascript onclick, followed by an
>> http
>> > method call"
>> > But i wanna do Method Binding first then Javascript OnClick.
>> > Any pointers/suggestions will be highly appreciated
>> > Here is the snippet which does both
>>
>> > > onmousedown="createPopUp('contactLookup','find')"
>> > onclick="return false" image="/images/contacts.gif" border="0"
>> alt="Click
>> > here to Manage Contacts"
>> > actionListener="#{contactBean.getFormVariables}">
>> >
>> >
>> >
>> >
>> >
>> > The reason i wanna do Method Binding first is i am passing parameters
>> based
>> > on which dataTable in popup xhtml should be rendered
>> >
>> >
>> > David Delbecq-2 wrote:
>> > >
>> > > Javascript = Client side
>> > > ActionListeners / Actions = Server Side
>> > >
>> > > So, answer is "no", you can't synchronize a javascript with a server
>> > > code. It's alwasy javascript onclick, followed by an http method call
>> > > (which will trigger all JSF stuffs)
>> > > bansi a écrit :
>>
>> > >> I have a requirement where i wanna execute both Method Binding and
>> > >> JavaScript
>> > >> Code at the same time using JSF h:commandButton.
>> > >>
>> > >> The Javascript will open a popup while MethodBinding will set th e
>> values
>>
>> > >> in
>> > >> Backing Bean
>> > >>
>> > >> Here is the snippet
>>
>> > >> > >> immediate="true"
>> > >> onmousedown="createPopUp('contactLookup','find')"
>> > >> onclick="return false" image="/images/contacts.gif" border="0"
>> > >> alt="Click here to Manage Contacts"
>> > >> actionListener="#{contactBean.getFormVariables}">
>> > >>
>> > >>
>> > >>
>> > >>
>> > >>
>>
>> > >> Any pointers/suggestions will be highly appreciated
>> > >>
>> > >
>> > >
>> > >
>> >
>> > --
>> > View this message in context:
>> > http://www.nabble.com/Is-it-possible-for-JSF-h%3AcommandButto n-to-e
>> xecute-Method
>> > Binding-and-JavaScript-tp15072394p15093039.html
>>
>> > Sent from the MyFaces - Users mailing list archive at Nabble.com.
>> >
> 
> 

-- 
View this message in context: http://www.nabble.com/Is-it-possible-for-JSF-h%3AcommandButton-to-execute-MethodBinding-and-JavaScript-tp15072394p15115338.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.