Cuneiform or JSF?
Posted on April 05, 2005 by Scott Leberknight
So if JSF forms only permit POST requests, how would you go about creating a link to submit your form instead of a button? Whereas you use the <h:commandButton>
tag to create an HTML submit button, you use <h:commandLink>
to create a link that will submit your form. So far so good, right? Not so fast. Because JSF only permits forms to be POSTed, the JSF generates an HTML link with an onclick
even handler that sets some hidden form variables and then submits the form. Lots of magic incantations going on here that are out of your control. Check out the following example code.
Suppose you have a form whose id
is "registerForm." Also suppose you want to have several text fields and a "register" link to submit the form. In your JSF page, you write this code:
<h:commandLink value="register"/>
I deployed the JSF page containing this code to Tomcat and then viewed the generated source code. Here it is:
<a href="#" onclick="document.forms['registerForm']['registerForm:_idcl'].value='registerForm:_id6'; document.forms['registerForm'].submit(); return false;">register</a>
In addition to the above, a hidden field was added:
<input type="hidden" name="registerForm:_idcl" />
Is this the kind of code you want your web framework generating? I think I'll pass.
Another fun thing about JSF's generated HTML code is the way it names HTML form fields. It uses the format formName:componentName where formName is the name of the form and componentName is the name of the HTML form control. So in my "registerForm" the code <h:inputText id="password"/>
generates the following:
<input id="registerForm:password" type="text" name="registerForm:password">
This code has the nice little side effect that it cannot be accessed in JavaScript in a simple manner. You cannot simply write document.forms.registerForm.password
. Instead, due to the colon in the element name you are forced to write the following:
document.forms.registerForm["registerForm:password"].value
This seems par for the course so far while learning JSF. Every time you turn around there is some other gotcha or misuse of technology or artificial restriction being placed upon you the developer. Have fun!