Mitja Rogl
Mitja Rogl

Reputation: 884

Adding values from multiple inputs to the arraylist

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?

Upvotes: 0

Views: 1920

Answers (2)

Darka
Darka

Reputation: 2768

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

Upvotes: 0

partlov
partlov

Reputation: 14277

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.

Upvotes: 2

Related Questions