我絕對是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
在這裏你可以看到(用於STS自動生成)對我的項目是如何構成的圖像:
有什麼不對?
在這裏你可以看到的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>
有沒有人有想法解決它?
@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
是的,這就是我的意思,你是否嘗試訪問URL'http:// localhost:8080/myproject/home'?該URL似乎是您的配置文件中正確的。 – 2012-02-02 10:59:22