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

Rit*_*esh 2 java spring-mvc url-routing

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

 <context:component-scan  base-package="abc" />
Run Code Online (Sandbox Code Playgroud)

在我的控制器文件(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/
 }
}
Run Code Online (Sandbox Code Playgroud)

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

http://localhost:8080/SpringMiddle/hello.htm
Run Code Online (Sandbox Code Playgroud)

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

 <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

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

编辑::它告诉我警告

WARNING:   No mapping found for HTTP request with URI [/SpringMiddle/hello.htm] in DispatcherServlet with name 'dispatcher'
Run Code Online (Sandbox Code Playgroud)

Jak*_*ski 6

你必须在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>
Run Code Online (Sandbox Code Playgroud)

在JavaConfig中:

@Configuration
@EnableWebMvc
public class WebConfig {

}
Run Code Online (Sandbox Code Playgroud)

请参阅Spring文档