HTTP状态404运行控制器上的Spring MVC错误

时间:2017-07-14 09:29:14

标签: java jsp spring-mvc servlets tomcat7

我无法在Spring MVC tomcat 7.0.56中运行控制器,因为它每次都显示“ HTTP状态404错误”。 Spring MVC可以在WEB-INF中运行welcome.jsp,但不能运行来自控制器的路径。 '的 /你好'。我已经查了几篇来自SO的帖子来弄清楚这一点但没有运气。我在这做错了什么?以下是我的代码:

项目结构:

enter image description here

的web.xml

  <display-name>Archetype Created Web Application</display-name>

  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/welcome.jsp</welcome-file>
  </welcome-file-list>


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

  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

用SpringMVC-servlet.xml中

    <context:component-scan base-package="org.spring"></context:component-scan>

   <context:annotation-config></context:annotation-config>
   <mvc:annotation-driven />

   <mvc:default-servlet-handler/>

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView"/>
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>
   </beans>

springController.java

 package org.spring;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    public class SpringController {

        @RequestMapping(value = "/hello")
    public ModelAndView helloworld() {
        ModelAndView model = new ModelAndView("welcome");
        return model;
    }
    }

的welcome.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
Welcome to Spring MVC
</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.springMVC</groupId>
  <artifactId>SpringMVC</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <java-version>1.8</java-version>
    <org.springframework-version>4.2.5.RELEASE</org.springframework-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
    </dependency>

    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

    <dependency>
        <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.2.5.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.2.5.RELEASE</version>
    </dependency>

     <dependency>
        <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.2.5.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>SpringMVC</finalName>
  </build>
</project>

1 个答案:

答案 0 :(得分:0)

更改代码

下面的SpringMVC-servlet.xml文件
     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
     <context:component-scan base-package="org.spring"/>
     <mvc:annotation-driven/>


    @RequestMapping(value = {"/","/index","/hello"})
    public ModelAndView helloworld() {
    ModelAndView model = new ModelAndView("welcome");
    return model;
}