spring org.springframework.web.servlet.DispatcherServlet noHandlerFound (未找到映射)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11241292/
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
org.springframework.web.servlet.DispatcherServlet noHandlerFound (No mapping found)
提问by Mahmoud Saleh
my jsps are under WEB-INF/jsp/ , and following is my web.xml:
我的 jsps 在 WEB-INF/jsp/ 下,以下是我的 web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Checkout</display-name>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
and here's mapping of page product.jsp which i am trying to access:
这是我试图访问的页面 product.jsp 的映射:
@Controller
@RequestMapping("/product.action")
public class ProductController {
/**
* Show the product selection form
*
* @return
*/
@RequestMapping(method=RequestMethod.GET)
public String get() {
return "products.jsp";
}
}
when trying to access the page from the following link:
尝试从以下链接访问页面时:
http://localhost:8080/myapp/product.action
i am getting 404in the browser, and i get the following warning in the console:
我进入404浏览器,在控制台中收到以下警告:
Jun 28, 2012 10:55:23 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/myapp/product.action] in DispatcherServlet with name 'myservlet'
am i missing something in the configuration ? please advise, thanks.
我在配置中遗漏了什么吗?请指教,谢谢。
EDIT: i tried adding the viewResolver bean to applicationContext with no luck:
编辑:我尝试将 viewResolver bean 添加到 applicationContext ,但没有运气:
<?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"
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-3.1.xsd">
<context:component-scan base-package="com.myapp"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
采纳答案by Mahmoud Saleh
problem was that the controller wasn't detected.
i changed the base-package from com.myappto com.myapp.controller, and it works fine now.
问题是没有检测到控制器。我将基础包从 更改com.myapp为com.myapp.controller,现在工作正常。
回答by Pradeep Kr Kaushal
Follow the rule whatever Sunilhas mentioned. I see one issue in your spring configuration xml that you don't have the
遵循Sunil提到的任何规则。我在您的 spring 配置 xml 中看到一个问题,您没有
<mvc:annotation-driven />
You need this to register the Controlleralong with
您需要使用该注册Controller沿
<context:component-scan base-package="com.myapp"/>
<context:component-scan base-package="com.myapp"/>
回答by Sunil Chavan
When you specify the RequestMapping, URI should not have extension. Dispatcher servlet will omit it from the request URI while searching for URI mapping.
当您指定 RequestMapping 时,URI 不应具有扩展名。Dispatcher servlet 将在搜索 URI 映射时从请求 URI 中省略它。
Use @RequestMapping("/product")
it should work.
使用@RequestMapping("/product")
它应该可以工作。
Second thing when you are using view resolver just return the name of the JSP file. Do not append .jsp extension, InternalViewResolver will do it for you.
当您使用视图解析器时,第二件事只需返回 JSP 文件的名称。不要附加 .jsp 扩展名,InternalViewResolver 会为你做。
回答by ASIK RAJA A
Use this class="org.springframework.web.servlet.view.UrlBasedViewResolver"instead of class="org.springframework.web.servlet.view.InternalResourceViewResolver"
使用这个 class="org.springframework.web.servlet.view.UrlBasedViewResolver"而不是 class="org.springframework.web.servlet.view.InternalResourceViewResolver"
in your application context bean.
在您的应用程序上下文 bean 中。
回答by user2542057
?f you use viewResolver in context xml , you should change get method return statemant to "products" and be sure the folder hierarchy is right
?如果您在上下文 xml 中使用 viewResolver,您应该将 get 方法返回语句更改为“products”并确保文件夹层次结构正确
回答by Malatesh K R
Check the package name spring-servlet.xml , if it is correctly spelled or not. That might be the issue. I faced while starting the Spring MVC
检查包名 spring-servlet.xml ,是否拼写正确。这可能是问题所在。我在启动 Spring MVC 时遇到了

