我正在使用 spring mvc 做一个小演示,但在部署应用程序时遇到问题。我使用的是 Wildfly 10.0.0
不明白发生了什么
在我的 web.xml
<web-app 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"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>simu-cmac</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>simu-cmac</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
在我的 app-config.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-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
<context:component-scan base-package="com.cmac.simu" />
<mvc:annotation-driven/>
还有我的 Controller
@Controller
public class SimuAPIController {
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping("/home.htm")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("Returning home view");
System.out.println("To home view");
return new ModelAndView("home.jsp");
}
}
部署应用程序时,控制台上会显示以下错误
19:50:39,417 WARN [org.springframework.web.servlet.PageNotFound] (default task-3) No mapping found for HTTP request with URI [/simu-cmac/home.htm] in DispatcherServlet with name 'simu-cmac'
最佳答案
似乎您的 Controller 没有被 Spring 应用程序扫描。正如您所定义的:
<context:component-scan base-package="com.cmac.simu" />
因此您的 Controller 必须位于 com.cmac.simu
包内。所以 spring config 可以扫描你的 Controller 。
关于java - 找不到 HTTP Spring MVC 的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46626611/