简体   繁体   中英

Initializing bean every time on <f:viewAction>

I am using jsf 2.2 from mojara 2.2.10 dependency.

My problem is that, in my viewscope bean, the initialize() function of <f:viewAction> tag's action attribute is called only when the page is loaded first time.(firebug inspect tells me that its a post(to the calling page) and then a get to the viewAction page).

I want to call it also after i submit a form which is on this page. From the commandButton that submits the form, i return a string "/myJsfPage.xhtml" without &faces-redirect=true because i want to return to the same page where the form was and not a different one.

The problem is that now {myBean.nameOfUser.size()} is not displayed and so is with other attributes. Also from debugging i know that initialize() is not called this time. The firebug shows only a get request when the submit button is pressed. I tried without onPostback="true" property but got no luck.

EDIT

My question is not about the placement of tag. So it is not a duplicate. Anyhow, adding <ui:insert name="metadata"> in the master template and than <ui:define name="metadata"> in the .xhtml page does not solve my problem.

My jsf page:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core"
                template="../components/defaultLayout.xhtml">

    <f:metadata>
        <f:viewAction action="#{myBean.initialize}" onPostback="true"/>
    </f:metadata>

    <ui:param name="bodyClass" value="container body-nomargin" />

    <ui:define name="body">
        <h:panelGroup layout="block" styleClass="col-md-10">

            <h:form id="uploadform" enctype="multipart/form-data">

                **#{myBean.nameOfUser.size()}**

                <!-- Only this code works after form submission -->

                    <h:panelGroup class="input-group" layout="block">
                        <input type="text"
                               class="form-control" readonly="readonly"
                               onclick="$('#uploadform\\:file').click();"/>

                        <label class="input-group-btn">
                            <h:panelGroup
                                    class="btn btn-default">
                                <h:outputText value="some value"/>
                                <h:inputFile id="file" value="some other value"
                                             validator="#{myBean.checkFile}"
                                             onchange="$('#uploadform\\:uploadBtn').removeClass('disabled');"
                                             style="display: none;"/>
                            </h:panelGroup>
                            <h:commandButton id="uploadBtn" value="Upload"
                                             action="#{myBean.uploadFile}"
                                             styleClass="btn btn-primary disabled"/>
                        </label>
                    </h:panelGroup>

                    <h:outputScript>
                        $(function () {
                            doUpload();
                        });
                    </h:outputScript>
                </h:panelGroup>
           </h:form>

My javascript function from .js file is:

function initializeFileUpload() {

    $(document).on('change', ':file', function() {
        var input = $(this),
            numFiles = input.get(0).files ? input.get(0).files.length : 1,
            label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
        input.trigger('fileselect', [numFiles, label]);
    });


    $(document).ready( function() {
        $(':file').on('fileselect', function(event, numFiles, label) {

            var input = $(this).parents('.input-group').find(':text'),
                log = numFiles > 1 ? numFiles + ' files selected' : label;

            if( input.length ) {
                input.val(log);
            } else {
                if( log ) alert(log);
            }

        });
    });
}

My Java Bean is:

 public void initialize() {        
    setAllFilesOfUser(myDatabaseFunction());                   
 }

 public String uploadFile() {

    //SomeCode

    System.out.println(nameOfUser.size());
        return "/myJsfPage.xhtml";
 }

    //Few more methods and attributes with getter and setters

So i found the solution although i still don't understand how it works.

From my function i have to return

"/myJsfPage.xhtml?faces-redirect=true";

instead of just

"/myJsfPage.xhtml"

When i was checking it with faces-redirect=true , i was adding & in the url instead of ? (very basic mistake - happened because i copy pasted it from somewhere where there was another parameter sent in the url along with redirect).

Now the firebug shows a post and then a get(which was previously missing) to the same page.

From this link i understood that faces-redirect=true should only be added when redirecting to a different page after form submit. That is why i din't add it initially as i wanted to load the same page after form submit. May be some one can clarify this?

"But more than often the result is just presented in the same page, if necesary conditionally rendered/included. Only on successful submits which absolutely needs to go to a different page (eg login/logout), you should indeed be sending a redirect."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM