2017-08-11 147 views
0

我是新的Spring MVC,我只是想为它的基本示例。但我收到Http Stats 404错误。我的文件如下:HTTP状态代码404春天mvc

这是一个maven项目。

/Web-Inf/spring-mvc-demo-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation=" 
     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/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <!-- Step 3: Add support for component scanning --> 
    <context:component-scan base-package="springdemo" /> 

    <!-- Step 4: Add support for conversion, formatting and validation support --> 
    <mvc:annotation-driven/> 

    <!-- Step 5: Define Spring MVC view resolver --> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/view/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

然后我有WEB-INF/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>spring-mvc-demo</display-name> 

    <!-- Spring MVC Configs --> 

    <!-- Step 1: Configure Spring MVC Dispatcher Servlet --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet --> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

/WEB-INF /视图/主-page.jsp

<html> 

<body> 

<h1>Welcome to home page!!!!</h1> 

</body> 

</html> 

然后SRC/HomeC ontroller.java

package springmvcdemo; 

import org.springframework.stereotype.Component; 
import org.springframework.web.bind.annotation.RequestMapping; 

@Component 
public class HomeController { 

    @RequestMapping("/") 
    public String startPage(){ 
     return "main-page"; 
    } 
} 

我创建Eclipse IDE中动态Web项目,这是一个Maven项目和服务器上我试图运行它是由Tomcat 8.0.44

任何人可以帮助我消除错误?

+0

是不是你的'spring-mvc-demo-servlet.xml'和'web.xml'是一样的吗?请发布你的'spring-mvc-demo-servlet.xml'。 –

+0

@罗西罗宾逊我改变了文件..我错误地打了同一个文件。现在它是正确的。 – Akki

+0

检查你的'组件扫描'。您在包'com.luv2code.springdemo'被扫描,然后你的控制器似乎是在'springdemo' –

回答

0

由于您的servlet名称是web.xml中的调度程序。春天将尝试加载配置文件名为调度-servlet.xml的

在调度-servlet.xml中,你必须提供组件扫描

+0

,但param-init和param-value是什么。会不会找到正确的XML? – Akki

+0

它应该。按照Anil的建议添加组件扫描 –

1

我在春天MVC-演示的servlet看到包名。 XML不是与包匹配你在控制器

弹簧MVC-演示servlet.xml中: - 。

HomeController中,

package springmvcdemo;