ClassNotFoundException但没有错

时间:2013-09-11 20:27:11

标签: java spring maven spring-mvc classnotfoundexception

我正在尝试从mkyong运行一个ControllerClassNameHandlerMapping示例。

我将项目导入eclipse并启动了tomcat 6.0,但是遇到了这个异常。我看了几个很容易错过的地方,却一无所获......

这是代码

web.xml:

<web-app id="WebApp_ID" 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">

    <display-name>Spring Web MVC Application</display-name>

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

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
   </context-param>

    <listener>
        <listener-class>
             org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

</web-app>

MVC-调度-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="caseSensitive" value="true" />
        <property name="pathPrefix" value="/customer" />
    </bean>

    <bean class="com.mkyong.common.controller.WelcomeController" />
    <bean class="com.mkyong.common.controller.HelloGuestController" />

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

目录结构:

![在此处输入图片说明] [1]

HelloGuestController:

package com.mkyong.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloGuestController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("HelloWorldPage");
        model.addObject("msg", "HelloGuestController");

        return model;
    }

}

WelcomeController:

package com.mkyong.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class WelcomeController extends AbstractController{

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("HelloWorldPage");
        model.addObject("msg", "WelcomeController");

        return model;
    }

}

异常通知:

引起:java.lang.ClassNotFoundException:com.mkyong.common.controller.WelcomeController

1 个答案:

答案 0 :(得分:0)

我可以想到这两种可能性:

  • 检查您的build / classes文件夹。它是否在相应的包中具有“WelcomeController”的类文件。
  • 如果您使用的是MAVEN,是否已将“Maven依赖项”添加到项目的“Web部署程序集”中。如果没有,请按如下方式添加(假设您使用的是Eclipse): 右键单击您的项目 - &gt;属性 - &gt;部署组件 - &gt;添加 - &gt; Java构建路径条目 - &gt;接下来然后从那里你可以添加“maven Dependencies”。然后构建并尝试运行您的应用程序。