Getting “WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI …” when trying to setup spring servlet

匿名 (未验证) 提交于 2019-12-03 02:34:02

问题:

I am trying to setup a Spring MVC project. I have added a dispatcher servlet, a jsp and setup the web.xml file. But I keep getting

WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/safesite/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'HelloWeb'

Here's my web.xml

... <context-param>     <description>Vaadin production mode</description>     <param-name>productionMode</param-name>     <param-value>true</param-value> </context-param>  <!-- To load the Spring context --> <listener>     <listener-class>org.activiti.explorer.servlet.WebConfigurer</listener-class> </listener>  <!-- To allow session-scoped beans in Spring --> <listener>     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>  <filter>     <filter-name>UIFilter</filter-name>     <filter-class>org.activiti.explorer.filter.ExplorerFilter</filter-class> </filter>  <filter>     <filter-name>JSONPFilter</filter-name>     <filter-class>org.activiti.explorer.servlet.JsonpCallbackFilter</filter-class> </filter>  <filter-mapping>     <filter-name>UIFilter</filter-name>     <url-pattern>/o/*</url-pattern> </filter-mapping>  <filter-mapping>     <filter-name>JSONPFilter</filter-name>     <url-pattern>/service/*</url-pattern> </filter-mapping>  <servlet>     <servlet-name>Vaadin Application Servlet</servlet-name>     <servlet-class>org.activiti.explorer.servlet.ExplorerApplicationServlet</servlet-class>     <init-param>         <param-name>widgetset</param-name>         <param-value>org.activiti.explorer.CustomWidgetset</param-value>     </init-param> </servlet>  <servlet-mapping>     <servlet-name>Vaadin Application Servlet</servlet-name>     <url-pattern>/ui/*</url-pattern> </servlet-mapping>  <servlet-mapping>     <servlet-name>Vaadin Application Servlet</servlet-name>     <url-pattern>/VAADIN/*</url-pattern> </servlet-mapping>  <servlet>     <servlet-name>HelloWeb</servlet-name>     <servlet-class>         org.springframework.web.servlet.DispatcherServlet     </servlet-class>     <load-on-startup>1</load-on-startup> </servlet>  <servlet-mapping>     <servlet-name>HelloWeb</servlet-name>     <url-pattern>*.jsp</url-pattern> </servlet-mapping>  <!-- Session timeout on one day --> <session-config>     <session-timeout>480</session-timeout> </session-config> 

And here is my HelloWeb-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    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-3.0.xsd    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  <context:component-scan base-package="org.activiti.explorer.controller" />  <bean id="viewResolver"           class="org.springframework.web.servlet.view.UrlBasedViewResolver">     <property name="viewClass"       value="org.springframework.web.servlet.view.JstlView" />     <property name="prefix" value="/WEB-INF/jsp/" />     <property name="suffix" value=".jsp" /> </bean> 

My HelloController

 package org.activiti.explorer.controller;   import org.springframework.stereotype.Controller;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.servlet.ModelAndView;   /**   *   * @author Fionn   */  @Controller  public class HelloController {  @RequestMapping("/hello") public ModelAndView helloWorld() {           String message = "<br><div align='center'>"                  + "<h3>********** Hello World, Spring MVC Tutorial</h3>This message is comming from CrunchifyHelloWorld.java **********<br><br>";          return new ModelAndView("hello", "message", message);      } } 

And my hello.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"     %> <%@page trimDirectiveWhitespaces="true" %> <!DOCTYPE html> <html> <head> <title>Hello Spring MVC</title> </head> <body> 

I can't figure this one out so any help would be greatly appreciated.

回答1:

You seem to be missing the <mvc:annotation-driven />

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"        xmlns:context="http://www.springframework.org/schema/context"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        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.xsd         http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc.xsd">      <!-- Enables the Spring MVC @Controller programming model -->     <mvc:annotation-driven />      <context:component-scan base-package="org.activiti.explorer.controller" />      <bean id="viewResolver"           class="org.springframework.web.servlet.view.UrlBasedViewResolver">         <property name="viewClass"                   value="org.springframework.web.servlet.view.JstlView" />         <property name="prefix" value="/WEB-INF/jsp/" />         <property name="suffix" value=".jsp" />     </bean> </beans> 

note that I've removed the version from the xsd files, this means that it will use the schema from your jar files (and there will be a validation error in case of incompatibilty)

after @Nikolay's comment, I've also noticed an error in your mapping (note that you still need the annotation-driven element), you should either change the mapping in your controller to

@RequestMapping("/hello.jsp") 

and access it via

/safesite/hello.jsp 

OR, more common, change the servlet mapping to

<servlet-mapping>     <servlet-name>HelloWeb</servlet-name>     <url-pattern>/</url-pattern> </servlet-mapping> 

and access as Nikolay said so /safesite/hello



回答2:

I had a similar issue before which was very confusing, after a series of tests, I found it's the url-pattern for the DispatcherServlet. Be cautious when you use the asterisk for wildcard match which might lead to unexpected behavior, and it's just safe to start from root "/" or your custom servlet context path "/foo/"

Try the following.

<servlet-mapping>   <servlet-name>HelloWeb</servlet-name>   <url-pattern>/</url-pattern> </servlet-mapping>


回答3:

I got that error when i migrated/updated from Spring framework 3.X to 4.X and using json (through Jackson2) as returned object and got the error above.

To solve that you should add

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">     <property name="messageConverters">         <list>             <bean class="org.springframework.http.converter.StringHttpMessageConverter">                 <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8"/>             </bean>         </list>     </property> </bean>  <bean  class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">     <property name="mediaTypes">         <map>            <entry key="json" value="application/json"/>        </map>     </property> </bean>  <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">     <property name="contentType" value="application/json"/> </bean> 

To XXX-servlet.xml file

And on the code you should use MappingJackson2JsonView as return value for example :

@RequestMapping("/user") MappingJackson2JsonView user() { MappingJackson2JsonView view = new MappingJackson2JsonView(); view.setExtractValueFromSingleKeyModel(true); view.addStaticAttribute("user", new User("Sébastien", "Deleuze")); view.setPrefixJson(true); return view; } 

for more examples look here: http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.web.servlet.view.json.MappingJackson2JsonView



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!