如何配置Spring框架以使用JSF

时间:2015-09-30 14:27:02

标签: spring jsf-2 primefaces

我在使用maven集成两个不同的项目时面临问题。

  1. SpringHibernateAnnotationExample是一个包含服务和dao相关内容的项目。
  2. Test是一个基于Web的项目,其中包含jsf和primefaces jar。
  3. 现在我的问题是在将SpringHibernateAnnotationExample集成到测试(Web)时,我遇到了一些问题,我将详细说明。

    1. SpringHibernateAnnotationExample具有BeanLocation.xml,它具有spring配置。
    2. 测试有一个单独的applicationContext文件。
    3. 现在我在web.xml中配置BeanLocation,但是当我运行具有jsf页面的应用程序时,我得到一些我无法理解的异常:

      Sep 30, 2015 6:53:06 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
      SEVERE: Error Rendering View[/index.xhtml]
      com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.java.view.HelloWorld.
          at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:191)
          at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
          at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
          at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
          at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
          at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
          at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
          at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
          at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
          at org.apache.el.parser.AstValue.getValue(AstValue.java:160)
          at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
          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:174)
          at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
          at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
          at com.sun.faces.renderkit.html_basic.LabelRenderer.encodeBegin(LabelRenderer.java:120)
          at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:864)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1894)
          at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
          at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
          at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
          at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
          at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
          at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
          at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
          at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
          at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
          at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
          at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
          at java.lang.Thread.run(Unknown Source)
      Caused by: java.lang.NullPointerException
          at com.java.view.HelloWorld.<init>(HelloWorld.java:41)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
          at java.lang.reflect.Constructor.newInstance(Unknown Source)
          at java.lang.Class.newInstance(Unknown Source)
          at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
          ... 49 more
      
      Sep 30, 2015 6:53:07 PM com.sun.faces.context.ExceptionHandlerImpl log
      1100: JSF1073: com.sun.faces.mgbean.ManagedBeanCreationException caught during processing of RENDER_RESPONSE 6 : UIComponent-ClientId=, Message=Cant instantiate class: com.java.view.HelloWorld.
      Sep 30, 2015 6:53:07 PM com.sun.faces.context.ExceptionHandlerImpl log
      1100: Cant instantiate class: com.java.view.HelloWorld.
      com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.java.view.HelloWorld.
          at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:191)
          at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100)
          at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
          at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
          at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257)
          at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117)
          at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
          at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
          at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
          at org.apache.el.parser.AstValue.getValue(AstValue.java:160)
          at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
          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:174)
          at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
          at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
          at com.sun.faces.renderkit.html_basic.LabelRenderer.encodeBegin(LabelRenderer.java:120)
          at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:864)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1894)
          at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
          at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1896)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
          at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899)
          at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451)
          at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
          at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
          at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
          at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
          at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
          at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
          at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
          at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
          at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
          at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
          at java.lang.Thread.run(Unknown Source)
      Caused by: java.lang.NullPointerException
          at com.java.view.HelloWorld.<init>(HelloWorld.java:41)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
          at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
          at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
          at java.lang.reflect.Constructor.newInstance(Unknown Source)
          at java.lang.Class.newInstance(Unknown Source)
          at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186)
          ... 49 more
      

      以下是我的文件:

      的web.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          id="WebApp_ID" version="2.5" metadata-complete="true">
              <context-param>
              <param-name>javax.faces.PROJECT_STAGE</param-name>
              <param-value>Development</param-value>
          </context-param>
          <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>Faces Servlet</servlet-name>
              <url-pattern>/faces/*</url-pattern>
          </servlet-mapping>
           <servlet-mapping>
            <servlet-name>Faces Servlet</servlet-name>
            <url-pattern>*.jsf</url-pattern> 
             <!-- Extension of file to be used like localhost:..../helloWorld.jsf -->
      
         </servlet-mapping>
          <session-config>
              <session-timeout>
                  30
              </session-timeout>
          </session-config>
          <welcome-file-list>
              <welcome-file>faces/index.xhtml</welcome-file>
          </welcome-file-list>
      
      
          <context-param>
          <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
          <param-value>.xhtml</param-value>
      </context-param>
      
      <context-param>
              <param-name>javax.faces.application.CONFIG_FILES</param-name>
              <param-value>/WEB-INF/faces-config.xml</param-value>
      </context-param>
      
          <context-param>
              <param-name>primefaces.THEME</param-name>
              <param-value>glass-x</param-value>
          </context-param>
      
          <!-- Adding for spring application Context loading -->
          <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
          </context-param>
          <listener>
      <listener-class>
          org.springframework.web.context.ContextLoaderListener
      </listener-class>
      </listener>
      <listener>
      <listener-class>
          org.springframework.web.context.request.RequestContextListener
      </listener-class>
      </listener>
      
      
      </web-app>
      

      面-配置:

       <?xml version="1.0" encoding="UTF-8"?>
      <faces-config
          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"
          version="2.2">
      
          <application>
              <el-resolver>
                   org.springframework.web.jsf.el.SpringBeanFacesELResolver
              </el-resolver>
          </application>
      
      
      </faces-config>
      

      的applicationContext:

         <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
          xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd">
      
          <context:component-scan base-package="com.java.view" />
      
          <import resource="classpath*:/BeanLocations.xml"/>
      
      </beans>
      

      Test.pom:

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.mytutorial</groupId>
          <artifactId>WebPrrimeFaces</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <packaging>war</packaging>
          <name>simpleWeb Maven Webapp</name>
          <description>Simple Web Test</description>
      
          <repositories>
              <repository>
                  <id>prime-repo</id>
                  <name>PrimeFaces Maven Repository</name>
                  <url>http://repository.primefaces.org</url>
                  <layout>default</layout>
              </repository>
          </repositories>
      
          <dependencies>
              <!-- Adding dependency for another project -->
              <dependency>
                  <groupId>com.mkyong.common</groupId>
                  <artifactId>SpringExample</artifactId>
                  <version>1.0-SNAPSHOT</version>
                  <type>jar</type>
              </dependency>
      
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>3.8.1</version>
                  <scope>test</scope>
              </dependency>
      
              <!-- Servlet -->
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>servlet-api</artifactId>
                  <version>2.5</version>
                  <scope>provided</scope>
              </dependency>
              <!-- Faces Implementation -->
              <dependency>
                  <groupId>com.sun.faces</groupId>
                  <artifactId>jsf-impl</artifactId>
                  <version>2.2.4</version>
              </dependency>
              <!-- Faces Library -->
              <dependency>
                  <groupId>com.sun.faces</groupId>
                  <artifactId>jsf-api</artifactId>
                  <version>2.2.4</version>
              </dependency>
              <!-- Primefaces Version 5 -->
              <dependency>
                  <groupId>org.primefaces</groupId>
                  <artifactId>primefaces</artifactId>
                  <version>5.0</version>
              </dependency>
              <!-- JSP Library -->
              <dependency>
                  <groupId>javax.servlet.jsp</groupId>
                  <artifactId>javax.servlet.jsp-api</artifactId>
                  <version>2.3.1</version>
              </dependency>
              <!-- JSTL Library -->
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>jstl</artifactId>
                  <version>1.1.2</version>
              </dependency>
              <!-- Primefaces Theme Library -->
              <dependency>
                  <groupId>org.primefaces.themes</groupId>
                  <artifactId>glass-x</artifactId>
                  <version>1.0.6</version>
              </dependency>
              <!-- Spring Web dependency -->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-web</artifactId>
                  <version>4.1.4.RELEASE</version>
              </dependency>
      
      
          </dependencies>
      
          <build>
              <sourceDirectory>src</sourceDirectory>
              <plugins>
                  <plugin>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>3.1</version>
                      <configuration>
                          <source>1.7</source>
                          <target>1.7</target>
                      </configuration>
                  </plugin>
                  <plugin>
                      <artifactId>maven-war-plugin</artifactId>
                      <version>2.3</version>
                      <configuration>
                          <warSourceDirectory>WebContent</warSourceDirectory>
                          <failOnMissingWebXml>false</failOnMissingWebXml>
                      </configuration>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      的HelloWorld:

      package com.java.view;
      
      import java.io.Serializable;
      
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.SessionScoped;
      
      import org.springframework.beans.factory.annotation.Autowired;
      
      import com.mkyong.stock.bo.StockBo;
      import com.mkyong.stock.model.Stock;
      
      @ManagedBean(name = "helloWorld")
      @SessionScoped
      public class HelloWorld implements Serializable {
      
          private static final long serialVersionUID = -6913972022251814607L;
      
          //private String s1 = "Hello World!!";
      
           @Autowired
          private StockBo stockBo;
      
          public StockBo getStockBo() {
              return stockBo;
          }
      
          public void setStockBo(StockBo stockBo) {
              this.stockBo = stockBo;
          }
      
      
      
          Stock stck=stockBo.findByStockCode("7668");
      
      
      
      }
      

      的index.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"
          xmlns:p="http://primefaces.org/ui"
          xmlns:f="http://java.sun.com/jsf/core">
      <h:head>
          <title>Hello World JSF Example</title>
      </h:head>
      <h:body>
      
          <h1>Hello World PrimeFaces</h1>
          <f:view>
              <h:form>
      
                  <h:outputLabel value="#{stck.stockName}" />
      
              </h:form>
      
          </f:view>
      
      </h:body>
      </html>
      

      我知道这不是一个很好的例子,但我的唯一目的是将spring相关bean的依赖注入到jsf / web应用程序中,其中我在web项目(Test)和其他applicationContext(BeanLocation.xml)中有applicationContext不同的项目。

0 个答案:

没有答案