Spring MVC 异常错误代码:无法找到类[com.spring.dao.StudentDao]

zi8p0yeb  于 2022-11-14  发布在  Spring
关注(0)|答案(1)|浏览(110)

我遇到的错误请帮助我修复它!!

你好,当我运行MyApplication时,我会得到index.jsp页面沿着当以下页面不工作时,当我单击index.jsp的子页面时,会触发ClassNotFoundException(* 注意:新的stackoverflow我试图附加图像,但我把作为链接由于声誉问题 *)

javax.servlet.ServletException: Servlet.init() for servlet spring threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1437)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
root cause

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.spring.dao.StudentDao] for bean with name 'dao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.spring.dao.StudentDao
    org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1281)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1350)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:916)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1437)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
root cause

java.lang.ClassNotFoundException: com.spring.dao.StudentDao
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
    org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:416)
    org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1302)
    org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1273)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1350)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:916)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1437)
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/8.5.9 logs.

pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.student</groupId>
  <artifactId>MyApplication</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>MyApplication Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
       <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>MyApplication</finalName>
  </build>
</project>

网页.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>SpringMVC</display-name>
  <servlet>
  
    <servlet-name>spring</servlet-name>    
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      
    <load-on-startup>1</load-on-startup> 
</servlet>   
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 
</web-app>

Spring服务小程序.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:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    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  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <context:component-scan base-package="com.spring.controller"></context:component-scan>    
        
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
    <property name="prefix" value="/WEB-INF/jsp/"></property>    
    <property name="suffix" value=".jsp"></property>    
</bean>    
    
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>    
    <property name="url" value="jdbc:mysql://localhost:3306/spring_mvc"></property>    
    <property name="username" value="root"></property>    
    <property name="password" value="Brownie@753"></property>    
</bean>    
    
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">    
    <property name="dataSource" ref="ds"></property>    
</bean>    
    
<bean id="dao" class="com.spring.dao.StudentDao">    
    <property name="template" ref="jt"></property>
</bean>       
</beans>

学生控制器.java

package com.spring.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.dao.StudentDao;
import com.spring.model.Student;

@Controller
public class StudentController {
    
    @Autowired
    StudentDao dao;
    
    
    @RequestMapping("/studentform")
    public String showform(Model m)
    {
        m.addAttribute("command", new Student());
        return "studentform";
    }
    
    @RequestMapping(value="/save",method = RequestMethod.POST)
    public String save(@ModelAttribute("Student") Student student)
    {
        dao.save(student);
        return "redirect:/viewstudent";
    }
    
    @RequestMapping(value="/viewstudent")
    public String viewstudent(Model m)
    {
        List<Student> list = dao.getStudents();
        m.addAttribute("list",list);
        System.out.println(list.size());
        return "viewstudent";
    }
    
    @RequestMapping(value="/editstudent/{id}")
    public String edit(@PathVariable int id, Model m)
    {
        Student student = dao.getStudentById(id);
        m.addAttribute("command",student);
        return "studenteditform";
    }   
    
    @RequestMapping(value="/editsave", method = RequestMethod.POST)
    public String editsave(@ModelAttribute("student") Student student)
    {
        dao.update(student);
        return "redirect:/viewstudent";
    }
    
    @RequestMapping(value="/deletestudent/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable int id)
    {
        dao.delete(id);
        return "redirect:/viewstudent";
    }
}

学生道.java

package com.spring.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.spring.model.Student;

public class StudentDao {

    JdbcTemplate template;
     /**
     * @param jdbcTemplate the jdbcTemplate to set
     */
    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }
    
    public int save(Student s)
    {
        String sql ="INSERT INTO student_detail (name,phone,address)VALUES('"+s.getName()+"','"+s.getPhone()+"','"+s.getAddress()+"')";
        return template.update(sql);
    }
    public int update(Student s)
    {
        String sql = "UPDATE student_detail SET name='"+s.getName()+"', phone='"+s.getPhone()+"', address='"+s.getAddress()+"' WHERE id="+s.getId()+"";
        return template.update(sql);
    }
    public int delete(int id)
    {
        String sql ="DELETE FROM student_detail WHERE id="+id+"";
        return template.update(sql);
    }
    public Student getStudentById(int id)
    {
        String sql = "SELECT * FROM student_detail WHERE id=?";
            return template.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<Student>(Student.class));
    }
    public List<Student> getStudents()
    {
         return template.query("SELECT * FROM student_detail",new RowMapper<Student>(){    
                public Student mapRow(ResultSet rs, int row) throws SQLException {    
                    Student s=new Student();    
                    s.setId(rs.getInt(1));    
                    s.setName(rs.getString(2));    
                    s.setPhone(rs.getString(3));    
                    s.setAddress(rs.getString(4));    
                    return s;    
                }    
            });    
    }
}

项目结构与构建路径声明:

eclipce project structure
java build path

dxxyhpgq

dxxyhpgq1#

这里有几件事。首先,我认为您需要调整组件扫描,以在com.spring级别进行扫描,而不是在com.spring.controller级别进行扫描,因为这将排除com.spring.dao包。您还需要通过添加annotation-driven标记来启用注解扫描。因此,spring-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:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    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  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <annotation-driven />
        <context:component-scan base-package="com.spring"></context:component-scan>    
        
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
    <property name="prefix" value="/WEB-INF/jsp/"></property>    
    <property name="suffix" value=".jsp"></property>    
</bean>    
    
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>    
    <property name="url" value="jdbc:mysql://localhost:3306/spring_mvc"></property>    
    <property name="username" value="root"></property>    
    <property name="password" value="Brownie@753"></property>    
</bean>    
    
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">    
    <property name="dataSource" ref="ds"></property>    
</bean>    
    
<!-- Not needed if annotated <bean id="dao" class="com.spring.dao.StudentDao">    
    <property name="template" ref="jt"></property>
</bean> -->
</beans>

然后你可以切换到你的学生刀上的一个注解:

@Component
public class StudentDao {

    @Autowired
    JdbcTemplate template;

并删除spring-servlet.xml中的定义。
您还可以在不同的类中定义DB连接,以精简XML,但这是另一个主题。

相关问题