没有找到带有URI的HTTP请求的映射

时间:2014-11-12 18:16:20

标签: java spring thymeleaf

我刚刚开始开发博客应用。这是我的第一个使用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>

弹簧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 with one form。如果我注释掉表格并留下你好的世界,我仍然会得到同样的错误。

知道可能导致这种情况的原因吗?

2 个答案:

答案 0 :(得分:1)

我认为您的网址格式正常(“/”),但我在 spring-servlet.xml 文件中看到的一件事就是在线下

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

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

答案 1 :(得分:0)

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

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