0

I am working through an example in the "JSF in action book" which displays a dynamic grid(html table) of numbers driven by an input. The jsp portion is below

<p>
<h:panelGrid id="controlPanel"
binding="#{helloBean.controlPanel}"
columns="20" border="1" cellspacing="0"/>
</p>
<h:commandButton id="redisplayCommand" type="submit"
value="Redisplay"
actionListener="#{helloBean.addControls}"/>

The binding bean code is below

public void addControls(ActionEvent actionEvent)
{
    Application application = FacesContext.getCurrentInstance().getApplication();
    List children = controlPanel.getChildren();
    children.clear();
    for (int count = 0; count < numControls; count++)
    {
        HtmlOutputText output = (HtmlOutputText)application.
        createComponent(HtmlOutputText.COMPONENT_TYPE);
        output.setValue(" " + count + " ");
        output.setStyle("color: blue");
        children.add(output);
    }
}

The code is functional for a few values and then out of nowhere i get this error

"javax.servlet.ServletException: Component ID welcomeForm:j_id51 has already been found in the view"

There doesent seem to be a pattern to when this exception occurs. Is there an way to "drop" a component from its parent?

vinS
  • 1,417
  • 5
  • 24
  • 37
user993797
  • 115
  • 2
  • 2
  • 10

3 Answers3

3

The binding attribute should refer to a request scoped bean property, not a session scoped one.

See also:

That said, depending on the concrete functional requirement, there may be better ways to achieve the requirement using normal UI components.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The bean is defined as request scope in faces-config.xml file request. Can we define scope to individual properties inside a bean? – user993797 Mar 27 '12 at 17:52
  • faces-config.xml? Which JSF version exactly are you using? – BalusC Mar 27 '12 at 17:53
  • I am not entirely sure, but the jar file i am using is javax.faces-2.1.7.jar – user993797 Mar 27 '12 at 17:56
  • I recommend you to download and stick to the same JSF version as mentioned in the book. It surely must be somewhere in the 1.x as old fashioned faces-config.xml way is been used to register the bean. Alternatively, look for a more modern book. JSF 2.x is out for 2.5 years already and creating components in the backing bean is somewhat ... clumsy. – BalusC Mar 27 '12 at 17:57
  • I will switch to a newer book then, however it does bug me that when something is defined in request scope, shouldnt the bean be destroyed right after every call? May be moot at this point. – user993797 Mar 27 '12 at 18:17
  • State saving has changed in JSF 2.x. Try turning off partial state saving by adding a `` with name of `javax.faces.PARTIAL_STATE_SAVING` and value of `false` to `web.xml`. Not sure if that would work out for you, I can't tell from experience as I have never created components in JSF2 this way, but in theory it may solve your concrete problem. Another way is to just downgrade to JSF 1.x JARs. – BalusC Mar 27 '12 at 18:20
0

Check if there are two elements with the same id in the form. In my case, this was the problem.

  • 2
    This is not the case in OP's case. Look closer at the error message: *"Component ID welcomeForm:j_id51 has already been found in the view"*. The component in question has a dynamically generated ID `j_id51` which thus means that the component in question doesn't have a fixed ID set at all which thus means that this problem is impossibly caused by specifying the same fixed ID on multiple components (which is a rather obvious mistake after all). – BalusC Feb 19 '13 at 11:31
-2

put this param to web.xml

    <context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>
lst
  • 27
  • 6
  • I put details. Even though i got same error (Duplicate id). javax.servlet.ServletException: Component ID :j_id22 has already been found in the view. Any other solution to resolve this problem? Help me.Thanks in advance – Eswar Jun 18 '13 at 10:05
  • This did not solve the problem in my case. – Chupacabras Jan 09 '14 at 14:48