Spring MVC:创建在ServletContext资源

时间:2017-04-29 02:38:51

标签: spring model-view-controller

我是Spring的新手,我正在尝试创建简单的spring MVC应用程序。但不幸的是我收到错误错误“创建名为'HandlerMapping'的bean在ServletContext资源中定义”。

请在下面找到我的文件,并在我出错的地方纠正我:

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>

弹簧调度-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”调用我的代码。

以下是我的错误列表供参考:

根本原因 - &gt;

org.springframework.beans.factory.BeanCreationException:在ServletContext资源[/WEB-INF/spring-dispatcher-servlet.xml]中定义了名为'HandlerMapping'的bean创建错误: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.CannotLoadBeanClassException:找不到ServletContext资源中定义名为'/Welcome.html'的bean的类[/ FirstSpringMVC / src / HelloController / HelloController]

根本原因 - &gt;

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)

根本原因 - &gt;

java.lang.ClassNotFoundException:/ FirstSpringMVC / src / HelloController / HelloController

2 个答案:

答案 0 :(得分:0)

您的代码中有几件事需要纠正。

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

答案 1 :(得分:0)

您已为映射到控制器类提供了绝对路径:

<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"/>

如果您将其替换为上面给出的