我在设置 Spring MVC +tiles 项目时遇到了非常常见的问题
我收到“在名称为 URI [//home.htm] 的 DispatcherServlet 中找不到 HTTP 请求的映射
我的 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"
id="WebApp_ID"
version="3.0"
>
<servlet>
<servlet-name><PROJECT_NAME></servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name><PROJECT_NAME></servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/<PROJECT_NAME>-servlet.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>
home.htm
</welcome-file>
</welcome-file-list>
</web-app>
我的图 block xml 是
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="baseLayout" template="/WEB-INF/jsp/layout.jsp">
<put-attribute name="title" value="Sample Title" />
<put-attribute name="header" value="/WEB-INF/jsp/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/jsp/menu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/jsp/footer.jsp" />
</definition>
<definition name="home" extends="baseLayout">
<put-attribute name="title" value="Home" />
<put-attribute name="body" value="/WEB-INF/jsp/home.jsp" />
</definition>
<definition name="page" extends="baseLayout">
<put-attribute name="title" value="Page" />
<put-attribute name="body" value="/WEB-INF/jsp/page.jsp" />
</definition>
</tiles-definitions>
project_name-servlet.xml 是
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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"
>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
<bean
id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"
>
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
application-context.xml 是
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<context:component-scan base-package="com.project" />
<context:annotation-config />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.context.request.async.StandardServletAsyncWebRequest"/>
</beans>
homecontroller.java 是
package com.project.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HomeController {
@RequestMapping("/home.htm")
public String myhome() {
System.out.println("INSIDE MY HOME");
return "home";
}
@RequestMapping("/page.htm")
public String page(@RequestParam(value="pageNo") String pageNo,HttpServletRequest request) {
System.out.println("PageNo: " + pageNo);
request.setAttribute("pageNo", pageNo);
return "page";
}
}
我在网上找到了很多解决方案,但没有任何效果。我尝试过以下操作:
- 还检查了服务器部署目录,一切都部署正常。
- 在 web.xml 中,在 servlet url 模式中,我尝试过/,/,.htm
- 在 Controller 中,在请求映射中我尝试了/home.htm、/project_name/home.htm、/
- 我尝试在 application-context.xml 中为不同的 spring 类添加 bean。
但是没有任何效果。
我正在点击http://localhost:8080/PROJECT-NAME/home.htm
最佳答案
摆脱这个
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.context.request.async.StandardServletAsyncWebRequest"/>
在您的应用程序上下文文件中,并通过适当的组件扫描将它们放入您的 servlet 上下文文件中。
<小时/>在一种相关注释中,不要使用DefaultAnnotationHandlerMapping
。 mvc:annotation-driven
已经注册了一个适当的 HandlerMapping
,一个 RequestMappingHandlerMapping
。
关于java - Spring MVC 中的部署失败,并且未找到带有 URI 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120150/