问题
I have following code in my .xhtml file
<h:form id="registration3Form">
<h:panelGroup id="terms_outer_panel">
<h:commandLink styleClass="item-7"
action="#{registerBusinessWizardController.addCourse}"
value="#{msgBundle['registerThirdPage.panel.addSchedule']}">
</h:commandLink>
<ui:repeat value="#{registerBusinessWizardController.courses}" var="course">
<h:panelGroup id="terms_inner_panel">
<div class="term">
<h:inputText id="price" styleClass="item-2" value="#{course.price}" />
<h:inputText id="dateFrom" styleClass="date" value="#{course.date}" >
<f:convertDateTime pattern="yy-MM-dd" type="date" dateStyle="long"/>
</h:inputText>
<h:inputTextarea value="#{course.additionalInfo}"/>
<p class="more">
<h:commandLink
value="#{msgBundle['registerThirdPage.panel.deleteSchedule']}"
actionListener="#{registerBusinessWizardController.removeCourse}">
<f:ajax render=":registration3Form:terms_outer_panel" execute="terms_inner_panel"/>
</h:commandLink>
</p>
</div>
</h:panelGroup>
</ui:repeat>
//some other stuff
</h:panelGroup>
</h:form>
And when page is rendered. And user clicks addSchedule link I expect that ui repeat will add new div class="term" with empty values bind to inputs, because:
public void addCourse(){
Course course = new Course();
courses.add(course);
}
I am adding new Course object with null price additionalInfo and date to the list used by ui:repeat.
Instead I get new
<div class="term">
part with values copied from previous entry.
When I remove element from list (In managedBean proper object from list is removed) I see data from object which was just removed :/
Did someone encounter this kind of problem? Do you have any advice?
回答1:
This seems to be another <ui:repeat>
bug. Mojarra's <ui:repeat>
has (had) too many problems. First step is to exclude the component from being suspect by replacing it by a fullworthy UIData
component, such as <h:dataTable>
. If it works, then it's most definitely a bug in <ui:repeat>
, otherwise it's just a bug in your own code (based on the code given as far I don't see any, I however assume that your bean is @ViewScoped
).
If it's indeed caused by <ui:repeat>
itself, then the first step is usually to upgrade to latest Mojarra version. This is currently 2.0.6 for Servlet 2.5 containers and 2.1.4 for Servlet 3.0 containers. If that doesn't solve the problem, then you should look at replacing Mojarra by MyFaces which has a different and better <ui:repeat>
implementation. If that isn't an option for some reason, then you should consider looking at a 3rd party component library which has an UIData
based component which doesn't render any markup, such as Tomahawk's <t:dataList>
.
来源:https://stackoverflow.com/questions/8464983/uirepeat-adding-removing-elements-from-list-does-not-work-properly