2017-04-15 128 views
2

我新來的Spring MVC,想盡各種例子,但沒有奏效 我用NetBEan IDE 8.2 春4.0.1 的Java 1.8和 的Apache Tomcat 8.0.27Spring MVC的4.0.1請求映射不起作用

這裏我的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
</web-app> 

我Dispath-servelet.xml

<?xml version='1.0' encoding='UTF-8' ?> 
    <!-- was: <?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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 


    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.controllers" /> 


    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp" /> 
    </bean> 


</beans> 

和控制器

package com.controllers.admin; 

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

@Controller 
@RequestMapping("/admin") 
public class AdminController { 

    public AdminController(){ 
    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String index(){ 

    return "hello"; 
    } 


    @RequestMapping(value = "/dashbord", method = RequestMethod.GET) 
    public String dashbord(){ 

     return "dashbord"; 
    } 
} 

當我瀏覽到http://localhost:8084/PropertySales/admin 它說404所請求的資源不可用。

我檢查服務器日誌,我發現這

WARNING [http-nio-8084-exec-202] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/PropertySales/admin/dashbord] in DispatcherServlet with name 'dispatcher' 

什麼錯在我的代碼。誰能幫

+0

如果你有選擇,我的建議是從[Spring Boot](https://projects.spring.io/spring-boot/)開始,然後你不需要XML。 –

+0

看到我的答案我認爲這將解決它。此外,這比春季啓動更好 - 你會學到很多 – strash

回答

0

在你web.xml,你沒有提供的dispath-servelet.xml作爲contextConfigLocation,所以將其添加如下圖所示:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/dispath-servelet.xml</param-value> 
</context-param> 
+0

如果沒有找到上下文配置文件,Spring會提前投訴。我認爲問題與此不同。 – ProgrammerBoy

0

我想你應該爲你改變這些添加到您的調度員的servlet名稱:

<servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/servlet-properties.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
+0

如果沒有找到上下文配置文件,Spring會提前投訴。我認爲問題與此不同。 – ProgrammerBoy

+0

感謝您的答案..但它帶來了另一個錯誤 「無法打開ServletContext資源」 –

0

我找到解決辦法我自己

問題是「的applicationContext.xml」文件 沒有把在「調度 - servlet.xml中」哪些配置,應該包括在「applicationContext.xml中」

這裏我的applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.controllers" /> 


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

和調度 - servlet.xml中

<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: <?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" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 


    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.controllers" /> 


    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/views/" /> 
    <property name="suffix" value=".jsp" /> 
    </bean> 


</beans>