У меня есть страница Facelet, на которой у меня есть selectCheckboxMenu, где пользователь может выбрать одну или несколько конференций.
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>
Мне нужно было создать конвертер для преобразования между объектом и строкой. Но что бы я ни делал, это не работает.
Либо EJB без сохранения состояния, который я вводил для использования в качестве службы, не создается, либо я получаю NPE, когда CDI пытается создать преобразователь:
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)
Пожалуйста, найдите весь мой код ниже.
Мой фейслет:
<?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>
Мой компонент конференции:
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;
}
}
Мой конвертер
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();
}
}
В этом случае я получаю исключение, упомянутое в начале.
Кроме того, я пробовал это:
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();
}
}
и ввел ссылку в свой x html :
<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>
В этом случае внедренный bean-компонент всегда имеет значение null.
Мой вспомогательный 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;
}
}
Мой сеансовый 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);
}
}
и мой компонент конфигурации
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:
Если я изменю начало моего конвертера на:
@FacesConverter(value = "confConverter", managed = true)
public class ConferenceConverter implements Converter<Object> {
, внедрение службы EJB будет работать должным образом .
Мне нужно, чтобы на моей странице X HTML было следующее
<f:converter converterId="confConverter"/>
Итак, теперь он работает должным образом, и я могу удалить код @MangedBean. * 1 049 *