我刚刚开始开发一个博客应用程序。这是我的第一个使用 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
@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,我仍然会遇到相同的错误。
知道什么可能导致这种情况吗?
最佳答案
我认为你的网址模式没问题(“/”),但我发现 spring-servlet.xml 文件中缺少的一件事是在下面的行
<context:component-scan base-package="your-base-package-path" />
我希望在 xml 文件中添加上述行后,spring mvc 将能够扫描 Controller 和服务。
关于java - 未找到 HTTP 请求与 URI 的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893745/