Spring MVC控制器未加载

时间:2015-11-30 08:58:37

标签: java eclipse spring maven spring-mvc

我遇到Spring没有拿起控制器的问题。我已经使用maven webapp原型在Eclipse中创建了一个项目并且工作正常 - 我可以访问index.jsp页面。当我将Spring添加到项目中时出现问题。 (上下文,控制器等)

我正在使用Eclipse Mars,Tomcat 8,maven 3.3.3和Spring 4.2.2

以下是我在日志中看到的内容(可能只有第三行):

Looking for matching resources in directory tree [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller]
Searching directory [C:\Users\dima\spring_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\cte\WEB-INF\classes\com\company\dept\demo\cte\controller] for files matching pattern [C:/Users/dima/spring_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/cte/WEB-INF/classes/com/company/dept/demo/cte/controller/**/*.class]
Resolved location pattern [classpath*:com/company/dept/demo/cte/controller/**/*.class] to resources []

而不是像

这样的东西
INFO: Mapped URL path ...

因此,在尝试打开网页时,我获得了404。这些是文件:

调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ldap="http://www.springframework.org/schema/ldap"
    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
        http://www.springframework.org/schema/ldap 
        http://www.springframework.org/schema/ldap/spring-ldap.xsd">

    <context:component-scan base-package="com.company.dept.demo.cte.controller" />

    <mvc:annotation-driven/>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

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

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>

    </servlet>

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

</web-app>

这是我的控制器:

package com.company.dept.demo.cte.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/pid")
public class PidController {

    @RequestMapping(value="/hello")
    public ModelAndView helloWorld() {
        ModelAndView mav = new ModelAndView("pidHello");
        mav.addObject("message", "hello world");
        return mav;
    }
}

尝试访问localhost时:8080 / cte / pid / hello 404返回,日志显示:

DispatcherServlet with name 'dispatcher' processing GET request for [/cte/pid/hello]
Looking up handler method for path /pid/hello
Did not find handler method for [/pid/hello]
No mapping found for HTTP request with URI [/cte/pid/hello] in DispatcherServlet with name 'dispatcher'

感谢。

更新 WAR文件是正确的,但出于某种原因,当它部署到Tomcat时,控制器类没有被复制。

1 个答案:

答案 0 :(得分:2)

它是你的完整web.xml,因为你必须将你的调度程序servlet引用添加到web.xml,如下所示 -

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