调用Spring MVC项目中创建的JSP文件时在本地主机上获取404错误消息

时间:2019-07-15 17:54:51

标签: java spring

调用Spring MVC项目中创建的JSP文件时,在localhost上收到404错误消息

我已经在eclipse中创建了动态Web项目,并在其中添加了spring jars。 创建控制器并通过它调用jsp文件,但得到404错误消息。服务器u

web.xml:

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd“ id =” WebApp_ID“ version =” 3.0“>   FirstMVCProject

<servlet>
    <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

1 个答案:

答案 0 :(得分:0)

您没有在web.xml中加载上下文:

<init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/applicationContext.xml</param-value>
 </init-param>

适当的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>example</display-name>
  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>