【问题标题】:Spring 3.2 MVC - Cannot match URL with ControllerSpring 3.2 MVC - 无法将 URL 与控制器匹配
【发布时间】:2014-10-22 08:06:31
【问题描述】:

我正在尝试使用 Spring 3.2 从头开始​​一个新的应用程序,当我尝试访问我的控制器时出现 404 错误。这是我的配置:

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>

web-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

浏览器尝试对http://localhost:8080/deblan/cuenta/cCuenta 进行GET,但我无法进入我的控制器。我调试了控制器,实际上并没有进入那里,所以:我的新手错误是什么?

提前致谢 马科斯

编辑:这是我的输出:

"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

【问题讨论】:

  • 你能把你的日志用来调试并检查 Spring 的输出吗?
  • 请启用 Spring 日志记录并重试。
  • 太大了,不符合发帖要求... 调试日志应该查什么?感谢您的回复...

标签: java spring spring-mvc controller


【解决方案1】:

如果 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 + 后面的任何内容。

【讨论】:

  • 行得通!!!但是我不明白为什么...现在我无法访问 index.jsp,但是当我尝试转到 localhost:8080/deblan/cuenta/cCuenta 时,控制器捕获了请求!
  • 它起作用的原因是 /deblan 将请求发送到您的应用程序。 servlet 映射标记只对 /deblan 之后的内容感兴趣。请通过选中向上/向下箭头下方的框来接受我的回答,然后我们将查看您的 index.jsp。
  • 啊什么鬼。将您的 url-pattern 更改为 / from /* ,您可能会恢复您的索引。但是请通过选择向上/向下箭头下的复选标记来接受我的回答。
猜你喜欢
  • 1970-01-01
  • 2014-06-29
  • 2012-12-11
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 2014-03-17
相关资源
最近更新 更多