【问题标题】:No mapping found for HTP request with URI未找到带有 URI 的 HTTP 请求的映射
【发布时间】:2014-11-12 18:16:20
【问题描述】:

我刚开始开发博客应用程序。这是我第一个使用 Spring、Thymeleaf 和 Hibernate 的应用程序。我显示了我的索引页面,然后我摆弄了 Hibernate 设置和持久层,突然我的应用程序无法加载。

我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <welcome-file-list>
        <welcome-file>index</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <mvc:annotation-driven />

    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".html"/>
        <property name="templateMode" value="HTML5"/>
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver"/>
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine"/>
        <property name="order" value="1"/>
    </bean>
</beans>

和我的控制器

@Controller
public class BlogController {

    @Autowired
    private BlogPostManager blogPostManager;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showPosts(Model model){
        model.addAttribute("postList", blogPostManager.getAllBlogPosts());
        model.addAttribute("blogPost", new BlogPost());
        return "index";
    }

    @RequestMapping(value = "/addpost", method = RequestMethod.POST)
    public String addPost(@ModelAttribute(value = "blogPost")BlogPost blogPost){
        blogPostManager.addBlogPost(blogPost);
        return "index";
    }

}

当我尝试访问我的应用程序(部署到 tomcat8 并打包为 war)时,我得到标准的 404 tomcat 页面,并且 tomcat 控制台打印以下警告:

WARNING [http-nio-8080-exec-108]
org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP
request with URI [/blog%2D1.0/] in DispatcherServlet with name 'spring'

如果需要,我可以发布我的 index.html,但这是一个简单的 hello world,只有一个表单。如果我注释掉表单并只留下 hello world,我仍然会收到同样的错误。

知道是什么原因造成的吗?

【问题讨论】:

  • 您需要 servlet 的映射,就像它说的那样?您希望哪个映射将该 servlet 映射到 url 路径?

标签: java spring thymeleaf


【解决方案1】:

我认为您的 url 模式没问题(“/”),但我在 spring-servlet.xml 文件中看到的一件事在下面一行

<context:component-scan base-package="your-base-package-path" />

我希望在您的 xml 文件中添加上述行后,spring mvc 将能够扫描控制器和服务。

【讨论】:

  • welp,查看 git 历史记录,出于某种原因,我删除了这一行。干杯。
【解决方案2】:

我只知道基本的 Servlet,但我会在这里冒昧地说您的 Spring servlet 映射是错误的。它应该是/* 而不仅仅是/。

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>   <<-- CHANGE THIS
</servlet-mapping>

【讨论】:

  • 我试过了。实际上这里的答案很少,但他们都说你应该使用/而不是/*
猜你喜欢
  • 2017-07-16
  • 1970-01-01
  • 2016-02-05
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多