使用默认STS模板的Spring MVC中的错误404

时间:2012-05-15 20:36:48

标签: spring-mvc http-status-code-404

我正在使用SpringSource Tool Suite,我从模板创建了一个Spring MVC项目,我很难理解为什么当我在项目中点击“run as ...”它确实工作正常,但是当我在WEB-INF / views目录下的home.jsp文件中单击它我得到一个404错误,因为这个我也看不到其他的jsp。这是正常的行为吗?如果是的话,你能解释或指出我的解释吗?

这是我在控制台中获得的内容。

15/05/2012 02:43:37 PM com.springsource.tcserver.security.PropertyDecoder <init>
INFO: tc Runtime property decoder using memory-based key
15/05/2012 02:43:37 PM com.springsource.tcserver.security.PropertyDecoder <init>
INFO: tcServer Runtime property decoder has been initialized in 181 ms
15/05/2012 02:43:37 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-1234"]
15/05/2012 02:43:38 PM com.springsource.tcserver.serviceability.rmi.JmxSocketListener init
INFO: Started up JMX registry on 127.0.0.1:6969 in 75 ms
15/05/2012 02:43:38 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 651 ms
15/05/2012 02:43:38 PM org.apache.catalina.core.StandardService startInternal
INFO: Arrancando servicio Catalina
15/05/2012 02:43:38 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: VMware vFabric tc Runtime 2.6.4.RELEASE/7.0.25.B.RELEASE
15/05/2012 02:43:38 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Desplieque del descriptor de configuración C:\springsource32\vfabric-tc-server-developer-2.6.4.RELEASE\spring-insight-instance\conf\Catalina\localhost\HelloWorld.xml
15/05/2012 02:43:38 PM org.apache.catalina.startup.SetContextPropertiesRule begin
ADVERTENCIA: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWorld' did not find a matching property.
15/05/2012 02:43:38 PM com.springsource.insight.collection.tcserver.ltw.TomcatWeavingInsightClassLoader start
INFO: Context [localhost|HelloWorld] will not be woven
15/05/2012 02:43:39 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Tue May 15 14:43:39 MDT 2012]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root-context.xml]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a29c6e: defining beans []; root of factory hierarchy
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 596 ms
15/05/2012 02:43:39 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'appServlet'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Tue May 15 14:43:39 MDT 2012]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9b2a51: defining beans [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,trampsController,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@a29c6e
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/tramps],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.hanzellegarda.HelloWorld.controllers.TrampsController.list(org.springframework.ui.Model)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.hanzellegarda.HelloWorld.HomeController.home(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 802 ms
15/05/2012 02:43:40 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Desplieque del descriptor de configuración C:\springsource32\vfabric-tc-server-developer-2.6.4.RELEASE\spring-insight-instance\conf\Catalina\localhost\insight.xml
15/05/2012 02:43:40 PM com.springsource.insight.collection.tcserver.ltw.TomcatWeavingInsightClassLoader start
INFO: Context [localhost|insight] will not be woven
15/05/2012 02:43:40 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
15/05/2012 02:43:56 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'Spring MVC Dispatcher Servlet'
15/05/2012 02:44:00 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Despliegue del directorio C:\springsource32\vfabric-tc-server-developer-2.6.4.RELEASE\spring-insight-instance\webapps\manager de la aplicación web
15/05/2012 02:44:00 PM com.springsource.insight.collection.tcserver.ltw.TomcatWeavingInsightClassLoader start
INFO: Context [localhost|manager] will not be woven
15/05/2012 02:44:01 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Despliegue del directorio C:\springsource32\vfabric-tc-server-developer-2.6.4.RELEASE\spring-insight-instance\webapps\ROOT de la aplicación web
15/05/2012 02:44:01 PM com.springsource.insight.collection.tcserver.ltw.TomcatWeavingInsightClassLoader start
INFO: Context [localhost|ROOT] will not be woven
15/05/2012 02:44:01 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-1234"]
15/05/2012 02:44:01 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 23080 ms

1 个答案:

答案 0 :(得分:3)

一般来说,home.jsp等静态文件是通过控制器呈现的。

在这种情况下,您的控制器正在进行注释驱动,由

指定
<annotation-driven />

位于WEB-INF / spring / appServlet / servlet-context.xml中的XML元素(假设你没有移动东西,我只是从模板中制作了一个示例mvc项目)

这意味着Spring会扫描你的java文件中的@Controller对象,然后它会占用并绑定到视图。

home.jsp无法渲染的原因是因为它是通过位于基础包中的HomeController呈现的。

您应该注意到有一个带有以下签名的注释

@RequestMapping(value = "/", method = RequestMethod.GET)

这将应用程序的根上下文(无论项目名称是什么)绑定到此注释下面的方法,该方法应该是(即localhost:8080 / your_app /)

public String home(Locale locale, Model model)

您应该注意的是此控制器方法的String的返回类型。这表示将在完成方法后呈现的视图名称。在这种情况下,它应该是:

return "home";

下一部分可能有点令人困惑,但一段时间后应该有意义。 “home”绑定到ViewResolver,如果你回到你的servlet-context.xml,应该有一个带有类的bean

org.springframework.web.servlet.view.InternalResourceViewResolver

请注意正在设置的属性:

<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />

这意味着视图解析器将开始在/ WEB-INF / views /中查找“home”视图文件,并将.jsp附加到home的末尾。它将找到它找到的第一个匹配项,如果您查看目录结构,则会在其中找到一个jsp文件:

  

/WEB-INF/views/home.jsp

希望这个答案对你有所帮助,随时发表评论。