0

我正在使用 SpringSource 工具套件,我从模板创建了一个 Spring MVC 项目,我很难理解为什么当我在项目中单击“运行为...”时它工作正常,但是当我单击它时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
4

1 回答 1

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

希望这个回答对你有所帮助,欢迎评论。

于 2012-05-17T20:20:33.683 回答