Spring LoadBeanClasseException
我创建了简单的SpringMVC控制器,当我试图执行时,我得到了错误 错误消息: org.springframework.beans.factory.CannotLoadBeanCl AssetException:找不到在ServletContext资源[/web-INF/SpringApp servlet.xml]中定义的名为“Redirect.jsp”的bean的类[SpringApp.web.java.HelloController];嵌套异常是java.lang.ClassNotFoundException:SpringApp.web.java.HelloController 这是我的应用程序结构 SpringAppSpring LoadBeanClasseException,spring,Spring,我创建了简单的SpringMVC控制器,当我试图执行时,我得到了错误 错误消息: org.springframework.beans.factory.CannotLoadBeanCl AssetException:找不到在ServletContext资源[/web-INF/SpringApp servlet.xml]中定义的名为“Redirect.jsp”的bean的类[SpringApp.web.java.HelloController];嵌套异常是java.lang.ClassNotFoun
----Web Pages
----META-INF
----WEB-INF
----springapp-servlet.xml
----web.xml
----Redirect.jsp
----index1.jsp
----Source Packages
----java
----HelloController.java
web.xml
--------
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<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>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index1.jsp
</welcome-file>
</welcome-file-list>
</web-app>
springapp-servlet.xml
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<!--
1. This file will be used up by the DispatcherServlet and which contains the bean definition
2. The file will be picked up by the specification in the WEB-INF/web.xml using <servlet>spring</servlet>
3. hello controller is responsible for handling the request for the particular page of the website and known
as the page controller.
-->
<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">
<!-- the application context definition for the springapp DispatcherServlet -->
<bean name="Redirect.jsp" class="SpringApp.web.java.HelloController"/>
</beans>
index1.jsp
---------
<%--
Document : index
Created on : Nov 23, 2012, 11:55:53 AM
Author : gopc
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Redirect.jsp
------------
<%--
Document : index
Created on : Nov 23, 2012, 11:55:53 AM
Author : gopc
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Controller redirect</title>
</head>
<body>
<h1>This is redirect from the HelloController!</h1>
</body>
</html>
HelloController.java
-------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package java;
/**
*
* @author gopc
*/
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
public class HelloController implements Controller {
protected final Log logger = LogFactory.getLog(getClass());
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
logger.info("Returning hello view");
System.out.println("hi this handlerequest");
return new ModelAndView("Redirect.jsp");
}
}
你说:
<bean name="Redirect.jsp" class="SpringApp.web.java.HelloController"/>
这意味着Spring希望在SpringApp.web.java包中找到您的控制器。您已经将其放入java包中
将控制器中的package语句更改为SpringApp.web.java,并将源文件移动到相应的目录SpringApp/web/java
另外请注意,java包是为java平台保留的,因此无论如何都不应该使用它。您不能将类放在名为java的包中。这个包是为标准JDK类保留的 如果类HelloController在包foo中,则其类名为foo.HelloController。不是SpringApp.web.foo.HelloController 调用bean Redirect.jsp会造成很多混乱。为什么用JSP名称调用控制器bean
坦率地说,您似乎没有掌握非常基本的Java概念,如类和包。在玩Spring之前,我会先从基础知识开始,Spring是一个复杂的野兽。嘿,现在我已经删除了java文件夹,也删除了java包。现在我可以显示index1.jsp的内容,但是我没有从控制器获得任何调试阶段或System.out.println。我建议您单独提问。但是FWIW:我在XML中没有看到任何链接index1.jsp和控制器bean的内容,所以我不明白为什么会调用它。特别是,Spring dispatcher servlet只配置为对以.htm结尾的文件执行任何操作,而index1.jsp则不是这样。我尝试了Redirect.htm,但仍然没有成功,我将为此发布一个新线程。嘿,现在我已经删除了文件夹java,也删除了包java。现在我可以显示index1.jsp的内容,但是我没有从控制器获得任何调试阶段或System.out.println。。