#ManagedProperty null after #PostConstruct in JSF 2.2 - jsf

My specs:
Dynamic Web Module 3.1
GlassFish Web Extensions 4.0
Java 1.8
JavaScript 1.0
JavaServer Faces 2.2
Server: glassfish-4.1.1
OS: Win 10
IDE: Version: Neon.2 Release (4.6.2)
Please, note that I have researched this topic and found several related posts.
e.g.
#ManagedProperty + #PostConstruct + init() = Nullpointer
#ManagedProperty injected AFTER #PostConstruct
But neither of the proposed solutions worked for me or applied to my situation.
I don't mix CDI and/or JSF and/or Spring. It's JSF 2.2 annotations only.
I inject #ManagedProperty("#{country}") Country country; to my ChangeCountrySystemEventListener but the value of the #ManagedProperty country is null.
I don't really see where the issue is. The Country constructor does get invoked.
Any tips where the issues is?
Here's my full code:
index.xhtml
<!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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<h3><h:outputText value="#{country.name}" /> </h3>
<h:form>
<h:commandButton
id="changeCountryNameBtn"
value="Change"
action="result"
actionListener="#{appBean.changeCountryName}"
/>
</h:form>
</h:body>
</html>
AppBean.java
package com.test.beans;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
#ManagedBean
#SessionScoped
public class AppBean {
public void changeCountryName(ActionEvent ev) {
FacesContext context = FacesContext.getCurrentInstance();
Application app = context.getApplication();
app.publishEvent(context, ChangeCountrySystemEvent.class, ev.getSource());
System.out.println(">>>> AppBean.publishEvent(ChangeCountrySystemEvent) fired... " + ev.getSource());
}
}
ChangeCountrySystemEvent.java
package com.test.beans;
import javax.faces.event.SystemEvent;
public class ChangeCountrySystemEvent extends SystemEvent {
private static final long serialVersionUID = -1587717461942271611L;
public ChangeCountrySystemEvent(Object source) {
super(source);
System.out.println(">>>> ChangeCountrySystemEvent.class :: constructor invoked!");
}
}
ChangeCountrySystemEventListener.java
package com.test.beans;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
public class ChangeCountrySystemEventListener implements SystemEventListener {
#ManagedProperty("#{country}")
Country country;
// getters and setters
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public ChangeCountrySystemEventListener(FacesContext fc) {
super();
System.out.println(">>>> ChangeCountrySystemEventListener.class :: Listener constructor invoked!!!");
}
#Override
public void processEvent(SystemEvent se) {
if (country != null) {
country.setName("Sweden");
System.out.println(">>>> ChangeCountrySystemEventListener.class :: SYSTEM EVENT PROCESSED... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ");
} else if (country == null) {
System.out.println(">>>> ChangeCountrySystemEventListener.class :: processEvent() > country managed property is EMPTY !!!!");
}
}
#Override
public boolean isListenerForSource(Object source) {
return true; // needs to be set to true, otherwise "processEvent" won't be called...
}
}
Country.java
package com.test.beans;
import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
#ManagedBean(name = "country", eager = true)
#SessionScoped
public class Country {
private String name = "Norway";
public Country() {
System.out.println(">>>> Country constructor called...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#PostConstruct
public void init() {
FacesContext fc = FacesContext.getCurrentInstance();
Application app = fc.getApplication();
app.subscribeToEvent(ChangeCountrySystemEvent.class, new ChangeCountrySystemEventListener(fc));
System.out.println(">>>> Country.class :: app.subscribeToEvent() called... ");
}
}
Console output:
2017-04-02T21:49:51.392-0300|Info: JSF_TestProject was successfully deployed in 579 milliseconds.
2017-04-02T21:49:52.251-0300|Info: >>>> Country constructor called...
2017-04-02T21:49:52.277-0300|Info: >>>> ChangeCountrySystemEventListener.class :: Listener constructor invoked!!!
2017-04-02T21:49:52.277-0300|Info: >>>> Country.class :: app.subscribeToEvent() called...
2017-04-02T21:50:16.572-0300|Info: >>>> ChangeCountrySystemEvent.class :: constructor invoked!
2017-04-02T21:50:16.572-0300|Info: >>>> ChangeCountrySystemEventListener.class :: processEvent() > country managed property is EMPTY !!!!
2017-04-02T21:50:16.572-0300|Info: >>>> AppBean.publishEvent(ChangeCountrySystemEvent) fired... javax.faces.component.html.HtmlCommandButton#424c250c

The ManagedProperty can be used on a field of a class annotated with ManagedBean to inject a value into this property.
If this annotation is present on a class that does not have the ManagedBean annotation, the implementation must take no action on this annotation.
Please see http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html
Since the class ChangeCountrySystemEventListener is not annotated with ManagedBean, no action is taken on the ManagedProperty field country and its null.

Related

JSF converter fails to intialize

I have a facelet page in which i have a primefaces selectCheckboxMenu where a user can select one or more conferences.
Primefaces 8.0,
5.194 #badassfish (build 327)|#]
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.3.9</version>
<scope>provided</scope>
</dependency>
I neeed to create a converter to convert between object and string. But no matter what I'm doing it does not work.
Either the stateless EJB I inject to be used as a service is not instantiated or I receive a NPE when CDI tries to create the converter:
Error Rendering View[/conferences.xhtml]
java.lang.NullPointerException
at com.sun.faces.cdi.CdiUtils.createConverter(CdiUtils.java:102)
at com.sun.faces.application.applicationimpl.InstanceFactory.createConverter(InstanceFactory.java:481)
at com.sun.faces.application.ApplicationImpl.createConverter(ApplicationImpl.java:510)
at javax.faces.application.ApplicationWrapper.createConverter(ApplicationWrapper.java:431)
at org.primefaces.renderkit.SelectRenderer.findImplicitConverter(SelectRenderer.java:202)
at org.primefaces.renderkit.SelectRenderer.getOptionAsString(SelectRenderer.java:181)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeOption(SelectCheckboxMenuRenderer.java:143)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeOption(SelectCheckboxMenuRenderer.java:137)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeInputs(SelectCheckboxMenuRenderer.java:128)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeMarkup(SelectCheckboxMenuRenderer.java:93)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeEnd(SelectCheckboxMenuRenderer.java:63)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:595)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1654)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:152)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:566)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1647)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:468)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:170)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:102)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:199)
at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:708)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1628)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:755)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:575)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:520)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:217)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
at java.base/java.lang.Thread.run(Thread.java:834)
|#]
JSF1073: java.lang.NullPointerException caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=null|#]
No associated message
java.lang.NullPointerException
at com.sun.faces.cdi.CdiUtils.createConverter(CdiUtils.java:102)
at com.sun.faces.application.applicationimpl.InstanceFactory.createConverter(InstanceFactory.java:481)
at com.sun.faces.application.ApplicationImpl.createConverter(ApplicationImpl.java:510)
at javax.faces.application.ApplicationWrapper.createConverter(ApplicationWrapper.java:431)
at org.primefaces.renderkit.SelectRenderer.findImplicitConverter(SelectRenderer.java:202)
at org.primefaces.renderkit.SelectRenderer.getOptionAsString(SelectRenderer.java:181)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeOption(SelectCheckboxMenuRenderer.java:143)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeOption(SelectCheckboxMenuRenderer.java:137)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeInputs(SelectCheckboxMenuRenderer.java:128)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeMarkup(SelectCheckboxMenuRenderer.java:93)
at org.primefaces.component.selectcheckboxmenu.SelectCheckboxMenuRenderer.encodeEnd(SelectCheckboxMenuRenderer.java:63)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:595)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1654)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:152)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:566)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1647)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:468)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:170)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:102)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:199)
at javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:708)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1628)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:755)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:575)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:520)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:217)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
at java.base/java.lang.Thread.run(Thread.java:834)
Please find all my code below.
My facelet:
<?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:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<style type="text/css">
.ui-selectlistbox-listcontainer{
overflow: initial !important;
}
</style>
</h:head>
<h:body>
<h2>SelectManyMenu Example</h2>
<h:form>
<p:selectCheckboxMenu value="#{conferenceSelectionBean.selectedConferences}"
multiple="true"
filter="true" filterMatchMode="contains"
style="width:300px;">
<f:selectItems value="#{conferenceSelectionBean.allConferences}" var="conf"
itemLabel="#{conf.title}" itemValue="#{conf}"/>
</p:selectCheckboxMenu>
<p:commandButton value="Submit" action="#{conferenceSelectionBean.blabla}" update="selectedEmpPanel"/>
<h3>Selected Employees:</h3>
<h:panelGrid id="selectedEmpPanel" columns="1">
<ui:repeat value="#{conferenceSelectionBean.selectedConferences}" var="conf">
<h:outputText value="#{conf.id} (#{conf.title}) - #{conf.date}"/>
<br/>
</ui:repeat>
</h:panelGrid>
</h:form>
</h:body>
</html>
My Conference bean:
package com.logicbig.example;
public class Conference {
private String id;
private String title;
private String date;
public Conference(String id, String title, String date) {
this.id = id;
this.title = title;
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
My converter
package com.logicbig.example;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
#FacesConverter(forClass=Conference.class, managed=true)
public class ConferenceConverter implements Converter<Conference> {
#Inject
ConferenceService conferenceService;
public ConferenceConverter() { System.out.println("HOLA 2");
}
#Override
public Conference getAsObject(FacesContext fc, UIComponent comp, String value) {System.out.println("Get as object");
if(conferenceService == null){
System.out.println("SERVICE IS NULL");
}
return null;
}
#Override
public String getAsString(FacesContext fc, UIComponent comp, Conference value) { System.out.println("Get as String");
return ((Conference) value).getId();
}
}
In this case i receive the exception mentioned in the beginning.
Also, I've tried this:
package com.logicbig.example;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
#FacesConverter(value = "confConverter")
public class ConferenceConverter implements Converter {
#Inject
ConferenceService conferenceService;
public ConferenceConverter() { System.out.println("HOLA 2");
}
#Override
public Object getAsObject(FacesContext fc, UIComponent comp, String value) {System.out.println("Get as object");
if(conferenceService == null){
System.out.println("SERVICE IS NULL");
}
return null;
}
#Override
public String getAsString(FacesContext fc, UIComponent comp, Object value) { System.out.println("Get as String");
return ((Conference) value).getId();
}
}
and entered the link in my xhtml:
<p:selectCheckboxMenu value="#{conferenceSelectionBean.selectedConferences}"
multiple="true" converter="confConverter"
filter="true" filterMatchMode="contains"
style="width:300px;">
<f:selectItems value="#{conferenceSelectionBean.allConferences}" var="conf"
itemLabel="#{conf.title}" itemValue="#{conf}"/>
</p:selectCheckboxMenu>
In this case the injected bean is always null.
My backing bean
package com.logicbig.example;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
#Named
#ViewScoped
public class ConferenceSelectionBean implements Serializable {
#Inject
ConferenceService conferenceService;
private List<Conference> selectedConferences = new ArrayList();
private List<Conference> allConferences;
#PostConstruct
private void init() {
allConferences = conferenceService.getAllConferences();
}
public void blabla(){
for(Conference c : selectedConferences){
System.out.println(c.getId() +" " + c.getTitle()+ " " + c.getDate());
}
}
public ConferenceService getConferenceService() {
return conferenceService;
}
public void setConferenceService(ConferenceService conferenceService) {
this.conferenceService = conferenceService;
}
public List<Conference> getSelectedConferences() {
return selectedConferences;
}
public void setSelectedConferences(List<Conference> selectedConferences) {
this.selectedConferences = selectedConferences;
}
public List<Conference> getAllConferences() {
return allConferences;
}
public void setAllConferences(List<Conference> allConferences) {
this.allConferences = allConferences;
}
}
My stateless session bean
package com.logicbig.example;
import java.util.Arrays;
import java.util.List;
import javax.ejb.Stateless;
#Stateless
public class ConferenceService {
private final List<Conference> conferences;
public ConferenceService() {
conferences = Arrays.asList(
new Conference("1", "Cool conference 1", "2010-05-10"),
new Conference("2", "Cool conference 2", "2010-05-10"),
new Conference("3", "Cool conference 3", "2010-05-10"),
new Conference("4", "Cool conference 4", "2010-05-10"),
new Conference("5", "Cool conference 5", "2010-05-10")
);
}
public List<Conference> getAllConferences() {
return conferences;
}
public Conference getConferenceById(String value) {
return conferences.stream()
.filter(e -> e.getId().equals(value))
.findAny().orElse(null);
}
}
and my configuration bean
package com.logicbig.example;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.annotation.FacesConfig;
#ApplicationScoped
#FacesConfig(version = FacesConfig.Version.JSF_2_3)
public class ConfigurationBean {
public ConfigurationBean() {
}
}
EDIT:
If I change the beginning of my converter to:
#FacesConverter(value = "confConverter", managed = true)
public class ConferenceConverter implements Converter<Object> {
the injection of the EJB service works as expected.
I need to have the following in my XHTML page
<f:converter converterId="confConverter"/>
So now it works as expected and I can retire the #MangedBean code.
As you already answered yourself, you need a converter for your non-primitive datatype.
For primitive datatypes or generic datatypes where the generic runtime information is lost there is regression / bug in mojarra which maybe is fixed in 2.3.10. https://github.com/eclipse-ee4j/mojarra/issues/4500.
Enums
converting enums without using omnifaces.genericEnumConverter
generics
Create concrete implementation class.
I had a class DataRange<T extends Comparable<T>> like DataRange<Date> and in p:calendar value="#{cc.attrs.range.start}" mojarra only retrieved Comparable<T> instead Date in conversion phase.
As a workaround i am using DataRangeDate to make sure runtime information of Date is not lost.
primefaces 6.19
This basically occurs when relying on default converters like in p:calendar or p:inputNumber.

SessionScoped Beans created multiple times for same request on (fast) double click

environment:
omnifaces 2.6
mojarra 2.2.12
weld-servlet-2.2.9
tomcat 8
and a minimal bean-setup:
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
#Named("test")
#SessionScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = -2697121845047289456L;
#PostConstruct
public void init(){
System.out.println("TestBean.init()");
}
}
test.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
#{test}
</ui:composition>
after tomcat is started, requesting quickly many times page test.xhtml (GET) results in the following console-output:
TestBean.init()
TestBean.init()
TestBean.init()
TestBean.init()
the same behaviour occurs when replacing cdi with jsf-annotations:
#ManagedBean(name="test")
#javax.faces.bean.SessionScoped
public class TestBean implements Serializable {
}
any ideas what this is caused by how to fix that?
Update:
i just reproduced this behaviour with a brand new project to ensure that there is nothing polluted in my code or configuration. only included latest mojarra 2.2.14 and tomcat 8.0.36. drag the link
localhost:8080/SessionBeanTest/test
to browser bar and started fast clicking on this link while tomcat starts. you'll see the same outputs. how can i prevent the creation of multiple sessions for the same request?
package test;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
#WebListener
public class SessionCounter implements HttpSessionListener {
private static int count;
#Override
public void sessionCreated(HttpSessionEvent event) {
System.out.println("session created: " + event.getSession().getId());
count++;
}
#Override
public void sessionDestroyed(HttpSessionEvent event) {
System.out.println("session destroyed: " + event.getSession().getId());
count--;
}
public static int getCount() {
return count;
}
}
web.xml
<listener><listener-class>test.SessionCounter</listener-class>
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name="test")
#SessionScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = -1L;
#PostConstruct
public void init(){
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
System.out.println("TestBean.init() with requested session-id: " + request.getRequestedSessionId());
}
public Integer getTotalSessions(){
return SessionCounter.getCount();
}
}
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">
<h:head>
<title>SessionBean-Test</title>
</h:head>
<h:body>
<h2>#{test.totalSessions}</h2>
</h:body>
output for clicking fast 4 times:
session created: 85473180423E40234184E2B748A3BF65
session created: 984C12EAF08B8A427C23B8CA98C6EF10
session created: DA147A440A9D03860B110E8AD063B548
session created: 577261D977E090D3E67E35DB55A97CD5
TestBean.init() with requested session-id: D385B49654C289FE41E718BE3BC9F5FA
TestBean.init() with requested session-id: D385B49654C289FE41E718BE3BC9F5FA
TestBean.init() with requested session-id: D385B49654C289FE41E718BE3BC9F5FA
TestBean.init() with requested session-id: D385B49654C289FE41E718BE3BC9F5FA

Why is JSF applying the Bean Validation of a shadowed private field?

I have encountered some surprising behaviour in Hibernate Validator and JSF. I would like to know whether the behaviour is a bug, or a misunderstanding in my own expectations.
I have this Facelets page:
<?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">
<h:body>
<h:form>
<h:inputText value="#{backingBean.someClass.someField}"/>
<h:commandButton value="submit" action="#{backingBean.submit1()}"/>
</h:form>
</h:body>
</html>
And I have this backing bean:
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.hibernate.validator.constraints.NotEmpty;
#ManagedBean
#RequestScoped
public class BackingBean {
private SomeClass someClass = new SomeSubClass();
public SomeClass getSomeClass() {
return someClass;
}
public void setSomeClass(SomeClass someClass) {
this.someClass = someClass;
}
public void submit1() {
System.out.println("BackingBean: " + someClass.getSomeField());
((SomeSubClass) someClass).submit2();
}
public static class SomeClass {
private String someField;
public String getSomeField() {
return someField;
}
public void setSomeField(String someField) {
this.someField = someField;
}
}
public static class SomeSubClass extends SomeClass {
#NotEmpty
private String someField;
private void submit2() {
System.out.println("SomeSubClass: " + someField);
}
}
public static void main(String[] args) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
SomeClass someClass = new SomeSubClass();
someClass.setSomeField("ham");
Set<ConstraintViolation<SomeClass>> errors = validator.validate(someClass);
for (ConstraintViolation<SomeClass> error : errors) {
System.out.println(error);
}
}
}
Note that SomeSubClass.someField has the same name as a private variable in the parent class. This should not matter because you cannot shadow a private field.
Two things to note:
If you run the main method in BackingBean, the validator will always return a validation error (message "may not be empty") because SomeSubClass.someField is always null. This behaviour seems correct to me.
If you submit the form with a blank value, you will receive a "may not be empty" error message; if you enter a value, it will pass validation, but you will see in the console that the value of SomeSubClass.someField is still null. This behaviour seems incorrect to me.
If you change the name of SomeSubClass.someField to someField2, you may submit the form with an empty value. This behaviour seems incorrect to me.
It appears that the JSF's Validation Phase and Hibernate Validator disagree on this behaviour. JSF is applying the #NotEmpty validator of a private field in a subclass to a field of the same name in the parent class, but Hibernate Validator does not show this behaviour when tested in isolation.
Can anyone explain this behaviour? Is it a bug, or a misunderstanding in my own expectations?
Using:
GlassFish Server Open Source Edition 3.1.2.2
Mojarra 2.1.6
Hibernate Validator 4.3.0.Final
JSF uses EL to get/set model values conform Javabean rules. EL uses reflection to inspect and invoke public getters and setters. In other words, #{backingBean.someClass.someField} actually uses getSomeField() and setSomeField() to get/set the model/submitted value. The field being manipulated is then actually the one in SomeClass.
Bean Validation uses reflection to inspect fields and ignores the presence of public getters/setters. In other words, BV of #{backingBean.someClass.someField} actually takes place on SomeSubClass.
This explains everything. But I agree that this is confusing and appears "incorrect". It'll work as expected when you also override the getter and setter in SomeSubClass.
public static class SomeSubClass extends SomeClass {
#NotEmpty
private String someField;
private void submit2() {
System.out.println("SomeSubClass: " + someField);
}
#Override
public String getSomeField() {
return someField;
}
#Override
public void setSomeField(String someField) {
this.someField = someField;
}
}
This way EL will see it and use that as model value.
This is however awkward (an #Override which merely calls super will flip static code style analysis tools like Sonar, PMD, Findbugs, etc). A better alternative is to move #NotEmpty to an overridden getter:
public static class SomeSubClass extends SomeClass {
private void submit2() {
System.out.println("SomeSubClass: " + getSomeField());
}
#Override
#NotEmpty
public String getSomeField() {
return super.getSomeField();
}
}
BV also supports this construct, so it'll stay working.

Getting selection from p:selectOneMenu in PrimeFaces

I want to select a value from a p:selectOneMenu component (a dropdownlist) in Primefaces. I get my data from a Java Bean. I have the following code:
XHTML:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<h:form>
<p:messages id="errorMessages" style="color:red;margin:8px;" />
<br></br>
<p:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Tasks: "/>
<p:selectOneMenu value="#{devTestController.selectedTask}">
<f:selectItems value="#{devTestController.tasks}" var="task" itemLabel="#{task.label}" itemValue="#{task.value}"/>
<f:converter converterId="infoRowBeanConverter" />
</p:selectOneMenu>
</p:panelGrid>
<br/>
<p:commandButton value="Execute Task" update = "errorMessages" action="#{devTestController.executeTask()}"/>
</h:form>
</h:body>
</html>
Java Bean DevTestController.java:
package mypackage;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
#ManagedBean
#RequestScoped
public class DevTestController
{
private InfoRowBean selectedTask;
private static List<InfoRowBean> tasks;
#PostConstruct
public void initList()
{
if (tasks == null)
{
tasks = new LinkedList<>();
tasks.add(new InfoRowBean("Task 1", "Task 1"));
tasks.add(new InfoRowBean("Task 2", "Task 2"));
}
}
public InfoRowBean getSelectedTask()
{
return selectedTask;
}
public void setSelectedTask(InfoRowBean selectedTask)
{
this.selectedTask = selectedTask;
}
public List<InfoRowBean> getTasks()
{
return tasks;
}
public void executeTask()
{
System.out.println("Executing task " + selectedTask.label);
}
}
InfoRowBean.java:
package mypackage;
import java.util.List;
public class InfoRowBean
{
String label = null;
String value = null;
public InfoRowBean(String label, String value)
{
setLabel(label);
setValue(value);
}
public String getLabel()
{
return label;
}
public void setLabel(String label)
{
this.label = label;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
// This must return true for another InfoRowBean object with same label/id.
public boolean equals(Object other)
{
return other instanceof InfoRowBean && (label != null) ? label.equals(((InfoRowBean) other).label) : (other == this);
}
// This must return the same hashcode for every InfoRowBean object with the same label.
public int hashCode()
{
return label != null ? this.getClass().hashCode() + label.hashCode() : super.hashCode();
}
// Override Object#toString() so that it returns a human readable String representation.
// It is not required by the Converter or so, it just pleases the reading in the logs.
public String toString()
{
return "InfoRowBean[" + label + "," + value + "]";
}
}
Converter InfoRowBeanConverter.java:
package mypackage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
#FacesConverter("infoRowBeanConverter")
public class InfoRowBeanConverter implements Converter
{
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
return value;
}
public String getAsString(FacesContext context, UIComponent component, Object value)
{
return value.toString();
}
}
If I press the button nothing happens (no error also). If I remove the parameter "value" from tag (namely leave ), the button works fine, but of course I don't get the selected item. What is the problem here?
The problem is that your converter isn't converting the submitted string value to a concrete InfoRowBean instance in getAsObject() method, but instead returning the raw submitted String value as you have generated in getAsString() method. This doesn't match the type of selectedTask, which is InfoRowBean.
You need to fix your converter accordingly in such way that getAsString() returns the unique string representation of the complex object, usually in flavor of the database identifier (so that it can be used further in text based formats such as HTML output and HTTP request parameters), and that getAsObject() converts exactly that unique string representation back to the concrete complex object instance, usually via a DB call using the unique identifier as key.
An alternative is to use omnifaces.SelectItemsConverter of the JSF utility library OmniFaces, so that you never need to create custom converters for components using <f:selectItem(s)> with complex objects as values.
Another alternative is to change selectedTask to be String instead of InfoRowBean (and get rid of the whole converter as it is completely useless in this construct).
See also:
How to populate options of h:selectOneMenu from database?

#ManagedProperty does not work in a CDI managed bean

I try to learn JSF and encountered on a problem connected with ManagedProperty. However I have tried to use it, it always failed - null exception pointer. What am I doing wrongly?
I have read some "similar posts" on stackoverflow, but they did not helped to me. (I use GlassFish 4.0, JSF 2.2, JDK 1.7, Netbeans 7.3.1 (Java EE pack) and Java EE 6.0)
<?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://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<br/>
User: #{books.user.name}<br/>
1: #{param.pageId}<br/>
2: #{books.pageId}<br/>
<h:form>
<h:inputText value="#{user.name}" /><br/>
<h:inputText value="#{books.v1}" /><br/>
<h:inputText value="#{books.v2}" /><br/>
<h:inputText value="#{books.result}" /><br/>
<h:commandButton value="dodaj" action="#{books.add}" />
</h:form>
</h:body>
</html>
Book
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tpsa.books.managed;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedProperty;
/**
*
* #author tomasz
*/
#Named(value = "books")
#RequestScoped
public class Books {
private int v1;
private int v2;
private int result;
#ManagedProperty(value = "#{user}")
private User user;
#ManagedProperty(value="#{param.pageId}")
private int pageId;
/**
* Creates a new instance of Books
*/
public Books() {
}
public void add() {
result = v1 + v2;
}
public int getV1() {
return v1;
}
public void setV1(int v1) {
this.v1 = v1;
}
public int getV2() {
return v2;
}
public void setV2(int v2) {
this.v2 = v2;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public User getUser() {
if (user == null) {
System.err.println("WTF");
}
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getPageId() {
return pageId;
}
public void setPageId(int pageId) {
this.pageId = pageId;
}
}
User
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package tpsa.books.managed;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
/**
*
* #author tomasz
*/
#Named(value = "user")
#SessionScoped
public class User implements Serializable {
private String name;
/**
* Creates a new instance of User
*/
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
#ManagedProperty is managed bean annotaion, that can't be used with CDI. In above code, you used CDI bean i.e. #Named that is default in JSF 2.2. In this case you can't use ManagedProperty. Please read following line copied from Java EE docs of ManagedBean.
If this annotation is present on a class that does not have the
ManagedBean annotation, the implementation must take no action on this
annotation.
For details see the link:
http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedProperty.html
So, use #Inject instead of #ManagedProperty for CDI bean.
#Inject
private User user;
Note that a getter/setter is unnecessary here.

Resources