簡體   English   中英

Spring MVC和圖塊中的部署失敗,未找到帶有URI的HTTP請求的映射

[英]Deployment failing in Spring MVC and tiles with No mapping found for HTTP request with URI

在設置Spring MVC + Tiles項目時,我面臨一個非常普遍的問題

我收到“在DispatcherServlet中名稱為URI [//home.htm]的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>

我的圖塊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";    
     }    
}  

我在互聯網上找到了很多解決方案,但沒有任何效果。 我嘗試了以下操作:

  1. 還要檢查服務器部署目錄,一切都部署正確。
  2. 在web.xml中servlet的URL模式,我已經試過/,/,的.htm
  3. 在控制器中,在請求映射中,我嘗試了/home.htm、/project_name/home.htm、/
  4. 我嘗試將application-context.xml中的bean用於不同的spring類。

但是沒有任何效果。

我正在點擊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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM