2

I have a form in my jsf page(a popup) that I used to upload a file. I need to refresh the parent page when the file is uploaded. What is the best approach that I can use to achive this task.

My JSF page is simple and looks like this

<h:form id="uploadForm" enctype="multipart/form-data">
    <ul class="page">
        <li>
            <h:messages layout="list" for="uploadForm" styleClass="generic-message"/>
        </li>
        <li>
            <h:outputLabel for="file" value="select a file" />
            <t:inputFileUpload id="file" value="#{aBean.file}" required="true" requiredMessage="File need to be selected"/>
            <h:message for="file" styleClass="msg" />
        </li>
        <li>
            <h:outputLabel value="Name" />
            <h:inputText id="name" value="#{aBean.name}" label="#{aBean.name}" required="true" requiredMessage="Name is required"/>
            <h:message for="name" styleClass="msg" />
        </li>
        <li>
            <h:commandButton value="Upload" action="#{aBean.submit}" />
        </li>
    </ul>
</h:form> 

any suggestion is highly appreciated

1 Answer 1

2

In the popup, you need to conditionally render a

<script>window.opener.location.reload(true);</script>

so that the parent window will be refreshed.

You can do this by triggering some boolean in the action method

private boolean reloadParent;

public void submit() {
    // ...
    reloadParent = true;
}

public boolean isReloadParent() {
    return reloadParent;
}

and wrapping the script in an <ui:fragment> with a rendered attribute

<ui:fragment rendered="#{aBean.reloadParent}">
    <script>window.opener.location.reload(true);</script>
</ui:fragment>
Sign up to request clarification or add additional context in comments.

6 Comments

thanx balusc for the prompt reply.. I added my JSF code <code> <ui:fragment rendered="#{aBean.print}"> <script type="text/javascript" > window.print(); </script> </ui:fragment> </code> I put this code block just under <code> <ui:composition template="/templates/aLayout.xhtml"> </code> but the javascript function doesn't get triggered is there any thing wrong where I have placed my <ui:fragment>
It needs to be placed in the popup page.
I have <ui:define name="sub-heading"> areas in my popup page and I have put the UI fragment section outside all these UI defined araes..so the javascript is not getting triggered. really sorry if I sounds confusing..just little new to JSF..
Put it inside the <ui:define>.
I have added the VIewScope for my bean and I have a postConstruct method in my bean as well. WHen I click submit, I see my boolean value "reloadParent" getting the value "true" but thereafter it goes again to the post construct method, so my boolean value remains to be false.. please advise me on this issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.