spring:找不到WebApplicationContext:没有注册ContextLoaderListener?

时间:2017-09-03 09:40:52

标签: spring jsf

我试图在弹簧4中使用JSF 2.2。但每当我尝试访问我的person.xhtml文件http://localhost:8085/jsf/person.xhtml时,我都会收到错误。

dispatcher-servlet和applicationContext.xml文件是从工作项目复制的,所以我假设没有问题。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/dispatcher-servlet.xml
        /WEB-INF/applicationContext.xml
    </param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener
    </listener-class>
</listener>
  <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>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

我的jsf配置文件。

面-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>

我要在浏览器上呈现的页面person.xhtml。

person.xhtml

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    <title>JSF Spring Hibernate Integration</title>
    <style type="text/css">
.tg {
    border-collapse: collapse;
    border-spacing: 0;
    border-color: #ccc;
}

.tg td {
    font-family: Arial, sans-serif;
    font-size: 14px;
    padding: 10px 5px;
    border-style: solid;
    border-width: 1px;
    overflow: hidden;
    word-break: normal;
    border-color: #ccc;
    color: #333;
    background-color: #fff;
}

.tg th {
    font-family: Arial, sans-serif;
    font-size: 14px;
    font-weight: normal;
    padding: 10px 5px;
    border-style: solid;
    border-width: 1px;
    overflow: hidden;
    word-break: normal;
    border-color: #ccc;
    color: #333;
    background-color: #f0f0f0;
}

.tg .tg-4eph {
    background-color: #f9f9f9
}
</style>
</h:head>
<h:body>
    <h1>Add a Person</h1>
    <h:form>
        <table>
            <tr>
                <td><label>Name</label></td>
                <td><h:inputText id="name" value="#{person.name}">

                </h:inputText>
                </td>
            </tr>
            <tr>
                <td><label>Country</label></td>
                <td><h:inputText id="country" value="#{person.country}">

                </h:inputText>
                </td>
            </tr>
            <tr>
                <td colspan="2"><h:commandButton
                        action="#{personService.addPerson(person)}" 
                        value="Add Person">
                        </h:commandButton>
                </td>
            </tr>

        </table>

    </h:form>

    <br>
    <h3>Persons List</h3>


    <c:if test="${!empty personService.listPersons()}">
        <table class="tg">
            <tr>
                <th width="80">Person ID</th>
                <th width="120">Person Name</th>
                <th width="120">Person Country</th>
            </tr>
            <ui:repeat value="${personService.listPersons()}" 
            var="person">
                <tr>
                    <td>${person.id}</td>
                    <td>${person.name}</td>
                    <td>${person.country}</td>
                </tr>
            </ui:repeat>
        </table>
    </c:if>

</h:body>
</html>

的pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>jsf</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>jsf Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.13</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.13</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.14</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.14</version>
        </dependency>
        <dependency>
            <groupId>com.sun.el</groupId>
            <artifactId>el-ri</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>jsf</finalName>
    </build>
</project>

错误: -

Type Exception Report

Message /person.xhtml @77,53 test="${!empty personService.listPersons()}" 
No WebApplicationContext found: no ContextLoaderListener registered?

Description The server encountered an unexpected condition that prevented 
it from fulfilling the request.

Exception

javax.servlet.ServletException: /person.xhtml @77,53 
test="${!empty personService.listPersons()}" 
No WebApplicationContext found: no ContextLoaderListener registered?
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:671)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause

javax.faces.view.facelets.TagAttributeException: /person.xhtml @77,53 
test="${!empty personService.listPersons()}" 
No WebApplicationContext found: no ContextLoaderListener registered?

    com.sun.faces.facelets.tag.TagAttributeImpl.getObject(TagAttributeImpl.java:358)
    com.sun.faces.facelets.tag.TagAttributeImpl.getBoolean(TagAttributeImpl.java:150)
    com.sun.faces.facelets.tag.jstl.core.IfHandler.apply(IfHandler.java:91)
    javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
Root Cause

java.lang.IllegalStateException: No WebApplicationContext found: 
no ContextLoaderListener registered?
    org.springframework.web.jsf.FacesContextUtils
    .getRequiredWebApplicationContext(FacesContextUtils.java:81)
    org.springframework.web.jsf.el.SpringBeanFacesELResolver
    .getWebApplicationContext(SpringBeanFacesELResolver.java:90)
    org.springframework.web.jsf.el.SpringBeanFacesELResolver
    .getBeanFactory(SpringBeanFacesELResolver.java:78)
    org.springframework.beans.factory.access.el.SpringBeanELResolver
    .getValue(SpringBeanELResolver.java:49)

1 个答案:

答案 0 :(得分:0)

尝试使用以下内容在web-inf中创建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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    </beans>