0

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
gogogaga
  • 175
  • 1
  • 3
  • 11
  • According to the JSF 2.2 documentation, the action method "signature must match public java.lang.Object action()" which your doesn't. Try to fix this first. – Ondřej Xicht Světlík Jan 10 '18 at 17:02
  • Possible duplicate of [When using templating, where should I declare the ?](https://stackoverflow.com/questions/9856847/when-using-uicomposition-templating-where-should-i-declare-the-fmetadata) – Kukeltje Jan 10 '18 at 19:43
  • @Kukeltje I don't think so. Please refer to my edit. – gogogaga Jan 11 '18 at 08:46
  • I know it was not your explicit question (50% of the questions in SO are sort of 'wrongly formulated btw). But if the placement is **'wrong'**, it might not work as expected. That is at least the understanding I (also) get from the duplicate.'Correcting' it would still be better... – Kukeltje Jan 11 '18 at 09:58
  • @Kukeltje Ok. i added it as a best practice anyway. – gogogaga Jan 11 '18 at 12:10

1 Answers1

0

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 (e.g. login/logout), you should indeed be sending a redirect."

gogogaga
  • 175
  • 1
  • 3
  • 11
  • Now I read the answer I understood the problem... All is explained here: https://stackoverflow.com/questions/36668268/returning-to-same-view-in-backing-bean-should-i-return-null-or-empty-string-or. So effectively you should return an empty string or make the return param void – Kukeltje Jan 11 '18 at 21:33