Java 在web.xml中添加ContextLoaderListener和RequestContextListener时未找到资源(404)
当我将Java 在web.xml中添加ContextLoaderListener和RequestContextListener时未找到资源(404),java,spring,spring-mvc,Java,Spring,Spring Mvc,当我将ContextLoaderListener添加到我的web.xml中时,得到了未找到资源的消息(404)。当我从web.xml中删除侦听器类时,我的index.php正在运行,但正在添加 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener&
ContextLoaderListener
添加到我的web.xml
中时,得到了未找到资源的消息(404)。当我从web.xml
中删除侦听器类时,我的index.php
正在运行,但正在添加
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
*
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FutureKid</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<description></description>
<display-name>InputFormServlet</display-name>
<servlet-name>InputFormServlet</servlet-name>
<servlet-class>com.webapp.InputFormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InputFormServlet</servlet-name>
<url-pattern>/input-form</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>CustomerServlet</display-name>
<servlet-name>CustomerServlet</servlet-name>
<servlet-class>com.webapp.CustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomerServlet</servlet-name>
<url-pattern>/get-customer</url-pattern>
</servlet-mapping>
</web-app>
未来小子
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
InputFormServlet
InputFormServlet
com.webapp.InputFormServlet
InputFormServlet
/输入表格
客户服务
客户服务
com.webapp.CustomerServlet
客户服务
/获得客户
仅仅添加ContextLoaderListener
是不够的,您还需要配置由ContextLoaderListener
加载的bean。通常,您可以为ContextLoaderListener
(通过上下文参数contextConfigLocation
)配置spring xml文件的名称,然后向该文件添加一些spring配置
你也应该阅读,它会让你对这两个spring上下文有一些简单的理解。它还包含
web.xml的一些示例配置
首先将以下内容添加到web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/spring.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-INF/spring/spring.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.request.RequestContextListener
接下来在WEB-INF/spring文件夹中创建一个spring.xml,并根据需要进行更改
示例spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="1" />
</beans:bean>
</beans:beans>
php应用程序的ContextLoaderListener?您能提供整个web.xml吗?Jens:这是纯java应用程序。基本上是EclipseKepler版本中的web动态项目。Yadvendra:我正在发布整个web.xml,还有你的SpringDispatcherservlet?您的“com.webapp.InputFormServlet”是什么?这看起来不像SpringMVC标准配置。当然,如果你有充分的理由,你可以这样做,但是你应该知道Spring是如何工作的。如果没有,请继续使用Spring标准方式,使用ContextLoaderListener和DispatcherServlet,但不使用其他Servlet。(本教程看起来不错:)我正在使用applicationContext.xml来放置bean。因此,我认为不需要spring.xml。我还使用RequestDispatcher for JSP提供了地址。还需要spring.xml吗?解决了!!!通过在我的运行时ext文件夹中使用xercesjar解决了这个问题。实际上,这是一个XSD解析器异常,这就是为什么我得到了“资源不可用”消息。您还可以通过在maven依赖项中添加xerces JAR来解决这个问题。因此,我认为不需要spring.xml。我还使用RequestDispatcher for JSP提供了地址。是否也需要spring.xml?
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="1" />
</beans:bean>
</beans:beans>