Spring MVC:创建 ServletContext 资源中定义的名称为“HandlerMapping”的 bean 时出错
Posted
技术标签:
【中文标题】Spring MVC:创建 ServletContext 资源中定义的名称为“HandlerMapping”的 bean 时出错【英文标题】:Spring MVC : Error creating bean with name 'HandlerMapping' defined in ServletContext resource 【发布时间】:2017-04-29 02:38:50 【问题描述】:我是 Spring 新手,我正在尝试创建简单的 Spring MVC 应用程序。但不幸的是,我收到错误错误“创建名称为 'HandlerMapping' 在 ServletContext 资源中定义的 bean”。
请在下面找到我的文件并纠正我的错误:
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>FirstSpringMVC</display-name>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.springframework.samples.petclinic.web"/>
<!-- Helper controller class -->
<bean id="HandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"
></bean>
<!-- Req. process Controller class -->
<bean name="/Welcome.html" class="C:/Users/Vijay Mekala/Desktop/SAP/Java Spring/FirstSpringMVC/src/com/vijay/hellocontroller/HelloController"></bean>
<!-- View Resolver class -->
<bean id="ViewResolver"
class="org.springframework.web.servlet.view.InternalResourceviewResolver"
>
<property name="prefix">
<value>/WEB-INF</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
- HelloController
package com.vijay.hellocontroller;
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 HelloController extends AbstractController
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
ModelAndView modelandview = new ModelAndView("Hellopage");
modelandview.addObject("welcomemessage", "Welcome to MVC 1st Application");
return modelandview;
// return null;
HelloPage.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>First MVC App OM</title>
</head>
<body>
<h2>
$welcomemessage
</h2>
</body>
</html>
更多细节:
我正在尝试创建我的第一个没有注释的 Spring MVC 应用程序。因此,我的 URL 请求“http://localhost:8080/FirstSpringMVC/Welcome.html”会调用我的代码。
附上我的错误列表以供参考:
根本原因 -->
org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring-dispatcher-servlet.xml] 中定义名称为“HandlerMapping”的 bean 创建错误:bean 初始化失败;嵌套异常是 org.springframework.beans.factory.CannotLoadBeanClassException:找不到类 [/FirstSpringMVC/src/HelloController/HelloController] 用于 ServletContext 资源 [/WEB-INF/spring-dispatcher- 中定义的名称为“/Welcome.html”的 bean servlet.xml];嵌套异常是 java.lang.ClassNotFoundException: /FirstSpringMVC/src/HelloController/HelloController 相关原因:org.springframework.beans.factory.CannotLoadBeanClassException:找不到类 [/FirstSpringMVC/src/HelloController/HelloController] 用于 ServletContext 资源中定义的名称为“/Welcome.html”的 bean
根本原因 ->
org.springframework.beans.factory.CannotLoadBeanClassException: 找不到在 ServletContext 资源 [/WEB-INF/spring-dispatcher- 中定义的名称为“/Welcome.html”的 bean 的类 [/FirstSpringMVC/src/HelloController/HelloController] servlet.xml];嵌套异常是 java.lang.ClassNotFoundException: /FirstSpringMVC/src/HelloController/HelloController org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385)
根本原因 -->
java.lang.ClassNotFoundException: /FirstSpringMVC/src/HelloController/HelloController
【问题讨论】:
【参考方案1】:您的代码中有几处需要更正。
1) 组件扫描基础包应该改变 -
<context:component-scan base-package="com.vijay.hellocontroller"/>
2) 应更正 Bean 创建
<bean id="someid" class="com.vijay.hellocontroller.HelloController"></bean>
3) 你手动注释你的控制器如下
@Controller
public class HelloController extends AbstractController
【讨论】:
嗨 Shiva,非常感谢您的回复。我正在尝试创建我的第一个没有注释的 Spring MVC 应用程序。因此,我的 URL 请求“localhost:8080/FirstSpringMVC/Welcome.html”会调用我的代码。 好的。将注释分开一段时间。正如您现在发布的异常,它说找不到类。因此,您可以将前 2 个建议应用于您的项目,看看该错误是否消失? 嗨 Siva,我已将 bean 替换为你已经给出了映射到你的控制器类的绝对路径:
<bean name="/Welcome.html" class="C:/Users/Vijay Mekala/Desktop/SAP/Java Spring/FirstSpringMVC/src/com/vijay/hellocontroller/HelloController"></bean>
尝试根据您的包提供相对路径:
<bean name="/Welcome.html" class="com.vijay.hellocontroller.HelloController"></bean>
同时替换这段代码
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-
package="org.springframework.samples.petclinic.web"/>
以下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
我觉得这个不用提了
<context:component-scan base-
package="org.springframework.samples.petclinic.web"/>
如果你用上面给出的替换它
【讨论】:
以上是关于Spring MVC:创建 ServletContext 资源中定义的名称为“HandlerMapping”的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章
Spring Tool Suite 3.7:创建 Spring MVC 项目的选项在哪里