Why does selectBooleanCheckbox send me to the bottom of the page on click? - primefaces

I am using Primefaces, and in my xhtml file, I have two "selectBooleanCheckbox" locations, and whenever I click them they send me to the bottom of the page. I checked the tags and all I have is an id and a value.
Could there be something on the Java backend that could cause it and if so is there something I could look out for?
Here is the xhtml side of it:
<p:outputLabel for="HasRoleAdmin" value="Admin: " />
<p:selectBooleanCheckbox id="HasRoleAdmin" value="#{UserMgt.newadminrole}"/>
<h:message for="HasRoleAdmin" style="color:red"/>
<p:outputLabel for="HasRoleUser" value="User: " />
<p:selectBooleanCheckbox id="HasRoleUser" value="#{UserMgt.newuserrole}"/>
<h:message for="HasRoleUser" style="color:red"/>
On the java side....
public boolean isNewadminrole() {
return newadminrole;
}
public void setNewadminrole(boolean Newhasroleadmin) {
newadminrole = Newhasroleadmin;
}
public boolean isNewuserrole() {
return newuserrole;
}
public void setNewuserrole(boolean Newuserrole) {
newuserrole = Newuserrole;
}

Related

Re-evaluate required expression after validation failed

I have a form with some required fields depending on a selectOneMenu, like the following:
<p:selectOneMenu id="myList" value="#{myBean.selectedItem}">
<p:selectItems value="#{myBean.myItems}" />
<p:ajax listener="#{myBean.myList_change}"
process="myList field1 field2 field3"
update="field1 field2 field3" />
</p:selectOneMenu>
<p:inputNumber id="field1" required="true" />
<p:inputNumber id="field2" required="#{myBean.selectedItem gt 1}" />
<p:inputNumber id="field3" required="#{myBean.selectedItem gt 2}" />
The first time I push the submit button, without fill the required fields:
<p:commandButton id="mySubmit" action="#{myBean.myAction}" />
I get the validation errors, then if I change the selectOneMenu value the required expression will no longer be evaluated.
For example, if I submit the form with selectedItem equals to 3 I get all the validation errors, then I submit the form with selectedItem equals to 1 and primefaces still requires all the three fields as mandatory.
I tried to add a resetInput to the button:
<p:resetInput target=":myForm" />
Rather than the immediate="true" to the selectOneMenu, without any success.
Any idea, please?
Note: The PrimeFaces version is 6.2
PS: In the image, just the first field is required, but should required all the elements.
Following the complete minimal reproducible example:
The xhtml file (test.xhtml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Example</title>
</h:head>
<h:body bgcolor="white">
<h:form>
<p:messages showDetail="true" />
<p:selectOneMenu id="myList" value="#{myBean.selectedItem}">
<f:selectItem itemLabel="1" itemValue="1" />
<f:selectItem itemLabel="2" itemValue="2" />
<f:selectItem itemLabel="3" itemValue="3" />
<p:ajax listener="#{myBean.myList_change}"
process="myList field1 field2 field3" update="field1 field2 field3" />
</p:selectOneMenu>
<p:inputNumber id="field1" required="true" decimalPlaces="0"
value="#{myBean.field1}" />
<p:inputNumber id="field2" required="#{myBean.selectedItem gt 1}"
decimalPlaces="0" value="#{myBean.field2}" />
<p:inputNumber id="field3" required="#{myBean.selectedItem gt 2}"
decimalPlaces="0" value="#{myBean.field3}" />
<p:commandButton actionListener="#{myBean.submit}" update="#form" />
</h:form>
</h:body>
</html>
The bean (MyBean.java):
package com.mkyong.common;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name = "myBean")
#SessionScoped
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
private static final String STRING_EMPTY = "";
private Long selectedItem;
private String field1, field2, field3;
public Long getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(final Long selectedItem) {
this.selectedItem = selectedItem;
}
public String getField1() {
return field1;
}
public void setField1(final String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(final String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(final String field3) {
this.field3 = field3;
}
public void myList_change() {
final long value = selectedItem.longValue();
if (value < 1) {
setField1(STRING_EMPTY);
}
if (value < 2) {
setField2(STRING_EMPTY);
}
if (value < 3) {
setField3(STRING_EMPTY);
}
}
public void submit() {
}
}
In the web.xml you should set the welcome-page:
<welcome-file-list>
<welcome-file>faces/test.xhtml</welcome-file>
</welcome-file-list>
This question initially was sort of strange, weird since it contained some illogical behaviour that could, like OP stated, not be solved with a p:resetInput or a resetValues="true" attribute on the p:ajax.
Compliments to #Alessandro for being responsive on the comments by initially creating a [mcve] and later on in the chat. Thanks for that, rewarding in comparison to some other people that fail to see the value of a [mcve] and being active in replying.
The first thing I did was change some illogical things in the code that might not be related
The type in the backingbean for selectedItem from a String to an int since that is what it effectively was.
Change the fields from String to int's too since they are with a p:inputNumber
Set the fields to null instead of empty String on a change of the select.
Java code:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name = "myBean")
#SessionScoped
public class MyBean implements Serializable {
private static final long serialVersionUID = 1L;
private int selectedItem;
private Integer field1, field2, field3;
public int getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(final int selectedItem) {
this.selectedItem = selectedItem;
}
public Integer getField1() {
return field1;
}
public void setField1(final Integer field1) {
this.field1 = field1;
}
public Integer getField2() {
return field2;
}
public void setField2(final Integer field2) {
this.field2 = field2;
}
public Integer getField3() {
return field3;
}
public void setField3(final Integer field3) {
this.field3 = field3;
}
public void myList_change() {
if (selectedItem < 1) {
setField1(null);
}
if (selectedItem < 2) {
setField2(null);
}
if (selectedItem < 3) {
setField3(null);
}
}
public void submit() {
}
}
In the xhtml I
Removed the field1 field2 field3 from the process attribute since the seemed useless (submission takes place on the 'submit', no ajax submit needed).
Also removed them from the update attribute (the change of a field becoming required after a change does not need an update)
Changed the actionListener on the p:commandButton to an `action
XHTML code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Example</title>
</h:head>
<h:body bgcolor="white">
<h:form id="myForm">
<p:messages showDetail="true" />
<p:selectOneMenu id="myList" value="#{myBean.selectedItem}">
<f:selectItem itemLabel="1" itemValue="1" />
<f:selectItem itemLabel="2" itemValue="2" />
<f:selectItem itemLabel="3" itemValue="3" />
<p:ajax listener="#{myBean.myList_change}"
process="myList"/>
</p:selectOneMenu> <br/>
<p:inputNumber id="field1" value="#{myBean.field1}" required="true" decimalPlaces="0" />
<p:inputNumber id="field2" value="#{myBean.field2}" required="#{myBean.selectedItem gt 1}" decimalPlaces="0" />
<p:inputNumber id="field3" value="#{myBean.field3}" required="#{myBean.selectedItem gt 2}" decimalPlaces="0" />
<p:commandButton value="submit" action="#{myBean.submit}" update="#form"/>
</h:form>
</h:body>
</html>
And then for me it started 'working'. In the chat, it became ovbious that OP had the fixed list of fields in the process and update attributes for a specific reason.
In the update attribute so when the select was changed from e.g. 3 to 2, the 3rd field could be cleared (set in the bean), requiring an 'upate' to make it empty.
In the process attribute so when the select was changed to e.g. 3, the 2nd field (not set/cleared in the bean) would be submitted AND updated. If it was not in the process attribute, the original value from the bean would be put back in, effectively overriding the value the user might have typed in.
But this combination was the cause of weird behaviour, since if something is processed, it is validated too (in this case related to something being required or not). Having three empty fields, setting the select to 3 and submitting all three fields will fail since 2 are empty but the '3' won't be put in the model either then. I would have suspected the resetValues to help out here too but it did not (does not for me either). But what OP effectively wanted is to conditionally update an input field. The condition for this is already in the bean and you can simply add the update of the specific field that changes there too
public void myList_change() {
if (selectedItem < 1) {
setField1(null);
}
if (selectedItem < 2) {
setField2(null);
PrimeFaces.current().ajax().update("myForm:field2");
}
if (selectedItem < 3) {
setField3(null);
PrimeFaces.current().ajax().update("myForm:field3");
}
}
There is totally no need for the field1, 2 and 3 to be in either the process or update attribute and all works as required (pun intended)

Trigger notification bar from <p:ajax> event

I'm looking to trigger a notification bar on checkbox click using <p:ajax>. The notification bar would have the save option to save data in the database. The update atribute within <p:ajax> doesn't seem to work. It's been quite a research, but couldn't derive at a solution. Am I missing something here ? I'm using JSF 2.1+Primefaces 3.5. Any help would be much appreciated.
xhtml page
<p:column headerText="#{msgs.ref}" styleClass="menuHeader textAlignLeft">
<p:notificationBar widgetVar="customSaveBar" position="top" styleClass="saveBar warningBar textAlignCenter" style="height: 25px;padding-top: 0; padding-bottom: 0;" effect="none" rendered="true">
<h:outputText value="There are pending changes on the page." styleClass="warningText"/>
<h:outputText value=" "/>
<p:commandLink action="#{abcDashboardBean.abcDTOValues}"
onclick="skipPageRedirectWarning = true;showPleaseWait();"
styleClass="warningText" id="saveFromBar"
onsuccess="customSaveBar.hide()"
oncomplete="placeRemoveIcon();"
update="#form"
process="#form"
value="#{msgs.save}"/>
</p:notificationBar>
<p:selectBooleanCheckbox styleClass="margin_left_10" value="#{abcDto.refBl}"
rendered="#{!authorizationBean.userADMIN or !authorizationBean.userPM or !authorizationBean.userINDM}">
<p:ajax event="click" partialSubmit="true" update="customSaveBar"></p:ajax>
</p:selectBooleanCheckbox>
</p:column>
abcDashboardBean.java
public Map<Object, Boolean> getAbcDTOValues(){
Map<Object, Boolean> map = new LinkedHashMap<Object, Boolean>();
//fill the map with the items defaulted to unchecked
for (AbcDTO abcDTO: abcList){
map.put(abcDTO.getPpcCode(), Boolean.FALSE);
}
Abc abc1 = dataAccessEjbService.find(Abc.class, abc.getId());
saveAbcRefChanges(map, abc1);
return map;
}
public void saveAbcRefChanges(Map<Object, Boolean> map, Abc abc) {
for (Map.Entry<Object, Boolean> entry : map.entrySet()) {
if (entry.getValue().equals(true) && abc != null) {
abc.setReferenceBl(true);
} else {
abc.setReferenceBl(false);
}
}
dataAccessEjbService.update(abc);
}
AbcDTO.java
private boolean refBl;
// getters + setters

Why can't I update a p:graphicImage twice after upload a file with p:uploadFile

I have been trying to upload an image with a p:uploadFile and it work fine at first time, but after the first uploaded file it not update the p:graphicImage, but if I reload the page the value of the graphicImage is fine and the image is showed. So, I think that is a problem with the primefaces uploadFile component, but I not sure.
My xhtml is this:
<h:form id="formNuevo" size="150%" enctype="multipart/form-data">
<h:outputLabel value="Diseño Neumatico: *" />
<p:fileUpload fileUploadListener="#{serviciosVentanaDiseno.manejarUploadedFile}" mode="advanced" auto="true" sizeLimit="9000000000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" update="mensajes graficImage"/>
<p:graphicImage id="graficImage" styleClass="disenoNeumatico" value="#{serviciosVentanaDiseno.streamedContentImagen}" ajax="true"/>
</h:form>
and my bean is a #SessionScoped bean, it's here:
public void manejarUploadedFile(FileUploadEvent event) {
UploadedFile uploadedFile = (UploadedFile)event.getFile();
try {
diseno.setImagen(uploadedFile.getContents());
streamedContentImagen = new DefaultStreamedContent(new ByteArrayInputStream(diseno.getImagen()));
} catch (IOException e) {
//log error
}
}
public StreamedContent getStreamedContentImagen() {
return streamedContentImagen;
}
public void setStreamedContentImagen(StreamedContent streamedContentImagen) {
this.streamedContentImagen = streamedContentImagen;
}
Try setting the cache attribute of the graphicImage component to false:
<p:graphicImage cache="false"
change scope #SessionScoped then <p:graphicImage cache="false">

PrimeFaces <f:attribute not working correctly with p:galleria attribute

I have issue when trying to make command link from inside p:galleria component
The problem is despite the fact at run time the link value value="Show present #{present.name} #{present.presentId}" contains the correct value of the id as example value="Show present Foo 1" , when pressing the command link it sends the wrong id of the second object every time
<h:form>
<p:galleria value="#{presentBean.allPresentList}" var="present" panelWidth="500" panelHeight="313" showCaption="true">
<f:facet name="content">
<h:commandLink value="Show present #{present.name} #{present.presentId}" action="pretty:present" actionListener="#{presentBean.setPresentObj}">
<f:attribute name="present" value="#{present.presentId}"/>
</h:commandLink>
</f:facet>
</p:galleria>
</h:form>
#ManagedBean(name="presentBean")
#SessionScoped
public class PresentBean implements Serializable{
ArrayList<Present> allUserPresentList = new ArrayList<Present>();
#PostConstruct
private void usersPresent(){
PresentDao presentDao = new PresentDaoImpl();
allPresentList = (ArrayList<Present>) presentDao.findAllPresents();
}
public ArrayList<Present> getAllUserPresentList() {
return allUserPresentList;
}
public void setAllUserPresentList(ArrayList<Present> allUserPresentList) {
this.allUserPresentList = allUserPresentList;
}
private String presentId ;
public String getPresentId() {
return presentId;
}
public void setPresentId(String presentId) {
this.presentId = presentId;
}
public void setPresentObj(ActionEvent ev){
Object presentOb = ev.getComponent().getAttributes().get("present");
if(presentOb != null){
this.presentId = (String) presentOb;
}else{
presentId = null ;
}
}
}
You need to use a setPropertyActionListener instead of <f:attribute name="present" value="#{present.presentId}"/> as the f:attribute tag is only evaluated when the component is created (only once) not when the component generates html based on the iterated rows.
So you'll need to instead use:
<f:setPropertyActionListener target="#{presentBean.presentId}" value="#{present.presentId}" />
That will set the value of the presentId in your managed bean, so in your action method you can just access the presentId itself already without having to work it out.
Alternatively if you're using a later version of JSF (using Servlet 3.0 or above), then you could create a method in the managed bean which takes the presentId or even the present object as a parameter
e.g. in your managed bean:
public void myAction(Present p){
//do whatever you want with the Present object
}
and in your .xhtml:
<h:commandLink value="Show present #{present.name} #{present.presentId}" actionListener="#{presentBean.myAction(present)}">
</h:commandLink>

Haw can I do for making button show me fileupload

haw can I do for diplay fileupload when I press a commande button.
<p:commandButton icon="ui-icon-refresh" onclick="data.show()"></p:commandButton>
this is my Fileupload
<h:form enctype="multipart/form-data" id="t" >
<p:fileUpload auto="true" disabled="true" id="data"
fileUploadListener="#{composantbean.handleFileUpload}"
sizeLimit="2097152"
label="Choose"
allowTypes="/(\.|\/)(pdf)$/"
description="Images"/>
</h:form>
on this solution the file uplad is displayed ..haw can I do
I don't quite understand your need. You want to display the p:fileUpload only after you click the p:commandButton?
If so, you need to create a boolean variable in your bean(controller) and when clicking on the button, set it true. It would look like this:
.xhtml
<p:commandButton icon="ui-icon-refresh" action="#{testController.renderFileUpload()}" update="#this"/>
<p:fileUpload auto="true" id="data" rendered="#{testController.isRenderFU()}"
fileUploadListener="#{composantbean.handleFileUpload}"
sizeLimit="2097152"
label="Choose"
allowTypes="/(\.|\/)(pdf)$/"
description="Images"/>
testController
...
private boolean renderFU = false;
public void renderFileUpload(){
renderFU = true;
}
public boolean isRenderFU() {
return renderFU;
}
public void setRenderFU(boolean renderFU) {
this.renderFU = renderFU;