2012-01-13 265 views
5

我絕對是Java和Spring的新手,我想從示例中學習。在名爲'appServlet'的DispatcherServlet中找到具有URI [/ myproject /]的HTTP請求的映射

我使用一個不折不扣的現成配置/安裝

  • 的Mac OSX
  • SpringSource工具套件的IDE
  • 春2.8.1.RELEASE
  • 的vFabric-TC -server-developer-2.6.1.RELEASE

我試着生成一個基於「Spring Template Project」的新項目。然後我選擇了「Spring MVC項目」。示例項目已生成。之後,我沒有修改任何東西,我嘗試通過「Run As」執行de「home.jsp」頁面。 Web服務器啓動並最終在控制檯Tab中收到錯誤。

沒有映射在 DispatcherServlet的與名稱找到的HTTP請求與URI [/ myproject的/] 'appServlet'

而在這些網頁該另一輸出:

  • http://localhost:8080/myproject/WEB-INF/views/home.jsp
  • http://localhost:8080/myproject

enter image description here

在這裏你可以看到(用於STS自動生成)對我的項目是如何構成的圖像:

enter image description here

有什麼不對?

在這裏你可以看到的web.xml文件的內容:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

根的context.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <!-- Root Context: defines shared resources visible to all other web components --> 

</beans> 

最後的的servlet-context.xml的有此內容。

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    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.0.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"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="com.mycompany.myapp" /> 

</beans:beans> 

有沒有人有想法解決它?

回答

0

WEB-INF下的所有東西都不能從外部訪問,而MVC框架的重點是在調度到視圖之前調用控制器,所以不應該直接調用JSP。

而你可能沒有任何Spring控制器映射到根URL,所以很明顯,URL http://localhost:8080/myproject/沒有任何內容。

+0

@AlfonsoDominguez @duffymo自動生成的項目有一個名爲「HomeController.java」的文件,內容如下: ' package com.mycompany.myapp; [...] OMITED部分 /** *處理應用程序主頁的請求。 */ @Controller 公共類HomeController的{ \t \t \t/** \t *只需選擇主頁視圖通過返回它的名字來呈現。 \t */ \t @RequestMapping(值= 「/」,方法= RequestMethod.GET) \t公共字符串家(區域設置區域設置,模型模型){ \t \t字符串str = 「服務器消息」; \t \t \t \t model.addAttribute(「serverTime」,str); \t \t \t \t return「home」; \t} \t } ' 是你說什麼? – 2012-01-13 10:48:26

+0

是的,這就是我的意思,你是否嘗試訪問URL'http:// localhost:8080/myproject/home'?該URL似乎是您的配置文件中正確的。 – 2012-02-02 10:59:22

1

Spring的約定是假定網絡中的<servlet-name>。對於DispatcherServlet,xml匹配Spring servlet上下文XML文件的開頭。將servlet-context.xml重命名爲appServlet-context.xml,看看是否有幫助。

+0

試着說我得到了同樣的錯誤。實際上,在web.xml文件中,有一行指定了servlet的名稱......現在我已經更改了你所建議的那個(加上文件的重命名)......但結果是一樣的。 \t \t \t​​contextConfigLocation的 \t \t \t /WEB-INF/spring/appServlet/appServlet-context.xml 2012-01-13 10:35:06

0

將Controller添加到您的應用程序,該應用程序返回名稱爲「home」的ModelAndView實例。然後爲該控制器配置一些處理程序映射,並嘗試使用與此類似的URL訪問您的Web應用程序:http://localhost:8080/myproject/home.do。更多的信息可以在herehere找到。

相關問題