0

I have one file with multiple file included on that

<ui:include src="file1.xhtml" />
<ui:include src="file2.xhtml" />
<ui:include src="file3.xhtml" />
<ui:include src="file4.xhtml" />

Once submitting file, I am fetching the Managed bean of all the included file and calling the save method

FacesContext ctx = FacesContext.getCurrentInstance();
File1ManagedBean fmb =(File1ManagedBean)ctx.getApplication().evaluateExpressionGet(ctx, "#{file1ManagedBean}", File1ManagedBean.class);
fmb.saveApplication();

Now in this file I have another button called "Add Another Member" which will repeat those included file again.That thing I am not able to do. I have tried with ui:repeat and but the problem is I am loading same managed bean twice and both are copying the same values. So, how could I achieve the same functionality

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kinkar
  • 9
  • 4

1 Answers1

1

This is indeed an awkward approach. I can understand why you got confused and blocked while maintaining and extending it. Just don't make all those models separate managed beans. Simply make all those models a property of a single managed bean which manages them all in single place.

E.g.

@Named
@ViewScoped
public class FileManagedBean implements Serializable {

    private List<FileModel> fileModels;

    @PostConstruct
    public void init() {
        fileModels = new ArrayList<>();
        addFileModels();
    }

    public void addFileModels() {
        fileModels.add(new File1Model());
        fileModels.add(new File2Model());
        fileModels.add(new File3Model());
        fileModels.add(new File4Model());
    }

    public void saveFileModels() {
        for (FileModel fileModel : fileModels) {
            fileModel.saveApplication();
        }
    }

    public List<FileModel> getFileModels() {
        return fileModels;
    }

}
<c:forEach items="#{fileManagedBean.fileModels}" var="fileModel">
    <ui:include src="#{fileModel.includeSrc}" /><!-- inside include file, just use #{fileModel}. -->
</c:forEach>
<h:commandButton value="Add" action="#{fileManagedBean.addFileModels}" />
<h:commandButton value="Save" action="#{fileManagedBean.saveFileModels}" />

Note that <ui:repeat><ui:include> won't work for reasons explained here: Dynamic <ui:include src> depending on <ui:repeat var> doesn't include anything.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • If we don't have any Managed bean for the inner page then how we are going to submit the values to the outer managed bean. I have tried with `fileManagedBean.fileModels[0].name`, but I am getting null for name. – Kinkar May 28 '15 at 13:54
  • See comment in answer. Use if necessary a tagfile instead of ui:include to make code more clear. – BalusC May 28 '15 at 14:06
  • Thanks sir, that worked. Now I want Save button in every page and clicking on it should save only that page data. how to get the list index while saving. How to achieve the same functionality in the above design – Kinkar May 29 '15 at 09:33
  • `#{fileManagedBean.save(fileModel)}` – BalusC May 29 '15 at 09:40