Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple forms and duplicate javax.faces.ViewState ids

 
iain coghill
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18156
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
iain coghill
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic