无法在spring web mvc中找到控制器

 只喝大瓶的雪碧 发布于 2023-02-02 18:48

似乎dispatcher-servlet无法使用执行组件扫描.

 

在我的控制器文件(HelloController.java)下包abc.代码编写如下:

@Controller
@RequestMapping("/hello")
public class HelloController {

@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
    model.addAttribute("message", "Hello Spring MVC Framework!");
    return "hello"; //I have already made hello.jsp in web-inf/jsp/
 }
}

我的应用程序名称是SpringMiddle.当尝试url为:

http://localhost:8080/SpringMiddle/hello.htm

我在web.xml中有以下url模式

 
    dispatcher
    *.htm

它显示我找不到HTTP 404错误.

编辑::它告诉我警告

WARNING:   No mapping found for HTTP request with URI [/SpringMiddle/hello.htm] in DispatcherServlet with name 'dispatcher'

Jakub Kubryn.. 6

你必须在Spring中启用MVC.在xml配置中,您可以这样做:




    


在JavaConfig中:

@Configuration
@EnableWebMvc
public class WebConfig {

}

请参阅Spring文档

1 个回答
  • 你必须在Spring中启用MVC.在xml配置中,您可以这样做:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <mvc:annotation-driven />
    
    </beans>
    

    在JavaConfig中:

    @Configuration
    @EnableWebMvc
    public class WebConfig {
    
    }
    

    请参阅Spring文档

    2023-02-02 18:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有