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