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

Spring-mvc Maven Hibernate上的配置错误,无法访问http no映射

穆嘉
2023-03-14

我有点问题。我正在用Spring工具套件构建一个应用程序,由SpringMVC Maven和Hibernate组成。我认为我所有的编码都可以或者接近于好,尽管我对Spring一无所知。

这里我放了我的项目的2个屏幕:

这里我把我认为与这个问题有关的文件

应用-config.mxl:

<?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: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.service"/>

</beans>
package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import form.Egg;
import service.EggService;

@Controller
//@RequestMapping("/ChickenTest/**")
public class GeneralController {

    @Autowired
    EggService eggService;

    @RequestMapping(value="/index")
    public ModelAndView index(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }

    //Index of Egg webpage
    @RequestMapping(value="/Egg/indexEgg")
    public ModelAndView indexEgg(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }

    //Action of adding an Egg
    @RequestMapping(value="/Egg/addEgg", method = RequestMethod.POST)
    public ModelAndView addEgg(@ModelAttribute Egg egg){
        ModelAndView mav = new ModelAndView("/index");

        Egg eggAux = egg;
        eggService.addEgg(eggAux);

        return mav;
    }

    //View list of eggs
    @RequestMapping(value="/Egg/viewEggs", method = RequestMethod.POST)
    public ModelAndView viewEggs(){
        ModelAndView mav = new ModelAndView("/index");

        mav.getModelMap().addAttribute("eggList", eggService.viewEggs());

        return null;
    }



    @RequestMapping(value="/Chicken/indexChicken")
    ModelAndView indexChicken(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }

    @RequestMapping(value="/Farm/indexFarm")
    ModelAndView indexFarm(){
        ModelAndView mav = new ModelAndView();

        return mav;
    }
}

web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>ChickenTest</display-name>

   <!--
        - Location of the XML file that defines the root application context.
        - Applied by ContextLoaderListener.
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!--
        - Servlet that dispatches request to registered handlers (Controller implementations).
    -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

mvc-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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.web"/>


    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>

</beans>

下面是当我尝试输入一个JSP时控制台的输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ChickenTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> tomcat-maven-plugin:1.1:run (default-cli) @ ChickenTest >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ ChickenTest ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ ChickenTest ---
[INFO] Compiling 16 source files to C:\Java\workspace2\ChickenTest\target\classes
[INFO] 
[INFO] <<< tomcat-maven-plugin:1.1:run (default-cli) @ ChickenTest <<<
[INFO] 
[INFO] --- tomcat-maven-plugin:1.1:run (default-cli) @ ChickenTest ---
[INFO] Running war on http://localhost:8080/ChickenTest
[INFO] Using existing Tomcat server configuration at C:\Java\workspace2\ChickenTest\target\tomcat
Nov 08, 2013 12:31:45 AM org.apache.catalina.startup.Embedded start
INFO: Starting tomcat server
Nov 08, 2013 12:31:45 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
Nov 08, 2013 12:31:45 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Nov 08, 2013 12:31:45 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Nov 08, 2013 12:31:45 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Fri Nov 08 00:31:45 ART 2013]; root of context hierarchy
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring/application-config.xml]
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@767ae5: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Nov 08, 2013 12:31:45 AM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 245 ms
Nov 08, 2013 12:31:45 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcherServlet'
Nov 08, 2013 12:31:45 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization started
Nov 08, 2013 12:31:45 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Fri Nov 08 00:31:45 ART 2013]; parent: Root WebApplicationContext
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/mvc-config.xml]
Nov 08, 2013 12:31:45 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@132299b: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@767ae5
Nov 08, 2013 12:31:46 AM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcherServlet': initialization completed in 384 ms
Nov 08, 2013 12:31:46 AM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Nov 08, 2013 12:31:46 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
HTTP Status 404 - /ChickenTest/index.jsp

type Status report

message /ChickenTest/index.jsp

description The requested resource (/ChickenTest/index.jsp) is not available.

Apache Tomcat/6.0.29

感谢阅读!如果你需要更多的东西,我会和你联系的!

更新1:

First, your @Controller class is in

package controller;
so your DispatcherServlet config needs to have a component-scan on that package. So change yours to

<context:component-scan base-package="controller"/>
So that the <mvc:annotation-driven> gets applied and your @Controller class is added as a handler.

Now even if you do this, you still don't seem to have a handler method for the path

/ChickenTest/index.jsp
You'll want to either add a <welcome-file> to your web.xml and an index.jsp file in your /webapps folder or add a @RequestMapping handler method that can handler /index.jsp.

但现在控制台告诉我GeneralController.class上的@autowire不工作。

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'generalController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.chicken.service.EggService com.chicken.controller.GeneralController.eggService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.chicken.service.EggService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

共有1个答案

查淮晨
2023-03-14

首先,您的@controller类在

package controller;

因此DispatcherServlet配置需要在该包上有一个component-scan。所以把你的改成

<context:component-scan base-package="controller"/>

以便应用 并将您的@controller类添加为处理程序

/ChickenTest/index.jsp

您现在得到的异常是(分解)

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'generalController': 
Injection of autowired dependencies failed; 

nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: com.chicken.service.EggService com.chicken.controller.GeneralController.eggService; 

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.chicken.service.EggService] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

因此,组件扫描无法为generalcontroller创建bean,因为Spring上下文找不到需要注入的eggservicebean。唯一可能的原因是您的上下文中没有eggservicebean。您没有将其声明为 或者您的component-scan没有扫描您的eggservice类声明为@component的包。

 类似资料:
  • 我正在尝试使用spring cloud配置,1.3.0.release版本,我成功地启动了服务器 我尝试用下面的pom.xml构建简单的客户端 然后我使用mvn spring-boot:run,它总是停止 如果我添加依赖项 spring-boot-starter-web 它可以启动,但当我访问localhost:8080/env时,它总是返回404,这里缺少什么?

  • 我正在使用IntelliJ IDEA,并且Spring的所有插件都被激活了,但是当我加载我的Maven项目时,我遇到了以下错误: Spring配置检查 找到未映射的Spring配置文件。 请为模块配置/设置Spring刻面 有什么想法阻止它自动配置?

  • 我使用Micronaut 3.2.3和Kubernetes集成,从配置映射和机密中注入配置值。 依赖: bootstrap.yml 如您所见,应用程序配置图包括kafka.brokers值: 我添加了一个简单的单例类,用于检查是否可以注入项目: 日志跟踪似乎表明这些配置映射已被正确访问: 但是,应用程序崩溃,因为它找不到它:

  • 遥不可及!更改:false,msg:SSH错误:无法将数据发送到远程主机。确保可以通过ssh访问此主机,“不可访问”:true 主持人: ansible.cfg 我正在使用executeshell运行Jenkins的剧本`

  • 正在尝试将代码更新为Selenium 3。x、 在尝试运行测试时,我不断遇到一个错误: 同样的代码曾经在运行测试时工作,我真的不知道它在哪里或为什么会出现这样一个奇怪的错误。我似乎找不到任何人以前写过的任何东西,所以我希望stackoverflow社区可以帮助我解决这个问题。 以下是生成此错误的代码:

  • 我正在通过 Windows(10) 启动时的批处理文件启动我的应用程序。这是我在批处理文件中的内容 - 当windows启动时,我得到了 无法访问文件.jar 然而,问题是在这个错误消息之后,应用程序仍然在启动中,并且运行良好。正在访问jar但我仍然无法访问jar错误的原因是什么,如何避免它?找不到这个特定案例的任何答案。