2012-05-15 48 views
0

我正在使用SpringSource工具套件,我從模板創建了一個Spring MVC項目,我很難理解爲什麼當我點擊「run as ...」項目它確實工作正常,但是當我在WEB-INF/views目錄內的home.jsp文件中單擊它時,我得到一個404錯誤,因此我也看不到其他jsp。這是正常行爲嗎?如果是這樣,你能解釋還是指出我的解釋?在默認的STS模板的Spring MVC中的錯誤404

這就是我在控制檯中得到的。

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.s[email protected]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.s[email protected]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.s[email protected]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 
+0

您是否在控制檯中收到任何消息? –

+0

控制檯中沒有錯誤。 你能展示一些你的問題代碼,以便我能更好地理解你的問題並解決這個問題嗎?:) –

回答

3

一般而言,像home.jsp這樣的靜態文件是通過控制器呈現的。

在這種情況下,你的控制器被註解驅動,由位於WEB-INF /春/ appServlet/servlet的context.xml中的

<annotation-driven /> 

XML元素指定的(假設你沒有移動我只是從模板中創建了一個示例mvc項目)

這是什麼意思是春天掃描您的java文件的@Controller對象,然後它將採取和綁定到視圖。

home.jsp無法呈現的原因是因爲它通過位於基本包中的HomeController呈現。

你應該注意到有具有以下簽名的註釋

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

這是捆綁應用程序的根上下文(無論項目名稱爲),這個註釋下面的方法,這應該是(即本地主機:8080/your_app /)

public String home(Locale locale, Model model) 

你應該注意的是字符串的這個控制器方法的返回類型。這表示將在方法完成後呈現的視圖名稱。在這種情況下,它應該是:

return "home"; 

接下來的部分可以是一個有點混亂,但經過一段時間應該是有意義的。 「家」是依賴於一個ViewResolver,如果你回到你的servlet-context.xml中,應該有帶班

org.springframework.web.servlet.view.InternalResourceViewResolver 

注意的屬性被設置一個bean:

<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

希望這個答案可以幫助你一點,隨時發表評論。

+0

這正是我需要的,它確實幫了我很多,非常感謝。 – Hanzel

+1

那麼你能接受答案嗎? – dardo

+0

是的,幫助一個同伴,並接受! – aglassman