Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Call Client Side Javascript from Java in JSF

Thread view: 
Ricardo Ríos - 29 Aug 2007 00:32 GMT
Hello wizards, I have a JSF application, in this application I have a
button, but in the action method for this button I need to call to the
following code:

<SCRIPT language="JavaScript">
<!-- hide from old browsers
alert('Error');
//-->
</SCRIPT>

How can I do that ?  It is possible ? Thanks in advance.
Carl - 29 Aug 2007 00:49 GMT
> Hello wizards, I have a JSF application, in this application I have a
> button, but in the action method for this button I need to call to the
[quoted text clipped - 7 lines]
>
> How can I do that ?  It is possible ? Thanks in advance.

This question more a javascript question than a Java/JSF question. But while
you're here I might as well suggest you try putting your javascript statement
in the buttons onClick event handler:

<input type="button" value="Click Me!" onClick="alert('WooHoo!');" />

Hope that helps,
Carl.
lord.tyranus.96@gmail.com - 29 Aug 2007 01:06 GMT
> Ricardo R?os <ricardo.rios...@gmail.com> writes:
> > Hello wizards, I have a JSF application, in this application I have a
[quoted text clipped - 17 lines]
> Hope that helps,
> Carl.

Thanks for your reply, but in the action method for this button I need
a server side validation code , for example:

 public String button1_action()
 {
     boolean test = false;

     test = this.getSessionBean1.validateForm(this.layoutPanel1);

     if (test)
     /* Here need to call the following code
     <SCRIPT language="JavaScript">
     <!-- hide from old browsers
       alert('Ok');
      //-->
      </SCRIPT>
      */
     else
     /* Here need to call the following code
     <SCRIPT language="JavaScript">
     <!-- hide from old browsers
       alert('Error');
      //-->
      </SCRIPT>
      */

     return null;
 }
Lew - 29 Aug 2007 01:48 GMT
>>> Hello wizards, I have a JSF application, in this application I have a
>>> button, but in the action method for this button I need to call to the
[quoted text clipped - 42 lines]
>       return null;
>   }

When you increased the level of specification of your question it changed the
answer.

It sounds like you need something more AJAXish, where you have JS submit to
the server-side validation code then parse the response to decide what dialog
to pop up.

OTOH, as long as you're doing a round-trip to the server to validate anyway,
why not abandon this silly Javascript and just pop up localized error messages
on the JSP itself, a technique supported directly by JSF?
<http://java.sun.com/javaee/javaserverfaces/1.2/docs/tlddocs/h/message.html>
<http://java.sun.com/javaee/javaserverfaces/1.2/docs/tlddocs/h/messages.html>

Signature

Lew

Lew - 29 Aug 2007 01:35 GMT
Ricardo Ríos writes:
>> Hello wizards, I have a JSF application, in this application I have a
>> button, but in the action method for this button I need to call to the
[quoted text clipped - 7 lines]
>>
>> How can I do that ?  It is possible ? Thanks in advance.

> you're here I might as well suggest you try putting your javascript statement
> in the buttons onClick event handler:
>
> <input type="button" value="Click Me!" onClick="alert('WooHoo!');" />

JSF tags support the onclick attribute, too, e.g.,
<http://java.sun.com/javaee/javaserverfaces/1.2/docs/tlddocs/h/commandButton.html>

This is a legitimate Java question in my book, since it's about JSP / JSF tag
libraries.  The question is a bit misleading from the solution, though, making
it superficially resemble a JS question.  In actuality, the solution Carl
recommends, and I endorse, is not to have "the action method for this button
... call" any Java, but conversely to have Java generate the JS to be called
directly on the client side.  It should produce the user experience you seek.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.