java 尝试设置 spring servlet 时出现“警告 org.springframework.web.servlet.PageNotFound - 未找到带有 URI 的 HTTP 请求的映射...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28364163/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:27:14  来源:igfitidea点击:

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

javaspringjspspring-mvcservlets

提问by OneTwo

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

我正在尝试设置一个 Spring MVC 项目。我添加了一个调度程序 servlet、一个 jsp 并设置了 web.xml 文件。但我不断得到

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'

警告 org.springframework.web.servlet.PageNotFound - 在 DispatcherServlet 中找不到名称为“HelloWeb”的带有 URI [/safesite/WEB-INF/jsp/hello.jsp] 的 HTTP 请求的映射

Here's my web.xml

这是我的 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

这是我的 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

我的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

还有我的 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.

我无法弄清楚这一点,因此任何帮助将不胜感激。

回答by Master Slave

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

你似乎错过了 <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)

请注意,我已经从 xsd 文件中删除了版本,这意味着它将使用您的 jar 文件中的架构(并且在不兼容的情况下会出现验证错误)

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

在@Nikolay 发表评论之后,我还注意到您的映射中有一个错误(请注意,您仍然需要注释驱动的元素),您应该将控制器中的映射更改为

@RequestMapping("/hello.jsp")

and access it via

并通过访问它

/safesite/hello.jsp

OR, more common, change the servlet mapping to

或者,更常见的是,将 servlet 映射更改为

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

and access as Nikolay said so /safesite/hello

并像尼古拉所说的那样访问 /safesite/hello

回答by Simon L

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/"

我之前也遇到过类似的问题,很困惑,经过一系列测试,我发现这是 DispatcherServlet 的 url-pattern。当您使用星号进行通配符匹配时要小心,这可能会导致意外行为,从根“/”或您的自定义 servlet 上下文路径“/foo/”开始是安全的

Try the following.

请尝试以下操作。

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

回答by Alon Asulin

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

当我从 Spring 框架 3.X 迁移/更新到 4.X 并使用 json(通过 Hymanson2)作为返回对象并得到上述错误时,我遇到了该错误。

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.MappingHymanson2JsonView">
    <property name="contentType" value="application/json"/>
</bean>

To XXX-servlet.xml file

到 XXX-servlet.xml 文件

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

在代码中,您应该使用 MappingHymanson2JsonView 作为返回值,例如:

@RequestMapping("/user")
MappingHymanson2JsonView user() {
MappingHymanson2JsonView view = new MappingHymanson2JsonView();
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.MappingHymanson2JsonView

更多示例请看这里:http: //www.programcreek.com/java-api-examples/index.php?api=org.springframework.web.servlet.view.json.MappingHymanson2JsonView