I'm working with primefaces 4.0 and JSF 2.2 and I'm currently trying to update a form with a p:tree on it. The commandButton works correctly but it does not update the form or call the init() method until I manually refresh the page. I don't know what I'm doing wrong since the same code does work for a DataTable element.
Here's the code:
<h:form id="preferenciasForm">
<div id="panelTree">
<p:panel id="defTree" style="margin-bottom: 20px">
<p:tree value="#{dtPreferencesBuilder.root}" var="node"
selectionMode="checkbox"
selection="#{dtPreferencesBuilder.selectedNodes}"
style="width:100%; height:100%;" animate="true">
<p:treeNode>
<h:outputText value="#{node.label}" />
</p:treeNode>
</p:tree>
<p:commandButton value="Add preferences"
icon="ui-icon-pencil"
actionListener="#{dtPreferencesBuilder.insertPrefNodes()}"
update=":preferenciasForm" ajax="true" />
</p:panel>
</div>
</h:form>
And here's is the java class.
#ManagedBean(name="dtPreferencesBuilder")
#ViewScoped //I've tried with or without the ViewScoped, neither work
public class PreferencesBuilderBean {
private TreeNode root;
private TreeNode prefRoot;
private TreeNode[] selectedNodes;
#PostConstruct
public void init() {
System.out.println("Building Tree");
selectedNodes=null;
root=null;
prefRoot=null;
root=getStandardTree();
prefRoot=getPreferedTree();
}
The init() is not called as the print is only show on manual reload so the tree is not updated nor the selectedNodes refreshed. Any ideas why it doesn't work?
As I cannot describe bean scopes better than excellent answers already given for similar questions I'll just refer you to the answers by BalusC and Kishor P here.
The init-method (or any method with the #PostConstruct-annotation) will be called by the framework only when the bean is created, after injections and therefore after the constructor has run as rion18 said. It would not be normal to use the method for anything else than initializing work. So create other methods, and call those from actions and actionListeners.
If you want the bean to be the same when you call it with ajax (as you do) it needs to be at least ViewScoped. If you really do want to call the init() every time it should be RequestScoped, but then the bean will be new when you call it with ajax and not remember a thing.
Related
well I'm learning jsf/hibernate and I'm having trouble understanding how to correctly call methods to my ManagedBean
its a simple CRUD so I need to pass vía the jsf form a Ciclista object, but since the inputs are Ciclista attributes how do I pass the Object ? (how to create instance of it) here's the code:
#Override
public String create(Ciclista c) {
Session s = sFac.openSession();
s.beginTransaction();
s.save(c);
s.getTransaction().commit();
s.close();
return "Administrador?faces-redirect=true";
}
and jsf form:
<h:form class="form">
<div id="input-wrapper">
<h:inputText class="inputs" value="#{ciclistaBeanDB.nombre}" />
<h:inputText class="inputs" value="#{ciclistaBeanDB.nacionalidad}" />
<h:inputText class="inputs" value="#{ciclistaBeanDB.equipo}" />
</div>
<h:commandButton class="btn" action="#{ciclistaBeanDB.create()}" value="Create" />
</h:form>
I get error: method not found because i don't pass the Ciclista c object in crear(//here)
since i don't have the object per se, just its attributes how do I proceed ??
You are getting the MethodNotFoundException, because you wrote crear instead of Create.
But the right way to do it, would be to call a save() method without arguments, which is responsible to assemble your object you want to persist.
Since you are already binding your input fields to bean properties, all you would need to do is, use a method like this:
public String save(){
Ciclista c = new Ciclista();
c.setNombre(this.nombre);
c.setNacionalidad(this.nacionalidad);
c.setEquipo(this.equipo);
create(c);
return "Administrador?faces-redirect=true";
}
your commandbutton now should target the save() method of your bean. The input values in the form will be inserted into your baking bean properties and the save() method would assemble the instance and finally calls your persist-method.
<h:commandButton class="btn" action="#{ciclistaBeanDB.save}" value="Create" />
Usually you should not call the DatabaseBean directly, but kind of a Controller - or how ever one would like to call it. This Bean then should utilize your database-service which should only be responsible for CRUD-Functionality.
At least for big applications it would be a mess having the databasemethods and other (view-related) methods mixed in one class.
There a several related question on this topic on SO and elsewhere, but I couldn't find a definitive answer on this specific question.
I have a p:dataTable and I want the possibility to click on a row and open a detail page (a new page, not a dialogue or window).
I have solved it this way (which I have from the primefaces website, for some reason it is no longer there: http://web.archive.org/web/20101001223235/http://www.primefaces.org/showcase/ui/datatableRowSelectionInstant.jsf):
<p:dataTable var="order" value="#{orderBean.orders}" selection="#{orderBean.selectedOrder}" selectionMode="single" rowKey="#{order.number}">
<p:ajax event="rowSelect" listener="#{orderBean.orderSelect}"/>
<p:column ... />
</p:dataTable>
The navigation is then executed in the bean:
public void orderSelect(SelectEvent event) {
ConfigurableNavigationHandler nh = (ConfigurableNavigationHandler)FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
nh.performNavigation("orderDetail?faces-redirect=true");
}
My Question: is there a way of doing this just inside JSF without the help of a backing bean?
I am also asking because they removed the code exmaple from the primefaces site, which might be an indication that this is not the right way of doing something like that.
Wrap the cell(s) of interest in a simple <h:link>.
<p:column>
<h:link outcome="orderDetail">
...
</h:link>
</p:column>
Use if necessary CSS display:block on the link to let it span the entire cell. You can if necessary pass request parameters using a nested <f:param>.
since it is an ajax request, typically the request/response is used to re-render some components in the web page. What you could do is
<p:ajax event="someventofintrest" onsuccess="javascript:myjsmethod();"></p:ajax>
and
<p:remotecommand name="myjsmethod" action="#{mybean.mybeanmethod}" />
and in the backing bean
public String mybeanmethod(){
return "mynewpage"; // Navigate away to mynewpage.xhtml
}
HTH.
As I didn't find a really perfect solution, this is how I do it now.
I have now a "navigator" class like this
#Component
public class Navigator {
public void nav(String page) {
UIHelper.navigateTo(page);
}
}
And I call this class from my ajax event:
<p:ajax event="rowSelect" listener="#{navigator.nav('orderDetail')}"/>
As I said, not really perfect, but I like the fact that I don't have to write code in my backing bean. (Of course I have to write code for the Navigator, but that I can re-use.)
I've got a problem considering JSF and AJAX.
I am trying to update some customer details after a visitor inserts the customer id.
Firstly, a excerpt from the xhmtl code:
<h:form>
<h:panelGrid columns="2">
<h:panelGrid columns ="2" border="1" id="customer_grid">
<h:outputLabel value="#{mbean_msg.reservation_lblCustomerNo}" for = "customer_id"/>
<h:inputText id = "customer_id" value="#{reservationHandler.customer.customer_id}">
<f:ajax listener="{reservationHandler.autocompleteCustomerDetails}"
render="customer_grid" />
</h:inputText>
<h:outputLabel value="#{mbean_msg.reservation_lblLastname}" for="lastname"/>
<h:inputText id="lastname" value="#{reservationHandler.customer.lastname}" required ="true"
requiredMessage="#{error_msg.errmsgLastname}" validator="#{reservationHandler.validateCustomer}"/>
<h:outputLabel value="#{mbean_msg.reservation_lblFirstname}" for="firstname"/>
<h:inputText id="firstname" value="#{reservationHandler.customer.firstname}" required ="true"
requiredMessage="#{error_msg.errmsgFirstname}" validator="#{reservationHandler.validateCustomer}"/>
The listener method is implemented within my java file (ReservationHandler.java) like that:
public void autocompleteCustomerDetails(){
System.out.println("Auto Complete"); // for testing
}
Basically I am trying to call the method autocompleteCustomerDetails with the Listener. Unfortunately this method is never called. Anyways, the render seems to work just fine, since the other inputTexts update themselves (visibly).
Does anybody have an idea, why the listener isn't called?
There are two problems in the code shown so far:
First,
<f:ajax listener="{reservationHandler.autocompleteCustomerDetails}" />
this isn't a valid EL expression. EL expessions have the form of #{}, not {}. Fix it accordingly:
<f:ajax listener="#{reservationHandler.autocompleteCustomerDetails}" />
Second,
public void autocompleteCustomerDetails() {
this isn't a valid default signature of a method expression for <f:ajax listener>. The tag documentation clearly tells the following:
signature must match public void processAjaxBehavior(javax.faces.event.AjaxBehaviorEvent event) throws javax.faces.event.AbortProcessingException.
So, you forgot the argument. Add it accordingly. The throws declaration isn't mandatory for unchecked exceptions, so we can just leave it out.
public void autocompleteCustomerDetails(AjaxBehaviorEvent event) {
Or, if you actually intend to get rid of the argument, then you should put parentheses in the EL method expression:
<f:ajax listener="#{reservationHandler.autocompleteCustomerDetails()}" />
Note that this works only if your container supports EL 2.2.
Try this:
<f:ajax listener="{reservationHandler.autocompleteCustomerDetails()}"
render="customer_grid" />
or change the method to:
getAutoCompleteCustomerDetails
just for testing...
Anyhow, you´re not allowed to use any jsf's components library? Like primefaces ?
It gets the job done in such a easy way...
Anyhow, with primefaces I would do that like this:
<p:ajax event="blur" listener="#{prospectoRadarController.atualizarRadar(data)}" update=":mainForm:painelRadar" />
Another guess would be, add the execute="#this" to the ajax flag...
Sorry, those are all wild guesses, but I really want to help.
PLease feedback!
I have a page where I want to include a part of the page (footer in this instance) dependant on values given from a view parameter.
I have my ViewScoped backing bean initializing on preRenderView
<f:metadata>
<f:viewParam name="racecode" value="#{displayResults.racecode}" />
<f:event type="preRenderView" listener="#{displayResults.init}" />
</f:metadata>
This queries the database to get the name of the footer to be included. This then, is used in this fashion :
<h:panelGroup id="customFooter" display="block">
<ui:include src="#{displayResults.customFooter}" />
</h:panelGroup>
This always gives me a missing page. But if I enter the page name manually it works. Same if I replace the ui:include with an h:outputText.
I understand that it has something to do with the phases of JSF and that at the time the ui:include is done, the value is not set yet. (reading up and better understanding the phases is something on my TODO list).
The question remains. How can I get something of the sort done. Have a bean use the viewParam, query the database and use that value in a ui:include?
#wemu has already explained the cause. The <ui:include src> is evaluated before init() method is called. His proposed <f:phaseListener> solution is however clumsy.
Just use #ManagedProperty/#PostConstruct on a #RequestScoped bean.
#ManagedProperty("#{param.racecode}")
private String racecode;
#PostConstruct
public void init() {
// ...
}
PreRenderView listeners are called within the RenderResponsePhase, before components are rendered BUT AFTER the TagHandlers are called. This means that TagHandlers will NOT see data initialized within a PreRenderView event.
If you are using a <ui:include value="#{myBean.myViewId}" /> to dynamically switch an include you can't use a PreRenderView event listener to set the myViewId property of myBean.
If you need to do that use a <f:phaseListener>.
This question already has an answer here:
How to disable/enable JSF input field in JavaScript?
(1 answer)
Closed 3 years ago.
I'm developing a Java EE application (JSF2 + richfaces 3.3.3 + facelets).
I want to disable my h:selectOneMenu when loading my page, and when it finishes loading (using the function onload()), I want to re-enable my component.
I have something like this:
<ui:define name="infosHead">
<script type="text/javascript">
window.onload = function() {
document.getElementById("forme1_myform:valueCh").disabled = false;
alert("here");
}
</script>
</ui:define>
<ui:define name="infosBody">
<h:form id="forme1_myform" target="_blank">
<h:selectOneMenu id="valueCh" value="#{mybean.value}" disabled="true" >
<f:selectItems value="#{mybean.values}" />
<a4j:support event="onchange"
ajaxSingle="true"
limitToList="true"
reRender="id1,id2,...."
ignoreDupResponses="true"
action="#{mybean.actionme}"
oncomplete="getId();"/>
</h:selectOneMenu>
</h:form>
</ui:define>
this is working fine.
But my bean is getting nothing (mybean.value == null).
It's like he thinks that the component is still disabled.
how can I make this works ?
The problem is that you are enabling your component only on the client side. On the server side it will always be disabled="true". To make this work you must :
a. Assign the disabled property of your component to a managed bean property that will be initially 'true'
disabled="#{myController.valueChDisableStatus}"
b. Inside your h:form insert window.onload = callScript
c. Finally, in the myController#someAction method set the valueChDisableStatus property to false
I cant check the solution right now, but I believe it will work fine.
I found this solution.
<ui:define name="infosHead">
<script type="text/javascript">
window.onload = function() {
updateName(false); // sets 'disabled' from true to false
}
</script>
</ui:define>
<ui:define name="infosBody">
<h:form id="forme1_myform" target="_blank">
<h:selectOneMenu id="valueCh" value="#{mybean.value}" disabled="#{mybean.render}" >
<f:selectItems value="#{mybean.values}" />
<a4j:support event="onchange"
ajaxSingle="true"
limitToList="true"
reRender="id1,id2,...."
ignoreDupResponses="true"
action="#{mybean.actionme}"
oncomplete="getId();"/>
</h:selectOneMenu>
</h:form>
<a4j:form>
<!-- this is where it's going to reRender my component -->
<a4j:jsFunction name="updateName" reRender="valueCh">
<a4j:actionparam name="param1" assignTo="#{mybean.render}" />
</a4j:jsFunction>
</a4j:form>
</ui:define>
and this is the content of mybean:
public class MyBean implements Serializable {
private List<SelectItem> values;
private String value;
private Boolean render = true; // the page loads with element disabled
public void actionme(){...}
//getters & setters
}
Neither of these solutions worked for me.
While setting the element's disabled attribute from a managed bean value worked fine, and changing it's value via javascript also worked fine, once the form is submitted the "someaction" method is still not reached in order to "set the valueChDisableStatus property to false". In addition I found that even though my managed bean had an isDisabled() and a setDisabled(boolean value), the setDisabled() method was never called after submitting the form (of course), though the isDisabled() was. So I was unable to find a way using these solutions to change the managed beans "disabledElement" value to false.
I did find a solution that did work for me though at this link:
http://blog.michaelscepaniak.com/jsf-you-cant-submit-a-disabled-form-element
Basically it suggests that by default all the elements should be "enabled" from the JSF perspective, and that all enabling and disabling should be done via Javascript. It does point out the the client and server states are out of sync temporarily in this solution... but it works very well for my scenario regardless of the temporary mismatch.