2017-07-14 116 views
0

我無法在Spring MVC tomcat 7.0.56中運行控制器,因爲它每次顯示「HTTP Status 404 error」。 Spring MVC可以在WEB-INF內運行welcome.jsp,但不能從控制器的路徑運行。 '/你好'。我已經從SO查看了幾篇文章,以找出這個但沒有運氣。我在這裏做錯了什麼?下面是我的代碼:HTTP狀態404運行控制器上的Spring MVC錯誤

項目結構:

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> 
+0

刪除'ControllerClassNameHandlerMapping'並添加''。 –

+0

我添加了,但仍然不起作用。我在上面的問題中做了修改。 –

+0

刪除歡迎文件列表,並像這樣更改映射@RequestMapping(value = {「/」,「/ hello」}) –

回答

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; 
}