当前位置: 首页 > 知识库问答 >
问题:

Spring MVC:创建名为“handlermapping”的bean时出错,该bean在ServletContext资源中定义

欧阳飞章
2023-03-14
<?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>
<?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>
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;
    }

}
<%@ 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',它调用了我的代码。

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

共有1个答案

孟豪
2023-03-14

在您的代码中有几处需要更正。

1)组件扫描基本封装应更改-

<context:component-scan base-package="com.vijay.hellocontroller"/>

2)应更正Bean创建

<bean id="someid" class="com.vijay.hellocontroller.HelloController"></bean>
@Controller
public class HelloController extends AbstractController
 类似资料: