I have a DTO that has a list. I want to add new rows to datatable when user clicks add button. But when I click add , the dto ie constructor is called and the value is initialized, and the list size is 0. bean is the conversation area. Do I have to start and end a conversation for the same view when using the bean conversation area? I use the same bean for editing and it works well. How to solve the initialization problem when using richfaces 4 and jsf 2 and ajax.
View:
<rich:panel id ="dataPnl"> <rich:dataTable value="#{legendbean.legendDTO.list}" var="legend" style="width:100%"> <rich:column> <f:facet name="header"> <h:outputText value="SN"/> </f:facet> <h:inputText value="#{legend.sn}"/> </rich:column> <rich:column> <f:facet name="header"> <h:outputText value="Description"/> </f:facet> <h:inputText value="#{legend.desc}"/> </rich:column> <rich:column> <a4j:commandLink value="Add" actionListener="#{legendbean.addLegendRange()}" render="nisForm:dataPnl"/> <h:outputText value=" / "/> <a4j:commandLink value="Remove" actionListener="#{legendbean.removeLegendRange(legend)}" render="nisForm:dataPnl"/> </rich:column> </rich:dataTable> </rich:panel>
Bean:
@Named("legendbean") @ConversationScoped public class LegendController implements Serializable { LegendDTO legendDTO = new LegendDTO(); String selectedLegend; boolean edit; @Inject private Conversation conversation; public boolean isEdit() { return edit; } public void setEdit(boolean edit) { this.edit = edit; } public LegendController() { Logger.getLogger(LegendController.class.getName()).warning("The value of Edit is : " + edit); if (!edit) { legendDTO.getList().add(new Legend()); Logger.getLogger(LegendController.class.getName()).warning("The size of list" + legendDTO.getList().size()); } } public LegendDTO getLegendDTO() { return legendDTO; } public void setLegendDTO(LegendDTO legendDTO) { this.legendDTO = legendDTO; } public void addLegendRange() { Logger.getLogger(LegendController.class.getName()).warning("List Size " + legendDTO.getList().size()); legendDTO.getList().add(new Legend()); Logger.getLogger(LegendController.class.getName()).warning("List Size " + legendDTO.getList().size()); } public void removeLegendRange(Legend legend) { if (legendDTO.getList().size() != 1) { legendDTO.getList().remove(legend); } } public String saveLegend() { Logger.getLogger(LegendController.class.getName()).warning("Save Legend Edit" + edit); LegendDAO dao = new LegendDAO(); if (dao.addLegend(legendDTO, edit)) { if (edit) { conversation.end(); edit = false; Logger.getLogger(LegendController.class.getName()).warning("Save Legend Edit" + edit); return "VIEWLEGEND"; } else { legendDTO = new LegendDTO(); legendDTO.getList().add(new Legend()); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Saved !")); return ""; } } else { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Could Not Save Confim if you have already defined Legend " + legendDTO.getLegendName() + "!")); return ""; } } public List<LegendDTO> getLegends() { LegendDAO dao = new LegendDAO(); return dao.getLegendDTO(); }
source share