PrimeFaces commandButton doesn't navigate - jsf-2

I have this code :
Add.xhtml :
<h:form id="nouv">
<h:panelGrid columns="2">
<h:outputText value="Nom:"></h:outputText>
<p:inputText value="#{ddeBean.nom}"></p:inputText>
<p:commandButton id="save"
value="ajouter"
update=":nouv:btn"
actionListener="#{ddeBean.ajouter}">
</p:commandButton>
<p:outputPanel id="btn">
<h:outputText rendered="#{ddeBean.created}" value="#{ddeBean.message}"/>
<p:commandButton id="btn_cr" value="add" rendered="#{ddeBean.created}"
action="pool.xhtml?faces-redirect=true">
</p:commandButton>
</p:outputPanel>
</h:panelGrid>
</h:form>
ddeBean.java :
#ManagedBean(name = "ddeBean")
#RequestScoped
public class DemandeBean implements Serializable{
ddeDAO dao = new ddeDaoImpl();
private String nom;
public String message = "";
private boolean created = false;
public void test(ActionEvent event){
Demande p = new Demande();
p.setDde(this.nom);
dao.Nouveau_dde(p);
created = true;
this.setMessage("saved!");
}
}
When I click commandButton Ajouter the message an the commandbutton Add will be shown, but commandbutton Add doesn't redirect to pool.xhtml.

When you click on the button Add, the bean is recreated and the value #{ddeBean.created} then reinitialized to false so the button doesn't get rendered before action take place.
To fix that, you need to change the scope of your bean to #ViewScoped.
You should also make sure to replace
public void test(ActionEvent event){
by
public void ajouter(ActionEvent event){
if you want that your button work properly.
Another solution
Since you are using PrimeFaces, you could show your button by JavaScript :
<p:commandButton id="save"
value="ajouter"
oncomplete="panel-create.show();"
actionListener="#{ddeBean.ajouter}">
</p:commandButton>
<p:outputPanel id="btn" style="display: none;" widgetVar="panel-create">
<h:outputText value="#{ddeBean.message}"/>
<p:commandButton id="btn_cr" value="add"
action="pool.xhtml?faces-redirect=true">
</p:commandButton>
</p:outputPanel>
Note the oncomplete attribute on the p:commandButton, the widgetVar attribute on the p:panel and the rendered attributes removed.

Related

Hide <p:messages> by clicking on embedded Submit button

I need to close my messages dialog without using the built in close button. An embedded "CLOSE" button within the message needs to fire of a method in the backing bean, and when finished, the message box should close.
My code calls the actionListener, but I am unable to clear the message box on submission. In my last attempt, I tried to do it from the Backing Bean, but the box refuses to go away.
Below is sample test code.
<h:form id="form1">
<h:panelGroup id="myPanel" layout="block" class="scap-block"
rendered="#{not chartView.acknowledged}">
<p:messages id="banner" showSummary="true" showDetail="false"
autoUpdate="true" closable="true" escape="false">
</p:messages>
<p:commandButton widgetVar="acceptContinue"
style-class="align right" value="Close" type="ajax"
actionListener="#{chartView.close}" render="myPanel" />
</h:panelGroup>
</h:form>
<f:event type="preRenderView" listener="#{chartView.init}" />
</h:body>
Bean:
#ManagedBean(name = "chartView")
#ViewScoped
public class ChartView implements Serializable {
private boolean acknowledged = false;
public void init() {
log.debug("preRender init() method");
String detail = "PrimeFaces Rocks."
+"<button aria-disabled='false' id='form1:j_idt5' name='form1:j_idt5' "
+ "class='ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' "
+ "onclick='PrimeFaces.ab({s:"form1:j_idt5"});return false;' type='ajax'>"
+ "<span class='ui-button-text ui-c'>Close</span></button>";
FacesContext.getCurrentInstance().addMessage("banner",
new FacesMessage(FacesMessage.SEVERITY_ERROR, detail, "Some details"));
}
public void close() {
System.err.println("Acknowledged");
log.debug("###Close called###");
setAcknowledged(true);
RequestContext context = RequestContext.getCurrentInstance();
context.update(":form1:myParent");
}

primefaces 5 backing bean values not updated from the UI controls

I have a simple input form that accepts data from the user and stores the data in DB upon a click action for a p:commandButton
the form looks like this
form.xhtml :
<div align="center" style="width: 80%">
<p:panelGrid columns="1">
<p:accordionPanel>
<p:tab title="#{msg['truck.tab.general']}">
<h:form><h:panelGrid columns="3">
<p:outputLabel value="*#{msg['truck.form.code']}"></p:outputLabel>
<p:inputText id="inputTruckCode" required="true"
requiredMessage="Must exist"
value="#{trucksBean.currentTruck.code}"></p:inputText>
<p:message for="inputTruckCode" showSummary="true"></p:message>
<!-- rest of the form -->
</h:form>
</p:tab>
</p:accordionPanel>
<p:commandButton action="#{trucksBean.insertTruck}"
value="Insert Truck"></p:commandButton>
</p:panelGrid>
</div>
the backing bean code:
#ManagedBean
#ViewScoped
public class TrucksBean {
private TruckFullInformation currentTruck;
public String insertTruck() {
//the values inside currentTruck member variable are NULLs
return null;
}
what's wrong with the code? why clicking on the commandButton doesn't affect/update the values of the "currentTruck" variable?
thanks in advance;

Enable/Disable <p:commandButton> in footer of <p:dataTable> based on change of <p:column selection="multiple">

I have a datatable where I am showing List of Employees. At the end of the datatable I have a commandbutton. Now if user selects any checkbox inside the datatable, I want my commandbutton to be enabled. Initially the command button is disabled.
Below is my index.xhtml page
<p:dataTable value="#{userPreferencesBean.studentDataModel}"
var="studentDetails" emptyMessage="No Student found."
selection="#{userPreferencesBean.selectedStudents}">
<f:facet name="header">
List of Students
</f:facet>
<p:ajax event="rowSelect" update="submit" listener="#{userPreferencesBean.onRowSelect}"/>
<p:ajax event="rowUnselect" update="submit" listener="#{userPreferencesBean.onRowSelect}"/>
<p:column selectionMode="multiple" style="width:2%" />
//All the columns
<f:facet name="footer">
<p:commandButton id="submit" value="Save Preferences"
icon="ui-icon-disk" style="float:right;"
action="#{userPreferencesBean.savePreferredInterfaces}"
update=":#{p:component('selectedInterfaceDetails')}"
disabled="#{not userPreferencesBean.hasPreferenceChanged eq false}"/>
</f:facet>
</p:dataTable>
Below is my backing bean:
#ManagedBean
#SessionScoped
public class UserPreferencesBean implements Serializable {
private boolean hasPreferenceChanged = false;
public void onRowSelect(SelectEvent event) {
this.setHasPreferenceChanged(true);
}
public boolean isHasPreferenceChanged() {
return hasPreferenceChanged;
}
public void setHasPreferenceChanged(boolean hasPreferenceChanged) {
this.hasPreferenceChanged = hasPreferenceChanged;
}
What I am expecting is that once I select a row, my submit button should be enabled. But it is not happening. Could you please help me to understand where I am doing wrong? Thanks.
With regards,
Sudipta Deb
Try put your table into form.
Also you can make it more convient:
<p:commandButton id="submit" ... disabled="#{userPreferencesBean.selectedStudents==null}"/>

Can not use hidden commandButton with #RequestScoped backing-bean

I have following sample code.
Initially, only commandButton Two is visible. When I click this button, commandButton One is also visible. But when I click One, the backing-bean method click1 does not get fired.
Following is my code:
xhtml
<h:form id="form1">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}" />
</h:form>
<h:form id="form2">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button2" value="Two" action="#{bean.click2}" />
</h:form>
backing-bean
#RequestScoped
#Named("bean")
public class JsfTrial implements Serializable {
private static final long serialVersionUID = 2784462583813130092L;
private boolean show1; // + getter, setter
public String click1() {
System.out.println("Click1()");
return null;
}
public String click2() {
System.out.println("Click2()");
setShow1(true);
return null;
}
}
I found a very informative answer by BalusC.
h:commandLink / h:commandButton is not being invoked
If I understand it correctly, my problem is due to point 5 of this answer.
Does that also mean we can not use hidden commandButton with #RequestScoped backing-bean?
You can use the request scope, you should only pass the condition as a request parameter to the subsequent requests by <f:param> instead of by a JSF hidden input field <h:inputHidden>. The value of the hidden input field is only set in the model during Update Model Values phase, while the condition of the rendered attribute is already evaluated during Apply Request Values phase which is earlier.
So, use <f:param> instead of <h:inputHidden>:
<h:form id="form1">
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}">
<f:param name="show1" value="#{bean.show1}" />
</h:commandButton>
</h:form>
<h:form id="form2">
<h:commandButton id="button2" value="Two" action="#{bean.click2}">
<f:param name="show1" value="#{bean.show1}" />
</h:commandButton>
</h:form>
This way you can extract them as request parameter in bean's (post)constructor.
public JsfTrial() {
String show1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("show1");
this.show1 = (show1 != null) && Boolean.valueOf(show1);
}
Ugly, but CDI doesn't offer a builtin annotation which substituties JSF's #ManagedProperty("#{param.show1}"). You could however homegrow such an annotation.

add user entered data to database using jsf

As I'm new to JSF framework, wanted to know how to add the user entered data to the database.
More clearly, i'm using jsf with jsp, my jsp contains some input fields and a submit button.How do i capture all the user entered data and send it as input paramters for the button, as my back end takes all these as input parameters and updates the table with a new record.
Please look into my code and lety me know my mistake
Registration.jsp
<body>
<f:view>
<h:form>
<h:panelGrid columns="2" rules="all" width="100%" style="background:#03547C;color:#FDD017">
<h:column>
<h:outputText value="Stu No : "></h:outputText>
<h:inputText value="#{RegBean.stuNo}"/>
</h:column>
<h:column>
<h:outputText value="Stu Name : "></h:outputText>
<h:inputText value="#{RegBean.stuName}"/>
</h:column>
<h:column>
<h:outputText value="Standard : "></h:outputText>
<h:inputText value="#{RegBean.standard}" />
</h:column>
<h:column>
<h:outputText value="School : "></h:outputText>
<h:inputText value="#{RegBean.school}" />
</h:column>
</h:panelGrid>
<h:panelGrid columns="2" rules="all" width="100%" style="background:#03547C;color:#FDD017">
<h:column>
<h:form>
<h:commandButton id="submitBtn" value="Submit" action="#{RegBean.submitDetails}">
<f:param name="sNo" value="#{RegBean.stuNo}" />
<f:param name="sName" value="#{RegBean.stuName}" />
<f:param name="std" value="#{RegBean.standard}" />
<f:param name="schl" value="#{RegBean.school}" />
</h:commandButton>
</h:form>
</h:column>
</h:panelGrid>
</h:form>
</f:view>
</body>
Registration Bean
public class VendorRegBean {
private String stuNo;
private String stuName;
private String standard;
private String school;
// getters and setters
public void submitDetails() {
Map requestMap = context.getExternalContext().getRequestParameterMap();
String stNo = (String) requestMap.get("sNo");
String stName = (String) requestMap.get("sName");
String stndrd = (String) requestMap.get("std");
String scl = (String) requestMap.get("schl");
vReg.stuRegistration(stNo ,stName ,stndrd ,scl );
}
}
You don't need to get the parameters from the request parameter map as long as you register your bean as managed bean and provide getter and setter methodes for your bean members.
Use annotations for your bean to declare it as managed bean. Getter and setter example are given for stuNo member:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class RegBean {
private String stuNo;
...
private String getStuNo() {
return stuNo;
}
private String setStuNo(String stuNo) {
this.stuNod = stuNo;
}
}
In the view you have to reference the managed bean with first letter lower case, such as:
<h:inputText value="#{regBean.stuNo}"/>
Your command button doesn't need <f:param>, simply use:
<h:commandButton id="submitBtn" value="Submit" action="#{regBean.submitDetails}"/>
Then all parameters are automatically available in your submitDetails method and you don't need to get them from the parameter map.

Resources