我有这个错误' java.lang.ClassNotFoundException' - 春天

时间:2017-09-22 17:07:37

标签: java spring spring-mvc

我有这个错误:

java.lang.ClassNotFoundException: net.kzn.shoppingbackend.dao.CategoryDAO
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
    ...

我有两个项目,onlineshopping和shoppingbackend是连接的,onlineshopping的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>net.kzn</groupId>
    <artifactId>onlineshopping</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>onlineshopping Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <!-- Version information will be stored here -->
    <properties>
        <javaee.version>7.0</javaee.version>
        <spring.version>4.3.6.RELEASE</spring.version>
    </properties> 

    <dependencies>    
        <!-- Link backend project -->
        <dependency>
            <groupId>net.kzn</groupId>
            <artifactId>shoppingbackend</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>    
        <!-- JAVA EE -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee.version}</version>
            <scope>provided</scope>
        </dependency>    
        <!-- JUNIT -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>    
        <!-- SPRING -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>    
        <!-- JSTL -->    
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>    
        <!-- Spring webflow -->
        <dependency>
            <groupId>org.springframework.webflow</groupId>
            <artifactId>spring-webflow</artifactId>
            <version>2.4.4.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <!-- Updated for the latest version of JAVA -->
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>    
        <finalName>onlineshopping</finalName>
    </build>
</project>
shoppingbackend的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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>net.kzn</groupId>
    <artifactId>shoppingbackend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>    
    <name>shoppingbackend</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.6.RELEASE</spring.version>
        <hibernate.version>5.2.7.Final</hibernate.version>
        <jackson.version>2.8.7</jackson.version>
    </properties>

    <dependencies>
        <!-- JUNIT VERSION 4.12 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>    
        <!-- SPRING -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>    
        <!-- H2 Database -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.193</version>
        </dependency>    
        <!-- Hibernate Dependency -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- Database Connection Pooling -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>    
        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>    
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!-- SLF4J Logging -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.1</version>
        </dependency>    
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.24</version>
        </dependency>    
        <!-- Validation -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>    
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.4.1.Final</version>
        </dependency>    
    </dependencies>
    <build>
        <!-- Updated for the latest version of JAVA -->
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我有dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:wf="http://www.springframework.org/schema/webflow-config"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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    
    http://www.springframework.org/schema/webflow-config
    http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd        
    ">

    <context:component-scan base-package="net.kzn.onlineshopping" />
    <context:component-scan base-package="net.kzn.shoppingbackend" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />

    </bean>

    <!-- id must be multipartResolver -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.support.StandardServletMultipartResolver" />

    <!-- Loading static resources -->
    <mvc:annotation-driven />

    <mvc:resources location="/assets/" mapping="/resources/**" />

    <!-- WEBFLOW CONFIGURATION -->
    <!-- Entry point for the flow -->

    <wf:flow-executor id="flowExecutor" flow-registry="flowRegistry" />

    <wf:flow-registry id="flowRegistry" base-path="/WEB-INF/views/flows"
        flow-builder-services="flowBuilderServices">
        <wf:flow-location-pattern value="/**/*-flow.xml" />
    </wf:flow-registry>

    <wf:flow-builder-services id="flowBuilderServices"
        view-factory-creator="viewFactoryCreator" validator="validator" />

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor" />
    </bean>

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
        <property name="flowRegistry" ref="flowRegistry" />
        <property name="order" value="-1" />
    </bean>

    <bean id="viewFactoryCreator"
        class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
        <property name="viewResolvers" ref="viewResolver" />
    </bean>

    <!-- validator bean -->
    <bean id="validator"
        class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

</beans>

Class PageController.class:

package net.kzn.onlineshopping.controller;

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

import net.kzn.shoppingbackend.dao.CategoryDAO;

@Controller
public class PageController {

    @Autowired
    private CategoryDAO categoryDAO;

    @RequestMapping(value = {"/", "/home", "/index"})
    public ModelAndView index() {       
        ModelAndView mv = new ModelAndView("page");
        mv.addObject("title","Home");
        mv.addObject("userClickHome",true);
        //passing the list of categories 
        mv.addObject("categories", categoryDAO.list());
        return mv;              
    }
    @RequestMapping(value = "/about")
    public ModelAndView about() {       
        ModelAndView mv = new ModelAndView("page");     
        mv.addObject("title","About Us");
        mv.addObject("userClickAbout",true);
        return mv;              
    }   

    @RequestMapping(value = "/contact")
    public ModelAndView contact() {     
        ModelAndView mv = new ModelAndView("page");     
        mv.addObject("title","Contact Us");
        mv.addObject("userClickContact",true);
        return mv;              
    }
}

我的CategoryDAO.java:

package net.kzn.shoppingbackend.dao;    
import java.util.List;
import net.kzn.shoppingbackend.dto.Category;

public interface CategoryDAO {

    //Category get(int id);
    List<Category> list();
}

和CategoryDAOImpl.java:

package net.kzn.shoppingbackend.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import net.kzn.shoppingbackend.dao.CategoryDAO;
import net.kzn.shoppingbackend.dto.Category;

@Repository("categoryDAO")
public class CategoryDAOImpl implements CategoryDAO {

    private static List<Category> categories = new ArrayList<>();

    static {
        Category category = new Category();

        //Adding firt category

        category.setId(1);
        category.setName("Television");
        category.setDescription("This is  some description for Television!");
        category.setImageURL("CAT_1.png");
        categories.add(category);

        //second category
        category = new Category();
        category.setId(2);
        category.setName("Mobile");
        category.setDescription("This is  some description for Mobile!");
        category.setImageURL("CAT_2.png");
        categories.add(category);

        //third category
        category = new Category();
        category.setId(3);
        category.setName("Laptop");
        category.setDescription("This is  some description for Laptop!");
        category.setImageURL("CAT_3.png");
        categories.add(category);
    }

    @Override
    public List<Category> list() {
        // TODO Auto-generated method stub
        return categories;
    }
}

请伙计们,我有几天试图解决这个问题。我无法找到错误的位置。感谢。

2 个答案:

答案 0 :(得分:0)

我解决了我的问题,我不知道它是否正确......但它有效:

属性&gt;&gt; Java构建路径&gt;&gt;来源&gt;&gt;链接源链接文件夹 位置&gt;&gt;文件夹名称:“shoppingbackend”&gt;&gt;完成

答案 1 :(得分:0)

请确认正在&#34; src / main / java&#34;中创建包。

enter image description here