找不到WebApplicationContext:不在DispatcherServlet请求中,也没有注册ContextLoaderListener

时间:2018-01-25 12:20:39

标签: spring-mvc

我正在尝试编写一个spring MVC应用程序,我收到以下错误 码: 的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">
  <display-name>Nov29thSpringMVC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>nov</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>nov</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>

NOV-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation=" 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">


   <context:component-scan base-package="com.searchsme.controller" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

   </beans>

的index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form id="searchSME" modelAttribute="searchSME" action="search" method="post">
<table align="center">
                    <tr>
                        <td>
                            <form:label path="username">Search SME: </form:label>
                        </td>
                        <td>
                            <form:input path="search" name="searchTopic" id="searchTopic" />
                        </td>
                    </tr>

                    <tr>
                        <td></td>
                        <td align="left">
                            <form:button id="login" name="login">Search</form:button>
                        </td>
                    </tr>
                    <tr></tr>

                </table>

</form:form>
</body>
</html>

错误:

java.lang.IllegalStateException: No WebApplicationContext found: not in a DispatcherServlet request and no ContextLoaderListener registered?

我看到了一些旧帖子并在下面尝试过它不起作用

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

我收到以下异常

  

引起:java.io.FileNotFoundException:无法打开   ServletContext资源[/WEB-INF/applicationContext.xml]

我们还必须编写applicationContext.xml吗?我已经有了混淆文件。

1 个答案:

答案 0 :(得分:0)

Hello请在WEB-INF中添加web.xml和no-servlet.xml,然后尝试下面的代码。用于调度程序servlet。

<servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/nov-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

如果你使用maven项目,必须将它放在同一个文件夹中你可以通过将参数传递给init-param来从资源文件夹获取文件

类路径:配置文件中-name.xml的