aspose file tools*
The moose likes JSF and the fly likes Multiple forms and duplicate javax.faces.ViewState ids Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Multiple forms and duplicate javax.faces.ViewState ids" Watch "Multiple forms and duplicate javax.faces.ViewState ids" New topic
Author

Multiple forms and duplicate javax.faces.ViewState ids

iain coghill
Greenhorn

Joined: Apr 19, 2009
Posts: 11
If you have a page with multiple forms each such form has its own hidden ViewState field. This gives rise to warnings when using a validator such as HTML Tidy as each field has an identical id. Is there any way to eliminate this duplication of ids?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15662
    
  15

Welcome to the JavaRanch!

The "id" atribute MUST be unique - it's a stricture that comes from XML. If you don't explicitly provide an ID, the JSF code generators will generate IDs for you, but since they're unpredictable and subject to change, it's better to supply your own IDs for cases where you want to always know what the ID of an item is.

Actually, in JSF, I believe that the rule is that the ID has to be unique within an ID container (such as a form). JSF concatenates the control IDs to their parent IDs, so that the actual HTML id is unique.

However, I don't think that Tidy understands ID containers, so it wants the IDs to be unique per-page, rather than per-container. So, if possible, try and do things that way if you can.


Customer surveys are for companies who didn't pay proper attention to begin with.
iain coghill
Greenhorn

Joined: Apr 19, 2009
Posts: 11
I am pretty sure that an ID attribute should always be unique within an entire document. Browsers are more forgiving as long as the ID is unique within a form as they will not conflict on submission, but it is still wrong and HTML Tidy is correct to flag it.

The problem is that JSF is injecting a hidden field with id="javax.faces.ViewState" for each form. For example:


results in

I don't know how to control the ID of those javax.faces.ViewState fields.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

Indeed, it is extremely important to make sure those id fields are unique.

Even when subviews may not be incorporated into a common form, and the temptation might be to ignore common ids on composite pages, one never knows when a page layout might change in the future. Plus, many helper classes, such as IBM's pagecode class, depends on a unique id across the page/form.

Try your best to keep those ids unique!

-Cameron McKenzie
iain coghill
Greenhorn

Joined: Apr 19, 2009
Posts: 11
Cameron Wallace McKenzie wrote:Indeed, it is extremely important to make sure those id fields are unique.

Even when subviews may not be incorporated into a common form, and the temptation might be to ignore common ids on composite pages, one never knows when a page layout might change in the future. Plus, many helper classes, such as IBM's pagecode class, depends on a unique id across the page/form.

Try your best to keep those ids unique!

-Cameron McKenzie


Indeed, that is why I use the handy Firefox HTML Validator add-on to catch such transgressions. The problem is that this particular hidden field is generated by JSF once per form, and I don't know if or how I can control the id generated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple forms and duplicate javax.faces.ViewState ids
 
Similar Threads
How can I get a form name
Question: Can I validate multiple forms with 1 set of rules? Or multiple fields with 1 rule?
Is Null, Not an Object, or Not Defined
Multiple Forms (in request scope!)
Can i have multiple ActionForms for multiple forms on one page?