0

I' have a simple problem with adding values from inputs to the ArrayList.

I have a POJO like this:

public class Person {

    private String firstName;
    private String lastName;
    private List<String> friends=new ArrayList<>();
    //getters and setters

then Backing bean:

  public class backingBean{
          Person p=new Person();

          public void addPerson(){

             for(String friend:p.getFriends)
                System.out.println(friend);
           }


}

and the view

        <h:form>
            <fieldset>
                <h:panelGrid columns="2">

                    <h:outputText value="Name" />
                    <h:inputText value="{backingBean.person.firstName}"/>              

                    <h:outputText value="LastName" />
                    <h:inputText value="#{backingBean.person.lastName}"/>


                    <h:outputText value="Friends" />
                    <h:inputText value="#{backingBean.person.friends}" />
                    <h:inputText value="#{backingBean.person.friends}" />

                </h:panelGrid>
                <h:commandButton  value="Add"
                    action="#{backingBean.addPerson}" />

            </fieldset>
        </h:form>

When I try to addPerson I get this error:

summary=(Conversion Error setting value...

I don't understand why convert String to String?

Mitja Rogl
  • 894
  • 9
  • 22
  • 39

2 Answers2

2

You can't bind value of h:inputText to ArrayList (without converter). When you submit form (by clicking button) JSF tries to call setFriends(String) and this is where this Exception occurs. Try to figure out what you are trying to achieve with these two h:inputText elements.

partlov
  • 13,789
  • 6
  • 63
  • 82
0

if you want to add 2 friends, just create only 2 different variables in backing bean as :

private String friend1;
private String friend2;

and then add them in addPerson like this:

    List<String> friends=new ArrayList<String>();
    friends.add(friend1);
    friends.add(friend2);

    p.setFriends(friends);

Not tested can be some bugs.

EDIT:

And if this not satisfies you, you can look at this @BalusC ANSWER

Community
  • 1
  • 1
Darka
  • 2,762
  • 1
  • 14
  • 31