【问题标题】:NullPointerException when managing bean with Spring+JSF(Primefaces)使用 Spring+JSF(Primefaces) 管理 bean 时出现 NullPointerException
【发布时间】:2013-08-13 15:37:31
【问题描述】:

我检查了很多地方,但在我的配置中找不到错误。请帮忙。

faces-config.xml

<managed-bean>
    <managed-bean-name>notificationServlet</managed-bean-name>
    <managed-bean-class>com.comviva.workflow.ui.notification.NotificationServlet</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>notificationDao</property-name>
        <value>#{notificationDaoService}</value>
    </managed-property>
</managed-bean>

applicationContext.xml

<bean id="notificationDao" class="com.comviva.workflow.ui.dao.NotificationDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="notificationDaoService"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref local="myTransactionManager" />
    </property>
    <property name="target">
        <ref local="notificationDao" />
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
            <prop key="get*">PROPAGATION_NEVER</prop>
        </props>
    </property>
</bean>

Java 类

@SessionScoped
@ManagedBean(name = "notificationServlet")
public class NotificationServlet extends HttpServlet
{

private static final long serialVersionUID = -3905661117247475401L;

@ManagedProperty("#{notificationDao}")
NotificationDao notificationDao;
}

我收到 NullPointerException。请建议我缺少什么。

编辑:-

2013-08-13 21:22:34,407 INFO  (NotificationServlet.java:43) notificationDao :: null
2013-08-13 21:22:34,407 ERROR (NotificationServlet.java:60) Exception ::
java.lang.NullPointerException
    at com.comviva.workflow.ui.notification.NotificationServlet.doPost(NotificationServlet.java:44)
    at com.comviva.workflow.ui.notification.NotificationServlet.doGet(NotificationServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

编辑:-

我不太擅长这些技术,但据我所知,我使用这些技术是因为-

Spring - 创建 bean 对象以防止每次访问类时创建多个对象。

Servlet - 我必须像“通知”一样实现 facebook,所以我在 JSP 页面中“计时(轮询)”servlet URL 以检查是否有任何值更新。

JSF - 我用它来创建 UI。我是 JSF 的新手,所以我不知道如何用它来实现 Spring。所以我问我是否遗漏了什么。

我的班级中有 setter/getter 方法。我只是没有显示它以节省空间。 (如果混淆了假设,请道歉。)

解决方案:-

我从 Java 类中删除了 faces-config.xml 和 JSF 注释中的两个 bean。我正在通过使用:-

在 java 类本身中捕获 dao bean
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    notificationDao =  (NotificationDao)context.getBean("notificationDao");

感谢您的建议。

【问题讨论】:

  • 究竟是哪里抛出了异常,能贴出堆栈跟踪吗?
  • 顺便说一句,您已经在 faces-config xml 中定义了 ManagedBean 和 Managed 属性以及类中的注释。其中一个就足够了。它们是声明托管 bean 和属性的两种不同方式。
  • 我做了更改,请检查。
  • 为什么你的托管 bean 还需要扩展一个 servlet?请同时提供 doGet/doPost 方法。
  • 我强烈建议立即停止并后退一步。您显然错过了 1) Servlets、2) JSF 和 3) Spring 的全部要点,并且不知何故将所有这些 API/框架视为同一事物并将它们放在一堆中。本质上,您的具体问题是因为您有 2 个完全不同的同一类实例,一个充当 JSF 托管 bean,另一个充当 HTTP servlet。

标签: java spring jsf


【解决方案1】:

这里有很多问题。

首先,您可以在 faces-config.xml 中声明 bean,也可以在类代码中使用 JSF 注解。

此外,要在运行时设置托管属性,您还需要该属性的 setter 和 getter 方法。所以你应该像这样改变你的java类:

Java 类

public class NotificationServlet extends HttpServlet
{

private static final long serialVersionUID = -3905661117247475401L;

//No need for annotations since you have managed-property definition in faces-config.xml whichever you prefer. 
//Also if you use the annoation the right way to do it would be @ManagedProperty("value =  #{notificationDao}"), you missed value = there

NotificationDao notificationDao;


public NotificationDao getNotificationDao() {
    return notificationDao;
}

public void setNotificationDao(NotificationDao notificationDao) { //Setter getter necessary
    this.notificationDao = notificationDao;
}


}

最后一个提醒。确保将以下行放入应用标记之间的 faces-config.xml 中,以便 JSF 能够将 Spring bean 定义用作托管 bean

<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

【讨论】:

  • 看,我有 setter/getter,我只是没有在帖子中显示它们以节省空间。我在 faces-config.xml 中有 el-resolver。我从 Java 类中删除了 faces-config.xml 和 JSF 注释中的两个 bean。我正在通过使用 WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); 在 java 类本身中捕获 dao bean
【解决方案2】:

我从 Java 类中删除了 faces-config.xml 和 JSF 注释中的两个 bean。我正在通过使用:-

在 java 类本身中捕获 dao bean
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
notificationDao =  (NotificationDao)context.getBean("notificationDao");

如果是另一个类,我正在使用这个 -

ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
notificationDao = (NotificationDao) ctx.getBean("notificationDao");

【讨论】:

    猜你喜欢
    • 2012-05-12
    • 2015-01-11
    • 2012-05-21
    • 2011-09-12
    • 1970-01-01
    • 2014-09-02
    • 2013-04-16
    • 2012-10-11
    • 2014-10-28
    相关资源
    最近更新 更多