473,385 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

How to use JavaScript in JSF

acoder
16,027 Expert Mod 8TB
JavaScript is used for a variety of tasks on the client-side. When using JavaScript with plain HTML code, we can quite easily access elements on the page. For example, the text box in the following HTML code:
[HTML]<form name="someForm" id="someForm">
<input type="text" name="someName" id="someID" value="">
</form>[/HTML]can be accessed like this:
Expand|Select|Wrap|Line Numbers
  1. var textbox = document.getElementById("someID");
  2. // or an alternative way via the elements array:
  3. textbox = document.forms["someForm"].elements["someName"];
What if we want to use JavaScript with JSF (Java Server Faces)?

In JSF or, for that matter, any server-side generated code, we need to look at the what the code looks like on the client-side.

First of all, have a look at this code by luxalexis:
[HTML]<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://geportal.ge.com/faces" prefix="g" %>

<f:view>
<script type="text/javascript">
function valid(form)
{

var Value = form["FormName:name"].value;
if (Value == "")
{
alert("Please enter Subject");
form["FormName:name"].focus();
return false;
}
form.submit();
return true;
}
</script>
<h:form id="FormName">

<h:outputText styleClass="jsf-output-text" value="Enter your name"/>
<h:panelGroup>
<h:inputText styleClass="jsf-output-text" id="name" value="#

{BeanName.attribute}" required="true"/>
<h:commandButton value="Submit" type="submit" styleClass="jsf-command-button"

action="#{BeanName.function}" onclick="return valid(this.form);"/>
</h:panelGroup>

</h:form>
</f:view>[/HTML]The code validates the input field (checks that it has a value entered) before the form is submitted.

The question is how do we access the text field to check. For that, we need to see how the JSF component renders on the client-side, in particular the name/id. The input field:
Expand|Select|Wrap|Line Numbers
  1. <h:inputText styleClass="jsf-output-text" id="name" value="#{BeanName.attribute}" required="true"/>
generates the following:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" ... id="FormName:name" name="FormName:name" ...>
So, the id is converted to a name/id in the following format: [Form Name]:[id]. We can use this to access the input element. The code above makes use of the passed form variable: form["FormName:name"]. This could also have been:
Expand|Select|Wrap|Line Numbers
  1. var Value = document.getElementById("FormName:name").value;
  2. // or alternatively:
  3. form.elements["FormName:name"].value;
Of course, the form validation could be performed via an onsubmit, i.e.
Expand|Select|Wrap|Line Numbers
  1. <h:form id="FormName" onsubmit="return valid(this)"> 
Conclusion
We've seen how we can use JavaScript to access JSF components. The trick is to understand how JSF renders the components into HTML. Once we access the elements with JavaScript, we can validate, show/hide, and otherwise modify the elements as we can with normal HTML. This is true of any server-side generated code.

Hope you enjoy and happy coding!
Sep 17 '07 #1
1 69316
acoder
16,027 Expert Mod 8TB
This was an article written from code that was posted. If you have any specific questions about JSF, ask in the Java forum.
Nov 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.