Spring在DispatcherServlet中找不到带有URI [/ FirstSpringMVCProject / welcome]的HTTP请求的映射,名称为' spring-dispatcher'

时间:2017-04-08 17:23:32

标签: java spring spring-mvc servlets annotations

我是Spring MVC的新手。我尝试创建一个新的Spring Web Application。我在我的eclipse控制台中收到以下错误在DispatcherServlet中找不到带有URI [/ FirstSpringMVCProject / welcome]的HTTP请求的映射,名称为' spring-dispatcher' 即可。但是在没有注释的情况下使用它可以正常工作。它不适用于注释方法。这是我的spring-dispatcher-servlet.xml 文件

spring-dispatcher-servlet.xml

HelloController.java

请找到我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>FirstSpringMVCProject</display-name>


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

</web-app>

我的spring-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd>
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">




    <bean id="HandlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

    <!-- Non-Annotation Based -->


    <context:component-scan base-package="com.gontuseries.hellocontroller.*" />

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


</beans>

我的HelloController.java文件是

package com.gontuseries.hellocontroller;

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

@Controller
public class HelloController {

    @RequestMapping("/welcome")
    public ModelAndView helloworld() {

        ModelAndView modelandview = new ModelAndView("HelloPage");
        modelandview.addObject("welcomeMessage",
                "Hi User, welcome to the first Spring MVC Application");
        System.out.println("ga");
        return modelandview;
    }
}

1 个答案:

答案 0 :(得分:-1)

您可以在您的web.xml中使用以下代码,并且URL将是b somthing / projectname / api / welcome

<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ApplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
   <filter-name>encoding-filter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
</welcome-file-list>