Java 无法在 spring web mvc 中找到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21131319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to find controller in spring web mvc
提问by Ritesh
It seems that dispatcher-servlet unable to perform component scan using.
似乎 dispatcher-servlet 无法使用执行组件扫描。
<context:component-scan base-package="abc" />
In my controller file (HelloController.java) under package abc. Code is written as follows:
在包abc下的我的控制器文件 ( HelloController.java) 中。代码写成如下:
@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/
}
}
My Application name is SpringMiddle. When try url as:
我的应用程序名称是SpringMiddle。当尝试 url 为:
http://localhost:8080/SpringMiddle/hello.htm
I do have following url pattern in web.xml
我确实在 web.xml 中有以下 url 模式
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
It shows me error HTTP 404 not found.
它向我显示错误 HTTP 404 not found。
EDIT:: it shows me warning
编辑::它向我显示警告
WARNING: No mapping found for HTTP request with URI [/SpringMiddle/hello.htm] in DispatcherServlet with name 'dispatcher'
采纳答案by Jakub Kubrynski
You have to enable MVC in Spring. In xml config you can do it in such way:
您必须在 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>
and in JavaConfig:
在 JavaConfig 中:
@Configuration
@EnableWebMvc
public class WebConfig {
}
Please refer to Spring documentation
请参考Spring文档
回答by mel3kings
if you are referring to an mkyong tutorial, I didn't use the annotations: @Configuration @EnableWebMvc, problem is you are using both annotations and xml declaration.
如果您指的是 mkyong 教程,我没有使用注释:@Configuration @EnableWebMvc,问题是您同时使用了注释和 xml 声明。
Annotation for setting the url:
设置url的注解:
@Controller
@RequestMapping("/hello")
you should remove this part:
你应该删除这部分:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
and you'll be able to visit the url:
您将能够访问以下网址:
http://localhost:8080/SpringMiddle/hello
Also make sure you have this in your web.xml:
还要确保你的 web.xml 中有这个:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
the dispatcher-servlet.xml is where the component scanning is declared
dispatcher-servlet.xml 是声明组件扫描的地方