org.springframework.web.servlet.DispatcherServlet noHandlerFound错误

时间:2017-09-09 15:43:27

标签: spring spring-mvc

您好我在春天尝试了一个简单的MVC示例,但在加载url" / ModelAndViewDemo / listUsers"时出现404错误。我得到以下异常。我从类似的问题尝试了许多解决方案但没有工作。请指教。

  

2017年9月9日下午8:54:51 org.springframework.web.servlet.DispatcherServlet      noHandlerFound      警告:找不到带URI的HTTP请求的映射      DispatcherServlet中的[/ ModelAndViewDemo / listUsers],名称为' dispatcher

以下是我的代码:

调度-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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

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

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

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

控制器类:

package com.manjosh.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.manjosh.model.User;
import com.manjosh.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/listusers", method = RequestMethod.GET)
    public ModelAndView registerSuccess() {

        ModelAndView modelAndView = new ModelAndView("UserList");
        List<User> userlist = userService.getUserList();
        modelAndView.addObject("userList", userlist);
        return modelAndView;

    }

}

3 个答案:

答案 0 :(得分:0)

您将控制器映射到/listusers,但是您拨打了/listUsers 将映射更改为/listUsers或调用/listusers

答案 1 :(得分:0)

我明白了。 dispatcher-servlet xml在InternalResourceViewResolver属性中缺少“/”。我添加了“/”并解决了问题。

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

答案 2 :(得分:0)

就我而言,问题在于基本软件包不正确。因此,程序包名称应与您定义的程序包匹配。

yum

<context:component-scan base-package="org.springdemo.controller" /> 更改为org,并且有效。

com