简体   繁体   English

将事件从数据库加载到日历主界面

[英]loading events from a database into a calendar primefaces

i'm having a trouble in loading some dates into a calendar, using the "schedule" component in primefaces. 我在使用Primefaces中的“计划”组件将某些日期加载到日历时遇到麻烦。 Here is what my client.xhtml look like : 这是我的client.xhtml的样子:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">

    <body>

        <ui:composition template="./PlanningTemplate.xhtml">

            <ui:define name="top">
                top
            </ui:define>

            <ui:define name="content">
            <p:schedule id="schedule" value="#{scheduleController.eventModel}" widgetVar="myschedule">  
                 <p:ajax event="dateSelect" listener="#{scheduleController.onDateSelect}"  oncomplete="eventDialog.show()" />  
                 <p:ajax event="eventSelect" listener="#{scheduleController.onEventSelect}" oncomplete="eventDialog.show()" />  
                 <p:ajax event="eventMove" listener="#{scheduleController.onEventMove}"  />  
                 <p:ajax event="eventResize" listener="#{scheduleController.onEventResize}"  />  

            </p:schedule>
                <p:dialog widgetVar="eventDialog" header="Event Details" showEffect="clip" hideEffect="clip">  
        <h:panelGrid id="eventDetails" columns="2">  
            <h:outputLabel for="title" value="Title:" />  
            <p:inputText id="title" value="#{scheduleController.event.title}" required="true"/>  

            <h:outputLabel for="from" value="From:" />  
            <p:inputMask id="from" value="#{scheduleController.event.startDate}" mask="99/99/9999">  
                <f:convertDateTime pattern="dd/MM/yyyy" />  
            </p:inputMask>  

            <h:outputLabel for="to" value="To:" />  
            <p:inputMask id="to" value="#{scheduleController.event.endDate}" mask="99/99/9999">  
                <f:convertDateTime pattern="dd/MM/yyyy" />  
            </p:inputMask>  




        </h:panelGrid>  
    </p:dialog> 
            </ui:define>




            <ui:define name="bottom">
                bottom
            </ui:define>

        </ui:composition>

    </body>
</html>

The baking been looks like this : 烘烤看起来像这样:

@Named(value = "scheduleController")
@SessionScoped
public class scheduleController implements Serializable {
    @PersistenceContext(unitName="MajanTelepointagePU")
    EntityManager em;
    private ScheduleModel eventModel;
    private ScheduleEvent event = new DefaultScheduleEvent() {};

    List<Intervention> listOfAllInterventions = new ArrayList<Intervention>();


    public scheduleController(){
        eventModel = new DefaultScheduleModel();
        // Load here the events into the eventModel via the method aventmodel.addEvent(title,startDate,endDate)

        //get the list of all interventions
        listOfAllInterventions = em.createNamedQuery("Intervention.findAll").getResultList();
        for(Intervention i : listOfAllInterventions){
            eventModel.addEvent(new DefaultScheduleEvent(i.getNotesGeneral(),i.getHeureDebut(),i.getHeureFin()));

        }



    }
    public ScheduleModel getEventModel() {
        return eventModel;
    }

    public void setEventModel(ScheduleModel eventModel) {
        this.eventModel = eventModel;
    }

    public ScheduleEvent getEvent() {
        return event;
    }

    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }


    public void addEvent(ActionEvent actionEvent) {  
        if(event.getId() == null) { 
            eventModel.addEvent(event);  }
        else { 
            eventModel.updateEvent(event);  }

        event = new DefaultScheduleEvent();  
    }  

    public void onEventSelect(SelectEvent selectEvent) {  
        event = (ScheduleEvent) selectEvent.getObject();  
    }  

    public void onDateSelect(SelectEvent selectEvent) {  
        event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());  
    }  

    public void onEventMove(ScheduleEntryMoveEvent event) {  
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());  

        addMessage(message);  
    }  

    public void onEventResize(ScheduleEntryResizeEvent event) {  
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());  

        addMessage(message);  
    }  

    private void addMessage(FacesMessage message) {  
        FacesContext.getCurrentInstance().addMessage(null, message);  
    }  


}

but the client.xhtml is not running, and here is the full stacktrace of the exception : 但是client.xhtml没有运行,这是该异常的完整堆栈跟踪:

com.google.common.collect.ComputationException: java.lang.RuntimeException: java.lang.NullPointerException
    at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:397)
    at org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:102)
    at org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:115)
    at org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:96)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:188)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:179)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:64)
    at org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:77)
    at org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:51)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:59)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:47)
    at org.primefaces.component.dialog.DialogRenderer.encodeContent(DialogRenderer.java:172)
    at org.primefaces.component.dialog.DialogRenderer.encodeMarkup(DialogRenderer.java:101)
    at org.primefaces.component.dialog.DialogRenderer.encodeEnd(DialogRenderer.java:43)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
    at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:186)
    at org.jboss.weld.util.reflection.SecureReflections.newInstance(SecureReflections.java:341)
    at org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:242)
    at org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:87)
    at org.jboss.weld.bean.proxy.ClientProxyProvider.access$000(ClientProxyProvider.java:43)
    at org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:53)
    at org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:46)
    at com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:355)
    at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184)
    at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)
    at com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)
    at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:393)
    ... 62 more
Caused by: java.lang.NullPointerException
    at MajanTelepointage.BB.scheduleController.<init>(scheduleController.java:48)
    at MajanTelepointage.BB.scheduleController$Proxy$_$$_WeldClientProxy.<init>(scheduleController$Proxy$_$$_WeldClientProxy.java)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at java.lang.Class.newInstance0(Class.java:372)
    at java.lang.Class.newInstance(Class.java:325)
    at org.jboss.weld.util.reflection.SecureReflections$16.work(SecureReflections.java:344)
    at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
    at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:173)
    ... 73 more

More exactly, the error is referencing the following line : 更确切地说,该错误引用了以下行:

 listOfAllInterventions = em.createNamedQuery("Intervention.findAll").getResultList();

Thanks for Help :) 感谢帮助 :)

Did you understand the stack trace? 您了解堆栈跟踪吗? It's telling that em is null inside the constructor. 这表明在构造函数中emnull You can also just say that so instead of showing the stack trace. 您也可以这样说,而不是显示堆栈跟踪。 Exceptions from java.lang package are rather trivial. java.lang包中的异常非常简单。

In any way, injected dependencies are indeed not available in the constructor. 无论如何,注入的依赖关系确实在构造函数中不可用。 It's technically not possible to set the value of an instance field before the instance is constructed. 从技术上讲,在构造实例之前无法设置实例字段的值。 If you'd like to work on the injected dependencies directly after instance's construction, then you should be using a @PostConstruct annotated method. 如果您想在实例构造后直接处理注入的依赖项,则应该使用@PostConstruct注释方法。

Replace 更换

public scheduleController(){

by 通过

@PostConstruct
public void init() {

And em will be available in there. 并且em将在那里可用。


Unrelated to the concrete problem, Java naming conventions dictate that class names must start with uppercase. 具体问题无关Java命名约定规定类名必须以大写开头。 Please work on that as well. 也请您对此进行努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM