简体   繁体   English

找不到映射错误Spring MVC

[英]No mapping found error Spring MVC

I made a very simple Spring MVC application to learn AOP, yet each time I try to navigate any of the application pages, I get (No mapping found) error as follows: 我创建了一个非常简单的Spring MVC应用程序来学习AOP,但每次我尝试浏览任何应用程序页面时,都会得到(找不到映射)错误,如下所示:

No mapping found for HTTP request with URI [/TestAOP/page1.htm] in DispatcherServlet with name 'appServlet'

I've doubled checked web.xml, servlet-context.xml, controller code but couldn't notice anything wrong. 我加倍检查了web.xml,servlet-context.xml,控制器代码但是没有注意到任何错误。 So I will appreciate it so much if someone can please look at the content of my web.xml, servlet-context.xml and controller and let me know what I am missing here and how I can overcome this error. 所以如果有人能够查看我的web.xml,servlet-context.xml和控制器的内容,我将非常感激,并让我知道我在这里缺少什么以及如何克服这个错误。 Thanks for your time 谢谢你的时间

  1. Web.xml 在web.xml

      <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 
  2. servlet-context.xml servlet的context.xml中

  <annotation-driven /> <context:component-scan base-package="com.sampledomain.app.controller" /> <aop:aspectj-autoproxy/> <resources mapping="/resources/**" location="/resources/" /> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> 
  1. Controller 调节器

     @Controller public class HomeController { @RequestMapping(value = "/page1", method = RequestMethod.GET) public String firstPage(HttpServletRequest request,Locale locale, Model model) { Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); String formattedDate = dateFormat.format(date); model.addAttribute("serverTime", formattedDate ); return "page01"; } } 
  2. root-context.xml Is mainly empty root-context.xml主要是空的

Note: page01.jsp is under /WEB-INF/views/ folder 注意:page01.jsp位于/ WEB-INF / views /文件夹下

I would suggest to double check whether the Controller is instantiated by spring. 我建议仔细检查控制器是否由spring实例化。 The easiest way is to create an empty constructor with a sysout("my controller has been instantiated!!!") or whatever and lets see whether this line is in the catalina.out. 最简单的方法是创建一个带有sysout的空构造函数(“我的控制器已经实例化!!!”)或者其他什么,让我们看看这行是否在catalina.out中。 Take a look whether 看看是否

<context:component-scan base-package="com.sampledomain.app.controller" />

this is the correct package of your controller. 这是您的控制器的正确包。 If your controller is not instantiated by spring than double check also the spring config paths etc. 如果你的控制器没有被弹簧实例化,那么也要检查弹簧配置路径等。

Try to remove the .htm from the URL. 尝试从URL中删除.htm。 That is: http://[server][:port]/TestAOP/page1. 即:http:// [server] [:port] / TestAOP / page1。

That should match the mapping on your controller. 这应该与您的控制器上的映射匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM