我绝对是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