2012-11-19 59 views
2

我試圖設置和學習如何使用Spring MVC開發Web應用程序。我一直在試圖遵循一些教程,但我的應用程序似乎並不想工作。我的Spring MVC配置有什麼問題?

ENVIRO:Eclipse中,M2E-WTP,彈簧,整個事情

上下文根是BidApp。

我有一個控制器:

@Controller 
public class AuctionController {  
    @RequestMapping("/hello") 
    public ModelAndView helloWorld() { 
     String message = "Hello World, Spring 3.0!"; 
     return new ModelAndView("hello", "message", message); 
    } 
} 

我在WEB-INF的web.xml:

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<web-app> 
    <display-name>BidApp</display-name> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-servlet.xml</param-value> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>*.*</url-pattern> 
    </servlet-mapping> 
</web-app> 

而且我爲spring-servlet.xml也在WEB-INF

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    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 --> 

    <!-- Scans within the base package of the application for @Components to configure as beans --> 
    <!-- @Controller, @Service, @Configuration, etc. --> 
    <context:component-scan base-package="com.bidapp.controllers" /> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 
</beans> 

我的我啓動應用程序並導航到http://localhost:8080/BidApp/hello,它給了我一個404。是我的xml文件在錯誤的地方,或者我錯過了一些配置參數?有沒有一個好的地方可以查看所有可用的參數來配置Spring應用程序。春季框架的參考,除非我讀錯了,沒有幫助。

編輯:

Tomcat logs: 
Nov 18, 2012 10:07:35 PM org.apache.catalina.core.AprLifecycleListener init 
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\php\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\MATLAB\R2010b\runtime\win64;C:\Program Files\MATLAB\R2010b\bin;C:\Users\Soto\Desktop\android-sdk-windows\tools;C:\Program Files (x86)\Common Files\Teleca Shared;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseHg\;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\MinGW\bin;C:\Program Files\nodejs\;C:\Ruby193\bin;C:\Program Files (x86)\SSH Communications Security\SSH Secure Shell;C:\Users\Soto\Desktop\android-sdk-windows\tools;C:\MinGW\bin;C:\Users\Soto\AppData\Roaming\npm\;C:\apache-maven-3.0.4\bin;C:\Program Files\Java\jdk1.7.0_02\bin;;. 
Nov 18, 2012 10:07:35 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:BidApp' did not find a matching property. 
Nov 18, 2012 10:07:35 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["http-bio-8080"] 
Nov 18, 2012 10:07:35 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["ajp-bio-8009"] 
Nov 18, 2012 10:07:35 PM org.apache.catalina.startup.Catalina load 
INFO: Initialization processed in 594 ms 
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.StandardService startInternal 
INFO: Starting service Catalina 
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.StandardEngine startInternal 
INFO: Starting Servlet Engine: Apache Tomcat/7.0.22 
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.StandardContext checkUnusualURLPattern 
INFO: Suspicious url pattern: "*.*" in context [/BidApp] - see section SRV.11.2 of the Servlet specification 
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing Spring FrameworkServlet 'dispatcher' 
Nov 18, 2012 10:07:36 PM org.springframework.web.servlet.FrameworkServlet initServletBean 
INFO: FrameworkServlet 'dispatcher': initialization started 
Nov 18, 2012 10:07:36 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Nov 18 22:07:36 EST 2012]; root of context hierarchy 
Nov 18, 2012 10:07:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml] 
Nov 18, 2012 10:07:36 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]6ad89088: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; root of factory hierarchy 
Nov 18, 2012 10:07:37 PM org.springframework.web.servlet.FrameworkServlet initServletBean 
INFO: FrameworkServlet 'dispatcher': initialization completed in 739 ms 
Nov 18, 2012 10:07:37 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-bio-8080"] 
Nov 18, 2012 10:07:37 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["ajp-bio-8009"] 
Nov 18, 2012 10:07:37 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 1395 ms 
+0

服務器啓動過程中是否有錯誤? –

+0

沒有。當我相信它應該時,它並沒有在上面顯示的地址上顯示任何內容。 –

+0

你有任何服務器端日誌記錄?你的控制器的軟件包是什麼? –

回答

2

當您添加<mvc:annotation-driven />到您的配置,您必須使用類似這樣的

<url-pattern>/</url-pattern> 

URL模式,它取代處理器映射和處理程序適配器的默認設置,而這些違約是該處理的OLD-的那些樣式控制器。

我認爲你應該理解component-scan和annotation-config之間的區別。如果你使用掃描,那麼你不需要使用配置。 Difference between <context:annotation-config> vs <context:component-scan>

請不要用你的舊代碼執行與拆卸<mvc:annotation-driven />

(一般)將不mvcdefault servlethandler

+0

/*

+0

@DarrylMiles請考慮答案http://stackoverflow.com/questions/13405868/in-a-servlet-mapping-in-spring-mvc-how-do-i-map如果你已經這麼做了,那麼就考慮自己進行投票了。 – user533

+0

這是問題的一部分,請參閱下面的缺失內容。 –

2

調度器具有一個無效的映射。只接受1個通配符。

因此改變調度匹配網址爲類似/*

所以/*.do/*.htm*.html但不/*.jsp比如Tomcat如果你想休息一下網址已有映射.jsps

,我可能會建議使用UrlRewriteFilter?它的使用非常簡單,可以像使用Apache重寫過濾器一樣使用。

1

更改URL的模式<url-pattern>/</url-pattern>甚至工作是/不是足夠。在我的春天-servlet.xml文件我是缺少一些元素:

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 
<!-- This tag allows for mapping the DispatcherServlet to "/" (all extensions etc)--> 
<mvc:default-servlet-handler/> 


<!-- Enables many annotations and searches for @Controller annotated methods etc.. --> 
<context:annotation-config /> 

一旦這些添加,相應的控制器被發現(基於註解),並在適當的URI開始/執行。

+0

請執行您的舊代碼,刪除 user533

+0

/即使沒有mvcdefault servlethandler – user533

+0

也會正常工作,將其放入您的答案中,我會接受它。 –

相關問題