我正在尝试从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
最佳答案
如果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
+之后的任何内容。