Spring:ClassNotFound异常

时间:2016-06-28 06:57:47

标签: java spring spring-mvc spring2.x

我在spring mvc开始一个新项目,当我试图运行hello world时,它会抛出错误:

这是我的dispatcher-servlet.xml映射:

     <?xml version="1.0" encoding="UTF-8"?>
     <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean name="/welcome.html" 
        class="com.shaurya.FirstController" />

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

这是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>*.html</url-pattern>
   </servlet-mapping>
</web-app>

控制器代码在这里:

    package com.shaurya;

    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 FirstController extends AbstractController{
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse  response) throws Exception {
    String message="welcome to Spring";
    ModelAndView model= new ModelAndView("welcome");
    model.addObject("message", message);
     return model;
       }
     }

这是控制器呈现welcome.jsp的视图页面:

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
      <body>
        <a href="welcome.html">sdsd</a>
     </body>
    </html>

为什么会出现错误,看起来像这样:

    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find       class [com.shaurya.FirstController] for bean with name'/welcome.html' enter code heredefined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.shaurya.FirstController.

这里有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试更改此代码吗

<bean name="/welcome.html" 
        class="com.shaurya.FirstController" />

到这个

<bean name="/welcome" 
        class="com.shaurya.FirstController" />

由于.html的映射已经在web.xml中完成了,你可能不需要在bean映射中再次提到它