2012-01-13 63 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找到。

相关问题