Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

jsf - Iterating through the datatable from the bean

I have the following dataTable:

   <h:form> 
<h:dataTable id = "notTable" value="#{editCategoryBean.allNotifications}" var="notification">
     <h:column>                 
        <f:facet name="header">Key</f:facet>                    
        <h:inputText id = "notkey" value="#{notification.key}" />
     </h:column>
     <h:column>
        <f:facet name="header">Lang</f:facet>
        <h:inputText id = "notlanguage" value="#{notification.language}"/>
     </h:column>
     <h:column>
        <f:facet name="header">Value</f:facet>
        <h:inputText id = "notvalue" value="#{notification.value}"/>      
     </h:column>
   </h:dataTable>
<h:commandButton action ="#{editCategoryBean.save()}"  value = "Save" >    </h:commandButton>

I want to edit my notifications from the allNotifications List in the datatable and save all changes with one button click. How can I iterate through the datatable from the editCategoryBean? Or how can I implement this behaviour otherwise?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

JSF has already updated the model behind value="#{editCategoryBean.allNotifications}" with the submitted values the usual way. So, all you basically need to do is to pass it through to the service layer for save:

public void save() {
    yourNotificationService.save(allNotifications);
}

Otherwise, if you really insist in iterating over it yourself for some reason, maybe to print the submitted values for testing purposes, then just do it the usual Java way:

public void save() {
    for (Notification notification : allNotifications) {
        System.out.println(notification.getKey());
        System.out.println(notification.getLanguage());
        System.out.println(notification.getValue());
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...