2014-10-21 54 views
0

我試圖用Spring 3.2從頭開始一個新的應用程序,當我嘗試訪問我的控制器時,我得到了404錯誤。下面是我的配置:春天3.2 MVC - 無法匹配網址與控制器

WEB-INF 
    web.xml 
    web-context.xml 

的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>Archetype Created Web Application</display-name> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/application-context.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <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/web-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/deblan/*</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" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.deboralandini.web.controllers"/> 
    <mvc:annotation-driven/> 

    <beans> 
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
      <property name="prefix" value="/WEB-INF/views/"/> 
      <property name="suffix" value=".jsp"/> 
     </bean> 
    </beans> 

</beans> 

我的控制器:

package com.deboralandini.web.controllers; 

    import ...; 

    @Controller 
    @RequestMapping("cuenta") 
    public class CuentaController { 

     @Autowired 
     private CuentaService cuentaService; 

     ... 

     @RequestMapping(value = "/cCuenta") 
     public String cCuenta(Model model){ 
      return "crearCuenta"; 
     } 

     ... 


    } 

我的index.jsp :

<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %> 
<html> 
<body> 
<h2>BlaBla</h2> 
<br/> 
<a href="cuenta/cCuenta">Crear Cuenta</a> 
</body> 
</html> 

不幸的是,當我點擊鏈接,我得到:

Status HTTP 404 - /deblan/cuenta/cCuenta 

瀏覽器嘗試做一個GET到http://localhost:8080/deblan/cuenta/cCuenta,但我不能在我的控制得到。我調試了控制器,實際上並沒有進入那裏,所以:我的新手錯誤是什麼?

在此先感謝 馬科斯

編輯:這是我的輸出:

"C:\...\App\tomcat8\bin\catalina.bat" run 
[2014-10-21 08:57:21,069] Artifact DebLan:war: Server is not connected. Deploy is not available. 
Using CATALINA_BASE: ... 
Connected to the target VM, address: '127.0.0.1:64352', transport: 'socket' 
21-Oct-2014 20:57:21.743 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.14 
21-Oct-2014 20:57:21.745 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Sep 24 2014 09:01:51 
21-Oct-2014 20:57:21.745 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.14.0 
21-Oct-2014 20:57:21.745 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:  Windows 8.1 
21-Oct-2014 20:57:21.745 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:  6.3 
21-Oct-2014 20:57:21.745 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 
21-Oct-2014 20:57:21.745 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.7.0_55-b13 
21-Oct-2014 20:57:21.745 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:  Oracle Corporation 
21-Oct-2014 20:57:21.924 INFO [main] org.apache.catalina.core.AprLifecycleListener.init Cargada la biblioteca nativa APR de Apache Tomcat 1.1.31 con la versión APR 1.4.8. 
21-Oct-2014 20:57:21.924 INFO [main] org.apache.catalina.core.AprLifecycleListener.init Capacidades APR: IPv6 [true], enviar fichero [true], aceptar filtros [false], aleatorio [true]. 
21-Oct-2014 20:57:22.551 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL inicializado correctamente (OpenSSL 1.0.1h 5 Jun 2014) 
21-Oct-2014 20:57:22.649 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"] 
21-Oct-2014 20:57:22.662 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"] 
21-Oct-2014 20:57:22.664 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 992 ms 
21-Oct-2014 20:57:22.695 INFO [main] org.apache.catalina.core.StandardService.startInternal Arrancando servicio Catalina 
21-Oct-2014 20:57:22.695 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.14 
21-Oct-2014 20:57:22.708 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"] 
21-Oct-2014 20:57:22.719 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"] 
21-Oct-2014 20:57:22.722 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 57 ms 
Connected to server 
[2014-10-21 08:57:23,165] Artifact DebLan:war: Artifact is being deployed, please wait... 
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
SLF4J: Defaulting to no-operation (NOP) logger implementation 
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 
log4j:WARN No appenders could be found for logger (org.jboss.logging). 
log4j:WARN Please initialize the log4j system properly. 
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 
[2014-10-21 08:57:32,003] Artifact DebLan:war: Artifact is deployed successfully 
[2014-10-21 08:57:32,003] Artifact DebLan:war: Deploy took 8,838 milliseconds 
21-Oct-2014 20:57:32.712 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Despliegue del directorio C:...\App\tomcat8\webapps\manager de la aplicación web 
21-Oct-2014 20:57:32.762 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory C:...\App\tomcat8\webapps\manager has finished in 50 ms 
+0

你可以把你的日誌調試並檢查Spring的輸出嗎? – 2014-10-21 23:16:24

+0

請啓用Spring日誌並重試。 – 2014-10-22 00:05:15

+0

它太大,不符合崗位要求...我應該在調試日誌中搜索什麼?感謝您的迴應... – MarcosTonina 2014-10-22 02:04:48

回答

2

如果deblan是上下文根,那麼請不要把它放在servlet映射。你有這個網址:

http://localhost:8080/deblan/cuenta/cCuenta 

,如果你已經部署了此應用deblan作爲上下文根,那麼:

http://localhost:8080/deblan 

帶你到你的應用程序

/CUENTA/cCuenta

現在映射到您的控制器,因此請嘗試:

<servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/*</url-pattern> 
</servlet-mapping> 

這意味着http://localhost:8080/deblan +任何後來。

+0

這工作!但我不明白爲什麼......現在我無法訪問index.jsp,但是當我嘗試訪問http:// localhost:8080/deblan/cuenta/cCuenta時,控制器捕獲了請求! – MarcosTonina 2014-10-22 02:09:48

+0

它工作的原因是/ deblan會將請求發送到您的應用程序。 servlet映射標記只對/ deblan之後的內容感興趣。請通過選中向上/向下箭頭下的方框來接受我的答案,然後我們將查看您的index.jsp。 – BillFromHawaii 2014-10-22 03:29:43

+0

啊什麼。將你的url-pattern改爲/ from/*,你可能會得到你的索引。但請通過在向上/向下箭頭下選擇複選標記來接受我的回答。 – BillFromHawaii 2014-10-22 03:43:23