JSF: Multiple ‘javax.faces.ViewState’ hidden fields


If you happen to have more than one h:form in your JavaServer Faces page, JSF will generate a hidden input field with javax.faces.ViewState as its name and id for each form. This breaks XHTML compatability because you have more than one element with the same id which isn’t allowed. I recently came across this myself and had some trouble finding a solution on Google. The problem results from an error in the JSF specification itself and thus cannot be solved easily but both major implementations have builtin workarounds:

For MyFaces, you can specify a context parameter in your web.xml called org.apache.myfaces.RENDER_VIEWSTATE_ID and set it to false. It will prevent MyFaces from rendering duplicate ids. For more information please refer to the MyFaces Bugtracking system (MYFACES-1700).

If you use the reference implementation (currently Mojarra), the name of the context parameter for the web.xml is com.sun.faces.enableViewStateIdRendering and you must also set it to false to prevent the RI from generating duplicate ids. For more information, please refer to the JSF-RI FAQ or the bugtracking system (issue 443).

Tags: , , , , , , ,

, , , , , , ,

  1. #1 von pizzi am 1. April 2010 - 15:18

    Hi,

    i’m using Mojarra (1.2_14-b01-FCS) and also if i set
    in web.xml com.sun.faces.enableViewStateIdRendering to FALSE

    the input hidden is rendered one per form …

    any idea ?

  2. #2 von Hendrik Busch am 6. April 2010 - 15:50

    Maybe there is a misunderstanding. Setting this context parameter to false does not prevent the rendering of the hidden fields, it just prevents the generation of the duplicate id attribute of these hidden fields. So if setting this value to false and you have 5 forms, Mojarra will still render 5 hidden fields, they just won’t have the id “javax.faces.ViewState”, because not the duplicate hidden fields themelves cause the problem, but the duplicate IDs.

  3. #3 von Ursula am 12. August 2011 - 14:17

    Danke! Hat sofort geklappt (myFaces)!

  4. #4 von marjan am 5. September 2011 - 10:05

    Has someone fixed the mojarra implementation?
    I have to implement a component tag for handling CSRF, which inputs a token and the decode() method validates whether the token is present. But
    in some forms it will throw exception : Component ID has already been found in the view.
    at com.sun.faces.util.Util.checkIdUniqueness

(wird nicht veröffentlicht)