p:inputSwitch Listener only executes once - jsf

I´m having a Problem with the Input Switch of Primefaces and the ajax change event for it. My Setup is following
<p:inputSwitch id="idOfTheSwitch"
style="width: 40px"
value="#{form.booleanWhichGetsChanged}">
<p:ajax listener="#{form.methodWhichShouldGetCalled()}"/>
</p:inputSwitch>
The Problem here is the listener executes the Method in the Beans just once.
public void methodWhichShouldGetCalled() {
// Should do something every time the Input Switch is getting switched.
// Loads filtered Data
}
Versions:
<!-- JSF -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<!-- Primefaces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.2.18</version>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>6.2.10</version>
</dependency>
The Standard event for ajax ist change, isn´t it?
The Name of the Beans and the Method is changed for Stackoverflow.

Related

Primefaces ContentFlow does not render the full content

I'm trying to use the Primefaces JSF component called contentFlow, which is demonstrated here: http://www.primefaces.org/showcase/ui/multimedia/contentFlow.xhtml
I added two images to the backing bean. They both can be displayed without using contentFlow, so they are available and at the right place. The problem is, that the contentFlow only shows the currently selected image. The expected behaviour is, that it displays all other images, too.
I googled alot and also found the documentation of Primefaces components to verify my XHTML. I also changed the JSF log level to see all debug messages, but JSF keeps telling me that everything is fine.
I've done nothing more than the showcase says, except for the configuration of the project. Here is a summary of my setting:
I'm using Apache Tomcat 8.0.39 on Windows x64
Firefox 50.0.2 and Chrome Version 55.0.2883.75 m (64-bit) are showing the same results (only the currently selected image is visible)
I'm using Java 8
And these are the project's dependencies
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.0.RC4</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.14</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.14</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
This is my backing bean:
package de.schuettec.jsfquestion.contentFlow;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
#ManagedBean
public class Images {
private List<String> images;
#PostConstruct
public void init() {
images = new ArrayList<String>();
images.add("img-01.png");
images.add("img-02.png");
}
public List<String> getImages() {
return images;
}
}
This is my XHTML page:
<!DOCTYPE html>
<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>
</h:head>
<h:body>
<h:form>
<p:contentFlow value="#{images.images}" var="image">
<p:graphicImage name="/images/#{image}" styleClass="content" />
<div class="caption">#{image}</div>
</p:contentFlow>
</h:form>
</h:body>
</html>
I've created a minimal reproducing example, which is available via GitHub here https://github.com/schuettec/jsf-question. This is a full Maven project that reproduces this issue.
I would appreciate if someone of the JSF/Primefaces experts can have a look at this issue. I like JSF and the Primefaces components and really want to understand whats wrong here.
Thank you in advance!
Your code is right. I think it's a bug or a feature.
add 2 pics -> shows 1 pic
add 3 pcis -> shows 3 pics
add 4 pics -> shows 3 pics
add 5 pics -> shows 5 pics
And you can use the final version from primefaces.
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.0</version>
</dependency>

Primefaces outputlabel behaves as escape = "false"

I am using primefaces 4.0 and I am having problems with p:outputLabel.
When It is mixed with any other primefaces component it behaves like escape="false"
This field is not processed even if I specify escape="true"
Example:
I have the following html code:
<h:form>
<p:outputLabel value="<b>TEXT</b>" escape="true"></p:outputLabel>
<h:outputLabel value="<b>TEXT</b> " escape="true"></h:outputLabel>
</h:form>
And for the Output I have this
<b>TEXT</b> <b>TEXT</b>
Which is expected behavior. (I am willing to see HTML tags as text).
But when I will add any other primefaces tag like in example:
<h:form>
<p:outputLabel value="<b>TEXT</b>" escape="true"></p:outputLabel>
<h:outputLabel value="<b>TEXT</b> " escape="true"></h:outputLabel>
<p:commandButton value="button"></p:commandButton>
</h:form>
Now even I have escape="true" I get
TEXT TEXT and then the button.
Does any one have experience with this how should I fix this.
Any suggestion.
I solved this issue.
I had this into my pom
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.12</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.12</version>
</dependency>
I just downgraded to 2.1.11 and everything works fine.

JSF2 ui:repeat nullpointerexception on development stage

I have an issue (no big deal but wanted to see if anybody else had seen it) where I have an structure like the one below:
<ui:repeat var="categoria"
value="#{newsletterPage.categoriasNewsletter}" varStatus="it">
boring code...
<ui:repeat
value="#{newsletterPage.getOportunidadesPremium(categoria)}"
var="trio">
boring code...
</ui:repeat>
categoria is an Enum from the enum list categoriasNewsletter and by each category I need to get some data from getOportunidadesPremium(categoria).
The error happens exactly on getOportunidadesPremium because somehow when categoria is passed as a parameter it's null inside the method. The funny story is this is null ONLY when using development stage, if it is production stage everything is fine, just by changing the:
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
I'm using primefaces 3.4 and
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.10</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.10</version>
</dependency>
I'm not asking for a fix for it as I suppose must be an api issue (and in production it'll be Production), just wanted to know if you have seen anything like this.
Thanks.

What is the function of #this exactly?

As far as I know the #this is to denote the current component triggering the event, such as :
<p:commandButton process="#this" ... />
And in JSF 2 Ajax, the #this can also mean the encapsulating component, like :
<h:inputText ...>
<f:ajax execute="#this" ... />
</h:inputText>
And I have one case where using p:datatable, including or excluding #this can have a different impact upon Ajax partial submit
Here's the example, in this case, the process is using #this, and this works as expected, where when process happens first, and then followed by setPropertyActionListener and last, the action is executed :
<p:column>
<p:commandLink
value="#{anggaranDetail.map['code']}"
process="#this infoAnggaranForm:Anggaran"
update="detailDialogForm:Anggaran detailDialogForm:SubAnggaran"
oncomplete="infoAnggaranDialog.hide()"
image="ui-icon ui-icon-search"
action="#{tInputBean.updateAnggaranSubAnggaran}">
<f:setPropertyActionListener value="#{anggaranDetail}"
target="#{infoAnggaranBean.selectedAnggaranDetail}" />
</p:commandLink>
</p:column>
But when I omit the #this from this example, the setPropertyActionListener and the action are never executed, as if they're not there.
I wonder why ? Perhaps #this has some other meaning other than the current component, perhaps the current record in this example ?
Im using tomcat 7, and these are my dependencies :
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
The PrimeFaces process and standard JSF execute attributes should point to spaceseparated component identifiers of components which JSF should process during the entire JSF lifecycle upon an ajax request (get request parameters, validate them, update model, execute action). The process defaults to #form, the current form, and the execute defaults to #this, the current component. In command links/buttons this is mandatory to execute the actions associated with the link/button itself.
However, in your datatable you have process="#this infoAnggaranForm:Anggaran", thus two components to process. If you omit #this but keep the other component, then it will only process/execute the other component and not the link/button component. If you omit the process attribute it will default to #form. If you have more other input components in the same form, then they will also be processed.
Depending on the concrete functional requirement, you could just keep it process="#this infoAnggaranForm:Anggaran", or omit it. JSF will then process/execute at least both the button and the other component, exactly as you want.
See also:
Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes

valueChangeListener and ajax execution order problem on selectOneMenu

I'm currently experiencing a strange phenomena, where setting the value to the model happens before executing valueChangeListener.
Basically what i would like to happen is :
I change the select menu
The new value get submitted using Ajax to update the myBean (using f:ajax)
Execute a query based on the newly submitted value (using valueChangeListener)
Render the table based on the result from the last query
But what happens when i run it is in this order, 1 - 3 - 2 - 4 (not 1 - 2 - 3 - 4 as i imagined)
Here's the part of the UI :
<h:selectOneMenu label="budget" id="budget"
converter="genericConverter"
value="#{myBean.budget}"
valueChangeListener="#{myBean.actionSearch}">
<f:ajax render="myGrid" />
<f:selectItem itemLabel="Choose one .." noSelectionOption="true" />
<f:selectItems ... />
</h:selectOneMenu>
And this is the excerpt from the bean :
public void actionSearch() {
System.out.println("searching with this.budget == " + this.budget);
...
}
public void setBudget(String budget) {
System.out.println("setting budget : " + budget);
this.budget = budget;
}
And this is the output :
searching with this.budget == xxxx
setting budget : yyyy
What i would like is, setting the budget first from the new value selected in the UI, and then start searching based on the new UI. But i dont know how to achieve that.
And im using Tomcat 7 along with these :
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
I wonder what i did wrong ?
Thank you !
The valueChangeListener is executed during Validations phase, before the Update Model Values phase. It is intented to be able to get a handle of both the old and new value so that you can if necessary do some business stuff (logging?) based on the real change. In JSF 1.x this was however more than often (ab)used to invoke actions on a dropdown change only, but it has to be used in combination with onchange="submit()", immediate="true", FacesContext#renderResponse() and other stuffs. The selected value is to be obtained by ValueChangeEvent#getNewValue() (the ValueChangeEvent is supposed to be definied as method's argument).
For JSF 2.x you don't need the valueChangeListener anymore in such case. You're also not interested in the old value anyway. Use the listener attribute of <f:ajax> instead.
<f:ajax listener="#{bean.actionSearch}" />
This will be executed during Invoke Action phase, after Update Model Values phase.

Resources