如何使用jsf RequestScope操作Java List

时间:2014-04-20 09:16:52

标签: jsf jsf-2 jsf-2.2

我正在使用JSF 2.2和RequestScoped managebean。我想更新Customers实体内部的Java List。提交时,只有Java List(List<'Phone>)中的信息消失了。我不想使用SessionScoped。任何人都可以分享一些提示或解决这个问题吗?

我的代码显示在这里。

客户实体:

{ ..
    @Column(name = "FIRSTNAME")
    private String firstname;
    @Column(name = "LASTNAME")
    private String lastname;


    ...

    @ElementCollection
    @CollectionTable(name = "CUSTOMERS_Phone",
            joinColumns = @JoinColumn(name = "CUSTOMERS_ID"))
    @AttributeOverride(name = "teleNumbers",
            column = @Column(name = "PHONE_NUMBER",length=30))
    private List<Phone> phone;
..}

JSF Managebean:

    @Named(value = "editCustomersBeanService")
    @RequestScoped
    public class EditCustomersBeanService implements Serializable {

        /**
         * Creates a new instance of EditCustomersBeanService
         */
        @PostConstruct
        public void init() {
            ctx = FacesContext.getCurrentInstance();
            customers =new Customers();
            phones = new ArrayList<>();
            customers.setPhone(phones);

        }

        public EditCustomersBeanService() {


        }


        @Inject
        private BusinessSessionCustomers businessSSCustomers;
        private int customerId;
        private List<Phone> phones;
        private Customers customers;

        //setter, getter ...

        //update to DB
        public String updatedCustomers() {
        System.out.println("customer Name: " + customers.getFirstname());
        System.out.println("customer LastName: " + customers.getLastname());
        System.out.print("List of Phones in updatedCustomers: ");
        for (Phone ph : customers.getPhone()) {
        System.out.print(ph.getPhoneType() + ", " + ph.getTeleNumbers());
        }

         businessSSCustomers.mergeToDB(customers);
         return "customers";
        }
    ..

}

CustomersEdit.xhtml:

<h:form>

    ...
    <label  for="Last Name">Last Name</label>
       <h:inputText  id="lastName"  p:placeholder="Last Name"
    value="#{editCustomersBeanService.customers.lastname}"/>

    <ui:repeat var="phone" value="#{editCustomersBeanService.customers.phone}"   varStatus="status">
    <label  for="phones">Phone: [#{status.index}]</label>

        <h:selectOneMenu     value="#{phone.phoneType}">
                       <f:selectItems  value="#{editCustomersBeanService.phoneTypeList}"
                             itemLabel="#{editCustomersBeanService.phoneType}" 
                             itemValue="#{editCustomersBeanService.phoneType}"/>
            </h:selectOneMenu>
       <h:inputText class=" form-control" value="#{phone.teleNumbers}" />
     </ui:repeat>

    <h:commandLink  value="Save"   action="#{editCustomersBeanService.updatedCustomers()}"  />

    …
    </h:form>

Phone.java:

@Embeddable
public class Phone {

    public enum PhoneType {

        Home, Mobile, Work
    }
    @Enumerated(EnumType.STRING)
    @Column(name = "PHONE_TYPE", length = 10)
    private PhoneType phoneType;
    @Column(name = "PHONE_NUM", length = 30)
    private String teleNumbers;
//setter, getter 
..
}

0 个答案:

没有答案