2015-12-21 35 views
0

我的web.xml:面對的問題與org.springframework.web.servlet.DispatcherServlet noHandlerFound

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>hello</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>hello</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/allconfig.xml</param-value> 
    </context-param> 
</web-app> 

我的HELLO-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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 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"> 

    <mvc:annotation-driven></mvc:annotation-driven> 
    <context:component-scan base-package="com.test.controller"/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/Jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 

allconfig.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:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc"  xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <!-- <bean id="user" class="com.test.User"/>  --> 
    <mvc:annotation-driven/> 
    <context:component-scan base-package="com.test"/> 
</beans> 

TestController

package com.test.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

import com.test.model.User; 

@Controller 
public class TestController { 
    @RequestMapping(value="/home",method=RequestMethod.GET) 
    public ModelAndView getHomePage(User user){ 

     return new ModelAndView("home", "user", user); 
    } 
    @RequestMapping(value="/processrequest",method=RequestMethod.POST) 
    public void processRequest(User user){ 
     System.out.println("request processed successfully"); 
    } 
} 

回到Home.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
<form action = "/TestController/processrequest" method="post"> 
    first name : <input type="text" name="firstName"/> 
    last name : <input type="text" name="lastName"/> 
    <input type="submit" value="submit"/> 
</form> 
</body> 
</html> 

請幫我解決這個問題:

2015年12月21日下午12時39分38秒org.springframework.web.servlet.DispatcherServlet noHandlerFound 警告:無使用名稱爲'hello'的DispatcherServlet中的URI [/SpringTest/home.jsp]爲HTTP請求找到的映射

回答

0

您正在點擊不存在映射的URL。

因此,將映射更改爲@RequestMapping(value="/home*",method=RequestMethod.GET)
或嘗試僅打/SpringTest/home

+0

我只使用這個URL(http:// localhost:8080/SpringTest/home)來測試代碼。並嘗試在家後添加*。沒有選擇的作品。並且還將url映射更改爲/並且對我無效。 – Monika

0

刪除allConfig.xml並刪除下面的配置在web.xml

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/allconfig.xml</param-value> 
    </context-param> 

該框架將自動查找爲hello-servlet.xml,你不需要額外的配置文件allConfig.xml

更改hello dispatcher servlet映射從/*/

<servlet-mapping> 
    <servlet-name>hello</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 

請確保您在路徑WEB-INF/JSP/home.jsp和 中有home.jsp,然後當您點擊網址localhost:8080/SpringTest/home時,將返回home.jsp

現在home.jsp改變你的表單動作action = "./processrequest"

<form action = "./processrequest" method="post"> 
    first name : <input type="text" name="firstName"/> 
    last name : <input type="text" name="lastName"/> 
    <input type="submit" value="submit"/> 
</form> 

當您提交表單,您的控制器方法被調用,而聲明System.out.println("request processed successfully");將被執行,你可以看到在控制檯一樣。但是你的頁面仍然顯示404錯誤,因爲你沒有從控制器返回任何東西。因此,請確保您的控制器方法返回一個jsp頁面,以便404錯誤也將得到解決。

相關問題