【问题标题】:How to fix "noHandlerFound" in SpringMVC如何修复 Spring MVC 中的“noHandlerFound”
【发布时间】:2020-11-21 07:28:04
【问题描述】:

这是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="com.robert.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.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>

    <absolute-ordering />

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

这是控制器 java 文件

public class HelloWorldController {

    // need a controller method to show the initial HTML form
    @RequestMapping("/showForm")
    public String showForm() {
        return "helloworld-form";
    }
}

当我尝试在服务器上运行它时,我会收到此错误:

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /spring-mvc-demo/showForm

我是 SpringMVC 的新手,我认为它与组件扫描有关,但我在 xml 文件中添加了组件扫描。

谢谢。

【问题讨论】:

    标签: java xml spring spring-mvc


    【解决方案1】:

    试试这个:

    @RequestMapping("/spring-mvc-demo")
    public class HelloWorldController {
    
        // need a controller method to show the initial HTML form
        @RequestMapping("/showForm")
        public String showForm() {
            return "helloworld-form";
        }
    }
    

    使用您当前的设置,您需要点击GET /showForm 然后它才能工作。

    请同时查看其他映射方式,例如GetMappingPostMappingPutMappingDeleteMappingPatchMapping

    【讨论】:

    • 感谢回答伙伴,我已经通过在 Eclipse 中将原始 JRE 库更改为 JRE 系统库来修复它。
    【解决方案2】:

    在eclipse中将原来的JRE库改成JRE系统库

    【讨论】:

      猜你喜欢
      • 2017-05-05
      • 2016-02-13
      • 1970-01-01
      • 2013-10-09
      • 2016-02-09
      • 2015-04-14
      • 1970-01-01
      • 2019-06-17
      • 2017-11-24
      相关资源
      最近更新 更多