How to save an array in JSF with ui:repeat + h:inputText + managed bean?

前端 未结 2 947
旧时难觅i
旧时难觅i 2021-02-04 22:56

In a postgres database I have a table with, among others, an int[] field.

In my model I have a persisted bean that maps the table, including the int[] field. In this cla

相关标签:
2条回答
  • 2021-02-04 23:13

    If editableBaseSetList is an int[], then baseNumber is an int. You're now binding the input text component to this int.

    This binding is however not bidirectional. The input element only gets to see the element you are binding to it, not the collection from which it originates. It thus has no knowledge of how to update this collection.

    You thus need to bind to something that can be updated. If your list for instance contained an IntHolder with a getter and setter for the internal integer (say getInt() and setInt()), and the list would be ArrayList you would use:

    <ui:repeat value="#{drawsetController.selected.editableBaseSetList}" var="baseNumber">
        <h:inputText value="#{baseNumber.int}"/>
    </ui:repeat>
    

    After the postback, JSF will call the setInt() method on each IntHolder in the list with the supplied values.

    For a collection that already holds integers or other immutable types, it can be a bit of a hassle to convert it to such collection mentioned above. There is however also another solution. There you don't use the var attribute of ui:repeat but use its index. You then give the h:inputText a binding to the collection indexed by this index var.

    E.g.

    Suppose you have the following bean:

    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    
    @ManagedBean
    @ViewScoped
    public class RepeatBean {
    
        List<Integer> list;
    
        public List<Integer> getList() {
            return list;
        }
    
        @PostConstruct
        public void initList() {
            list = new ArrayList<Integer>();
            list.add(10);
            list.add(20);
            list.add(30);
        }
    
        public String save() {
            // list now contains the values provided by the user.
            return "";
    
        }
    
    }
    

    Used on the following Facelet:

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"        
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        >
    
        <h:body>
    
            <h:form>
                <ui:repeat value="#{repeatBean.list}" varStatus="status">            
                    <h:inputText value="#{repeatBean.list[status.index]}" />
                </ui:repeat>
                <h:commandButton value="Save" action="#{repeatBean.save}" />
            </h:form>
    
        </h:body>
    </html>
    

    This will initially display 10 20 30 on your screen. When you change the numbers and click save, you can verify via e.g. a break-point that the list instance variable contains the updated numbers.

    0 讨论(0)
  • 2021-02-04 23:19

    Don't know if I get you right, but baseNumber is not bound to any property in any managed bean. It only exists in the scope of ui:repeat.

    You should do something like:

    <ui:repeat value="#{editableBaseSetList}" var="myVariable">
        <h:inputText value="#{managedBean.property}" />
    </ui:repeat>
    
    0 讨论(0)
提交回复
热议问题