Tomcat不调用Spring MVC控制器

pme*_*ext 5 spring tomcat spring-mvc

更新:不知何故,经过另一轮调整和重新部署后,localhost:8080/ping-1.0/ping开始工作.配置文件仍然如下.我希望在不知情的情况下知道我修复了什么,但现在已经解决了.

我已经和它搏斗了几天,尝试了我在这里和其他地方看过的各种解决方案,但没有任何效果.我在Tomcat中部署了一个Spring MVC控制器,但无法访问它.

工具:

Spring 3.2.0
Tomcat 7
Java 1.6

弹簧控制器:

@Controller
public class PingController {

@RequestMapping("/ping")
public String ping (Model model) throws Exception {
    System.out.println("ping ping ping");
    String s = (new Date()).toString();
    model.addAttribute("message", s);
    return "ping";
}
}
Run Code Online (Sandbox Code Playgroud)

web.xml中:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
    <servlet-name>ping</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/ping-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>ping</servlet-name>
    <url-pattern>/ping</url-pattern>
</servlet-mapping>

</web-app>
Run Code Online (Sandbox Code Playgroud)

平servlet.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/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">

<context:component-scan base-package="myclass.ping"/>

<mvc:annotation-driven />
</beans>
Run Code Online (Sandbox Code Playgroud)

WAR文件名为ping-1.0.war.部署似乎没问题.我在$ CATALINA_BASE/webapps中看到一个名为ping-1.0的目录,这在catalina.log中:

INFO: Mapped "{[/ping],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String myclass.ping.PingController.ping(org.springframework.ui.Model) throws java.lang.Exception
Run Code Online (Sandbox Code Playgroud)

Tomcat在端口8080上运行.例如,我可以访问localhost:8080/manager.但localhost:8080/ping从Tomcat返回404消息.除了GET请求的记录之外,我在日志中看不到任何内容.完全没有错误.我尝试过很多变体的请求映射,URL过滤等等,但是无法让它工作.

cla*_*lav 4

您的 URL 上没有上下文根,但实际上需要有,/ping/ping因为调度程序 servlet 和 ping 控制器都映射到/ping. 尝试这个:

http://localhost:8080/ping-1.0/ping/ping
Run Code Online (Sandbox Code Playgroud)