2014-01-29 100 views
2

我正在嘗試使用Primefaces 3.5和Glassfish 4.0在netbeans 7.4,SPRING 3.2.3,JSF 2.2中創建一個Web。在支持bean中注入Spring服務

我的目標是創建一個非常簡單的項目,在後臺bean中注入服務。 到目前爲止,我已經用這兩個框架在NetBeans中創建了一個新項目。這些都是在我的項目中的文件:

項目樹

webApplication1 
    ├ Web Pages 
    │ ├ WEB-INF 
    │ │ ├ applicationContext.XML 
    │ │ ├ dispatcher-servlet.XML 
    │ │ ├ faces-config.XML 
    │ │ └ web.XML 
    │ └ index.XHTML 
    ├ source packages 
    │ └ controladores 
    │   ├ IndexBB.java 
    │   ├ Servicio.java 
    │   └ ServicioImpl.java 
    ├ Test Packages 
    ├ Libraries 
    ├ Test Libraries 
    └ Configuration Files 

的index.xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<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:ui="http://java.sun.com/jsf/facelets" 
     xmlns:p="http://primefaces.org/ui"> 

    <f:view contentType="text/html">  
     <h:body> 
      <h:form> 
       <p:inputText id="aInput" value="#{indexBB.mensaje}" /> 
       <p:commandButton id="btnTest" actionListener="#{indexBB.getServiceSMS}" value="Test" update="aInput" /> 
      </h:form>    
     </h:body> 
    </f:view> 
</html> 

IndexBB.java

package controladores; 

import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ManagedProperty; 
import javax.faces.bean.ViewScoped; 

@ManagedBean 
@ViewScoped 
public class IndexBB implements Serializable{ 

    @ManagedProperty(value="#{rfService}") 
    private Servicio elServicio; 

    private String mensaje="a default SMS"; 

    public IndexBB() { 

    } 

    public String getMensaje() {   
     return this.mensaje; 
    } 

    public void setMensaje(String mensaje) { 
     this.mensaje = mensaje; 
    } 

    public Servicio getElServicio() { 
     return this.elServicio; 
    } 

    public void setElServicio(Servicio elServicio) { 
     this.elServicio = elServicio; 
    } 

    public void getServiceSMS() { 
     this.mensaje=elServicio.getSMS();   
    } 
} 

Servicio.java

package controladores; 

public interface Servicio { 

    public String getSMS(); 
} 

ServicioImpl.java

package controladores; 

import org.springframework.stereotype.Service; 

@Service(value="rfService") 
public class ServicioImpl implements Servicio{ 

    @Override 
    public String getSMS() { 
    return "a message generated by a service"; 
    } 
} 

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 


</beans> 

調度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

    <!-- 
    Most controllers will use the ControllerClassNameHandlerMapping above, but 
    for the index controller we are using ParameterizableViewController, so we must 
    define an explicit mapping for it. 
    --> 
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props> 
       <prop key="index.xhtml">indexController</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/" 
      p:suffix=".xhtml" /> 

    <!-- 
    The index controller. 
    --> 
    <bean name="indexController" 
      class="org.springframework.web.servlet.mvc.ParameterizableViewController" 
      p:viewName="index" /> 


</beans> 

faces-config.xml中

<?xml version='1.0' encoding='UTF-8'?> 
<faces-config version="2.2" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> 

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

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/applicationContext.xml</param-value> 
    </context-param> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>2</load-on-startup> 
    </servlet> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 
</web-app> 

正如你所看到的項目是非常簡單的。只需一個從服務中獲取值的按鈕將其放入一個變量中即可。

的問題是,當我按下按鈕的Glassfish給了我一個空指針這個堆棧跟蹤:

javax.el.ELException: java.lang.NullPointerException 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:279) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) 
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149) 
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:300) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: java.lang.NullPointerException 
    at controladores.IndexBB.getServiceSMS(IndexBB.java:48) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:275) 

在我看來,服務沒有被正確注射,所以當我按下按鈕,它是NULL。

我在網上搜索沒有結果(我找到的大部分代碼都不起作用)。 有人可以配置Spring的經驗看看我的項目嗎? 在此先感謝!

回答

0

您有3個問題。

首先你有一個DispatcherServlet這在JSF環境中是不需要的。刪除它,它的映射。您不需要JSF處理的所有控制器內容。

其次,你有一個@Service,但沒有任何東西是拿起那個bean。在您的applicationContext.xml添加組件掃描。

<context:component-scan base-package="controladores" /> 

第三的託管bean有錯誤的制定者。您必須有一個符合@ManagedProperty註釋中的值的setter,所以在您的情況下,您應該有一個setRfService(..)方法。

+0

我已經刪除了DispatcherServlet,添加了「xmlns:context =」http://www.springframework.org/schema/context「」和你的行到applicationContext。我還稱這項服務爲「elServicio」,但它仍然不起作用。 –

+0

Stacktrace:加載應用程序時出現異常:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:來自ServletContext的XML文檔中的第25行resource [ /WEB-INF/applicationContext.xml]無效;嵌套異常是org.xml.sax.SAXParseException; lineNumber:25; columnNumber:60; cvc-complex-type.2.4.c:Elcomodíncoincidente es estricto,pero no se ha encontrado ningunadeclaraciónpara el elemento'context:component-scan'。 –

+0

」有任何想法嗎? –

1

非常感謝M. Deinum! 你的答案非常接近,我只需要谷歌一點! 在ADITION所有你的話我添加這些行到應用上下文的「豆」開始標籤內:

xmlns:context="http://www.springframework.org/schema/context" 

而這其中的XSI裏面:的schemaLocation

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 

我也刪除了在web.xml線即宣告分派:

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>2</load-on-startup> 
</servlet> 

這:

<servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

謝謝!!!