spring mvc error:BeanCreationException

时间:2017-09-21 12:31:00

标签: spring exception model-view-controller

我从服务器下载文件时在spring mvc中出错。我无法找到错误。请帮助我..

控制器是:

@Controller
public class CareerController {

    //inject objects of dao class
    @Autowired
    CareerDAO cardao;

    private static final int BUFFER_SIZE = 4096;

    @Autowired
    ServletContext context;

    //inject objects of dao class
    // @Autowired
    // VacancyDAO Vdao;
    //inject objects of JavaMailSender class
    @Autowired
    private MailMail m;

    public void setM(MailMail m) {
        this.m = m;
    }

    public void setCardao(CareerDAO cardao) {
        this.cardao = cardao;
    }

    String resPath = "C:\\Users\\Anjana\\Documents\\NetBeansProjects\\IntelliLabsWeb\\build\\web\\uploads\\";

    public String getResPath() {
        return resPath;
    }

    public void setResPath(String resPath) {
        this.resPath = resPath;
    }

    //go to careers page from index page
    //-------------------------------------------------------------------- 
    @RequestMapping("/careersPage")
    public ModelAndView ViewVacancyForPublic() {
        return new ModelAndView("redirect:/AllVacancies");
    }

    //go to Job Application Form 
    //--------------------------------------------------------------------
    @RequestMapping("/ApplicationForm")
    public ModelAndView Apply() {
        return new ModelAndView("careers/ApplicationForm", "command", new Career());
    }



    //delete applicant only by admin
    //----------------------------------------------------------------------   
    @RequestMapping(value = "/deleteProfile/{a_id}")
    public ModelAndView deleteProfile(@PathVariable int a_id) {

        if (cardao.deletion(a_id) == 1) {
            System.out.println("Profile " + a_id + " deleted\n");
            return new ModelAndView("redirect:/ShowApplicants");
        } else {
            return new ModelAndView("redirect:/failiure");
        }

    }

    //show one profile for admin
    //----------------------------------------------------------------------   
    @RequestMapping(value = "/oneProfile/{a_id}")
    public ModelAndView viewOneProfile(@PathVariable int a_id) {
        Career prof = cardao.retrieveProfile(a_id);

        prof.setResumePath(resPath);

        System.out.println("\n\nid : " + prof.getA_id());
        System.out.println("name : " + prof.getName());
        System.out.println("path" + prof.getResumePath());
        System.out.println("date : " + prof.getDateApplied() + "\n\n");

        List<Career> profList = new ArrayList<>();
        profList.add(prof);
        return new ModelAndView("admin/ViewOneProfile", "profile", prof);
    }

    //method doUpload -- uploads file to uploads folder
    //********************************************************************
    private File doUpload(HttpServletRequest request, Career career) {

        String Aname = career.getName();
        System.out.println("Name: " + Aname);

        // Root Directory.
        String uploadRootPath = request.getServletContext().getRealPath("/uploads");
        //ie,create a folder named "uploads" under the project's Web Pages folder.
        //otherwise nullpointer exception will be thrown. 
        //actually,program creates the directory if it doesn't exists.
        //just don't forget to put the slash(/) in getRealPath("/uploads")
        System.out.println("uploadRootPath=" + uploadRootPath);

        File uploadRootDir = new File(uploadRootPath);
        // Create directory if it not exists.
        if (!uploadRootDir.exists()) {
            uploadRootDir.mkdirs();
        }

        CommonsMultipartFile fileData = career.getFileDatas();

        // Client File Name
        String name = fileData.getOriginalFilename();
        System.out.println("Client File Name = " + name);

        File serverFile = null;

        if (name != null && name.length() > 0) {
            try {
                // Create the file on server
                serverFile = new File(uploadRootDir.getAbsolutePath() + File.separator + name);

                // Stream to write data to file in server.
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
                stream.write(fileData.getBytes());
                stream.close();
                //

                System.out.println("Write file: " + serverFile);
            } catch (Exception e) {
                System.out.println("Error Write file: " + name);
            }
        }

        // model.addAttribute("Aname", Aname);
        //model.addAttribute("resumeFile", serverFile); 
        return serverFile;

    }

    //download resume
    //----------------------------------------------------------------------  
     @RequestMapping(value="/download",method=RequestMethod.GET)
    public void downloadPDFResource(@RequestParam("myResName") String fileName,HttpServletRequest request,
            HttpServletResponse response){
           // @PathVariable("fileName") String fileName) {

         System.out.println("filename i got :"+fileName);
        //If user is not authorized - he should be thrown out from here itself

        //String fileName="Ente Kadha - Madhavikkutty.pdf";

        //Authorized user will download the file
        String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/downloads/");
        Path file = Paths.get(dataDirectory, fileName);
        if (Files.exists(file)) {
            response.setContentType("application/pdf");
            response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
            try {
                Files.copy(file, response.getOutputStream());
                response.getOutputStream().flush();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        else
        {
            System.out.println("\n\nFile not found!!\n\n");
            System.out.println(file);
        }
    }
}

dispatcher-servlet是:

<?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-3.0.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

    <!--<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>-->

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <!--<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>-->

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <bean id="multipartResolver"   
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

    <bean id="con" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/intelliLABS"></property>
        <property name="username" value="root"></property>   
        <property name="password" value="root"></property> 

        <!--
        //for ms sql server,it may be like :
        database-driver=net.sourceforge.jtds.jdbc.Driver
        url=jdbc:jtds:sqlserver://localhost:1433/simplehr;instance=SQLEXPRESS
        username=shoppingcart
        password=12345
        -->     
    </bean>

    <!--    normal jdbc template bean-->
    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="con"></property>
    </bean>

    <!-- For sending email,adding spring's mail support -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
        <property name="host" value="smtp.gmail.com" />  
        <property name="username" value="thisisanjusdummyacc@gmail.com" />  
        <property name="password" value="thisisdummy" />  
        <property name="javaMailProperties">  
            <props>  
                <prop key="mail.smtp.auth">true</prop>  
                <prop key="mail.smtp.socketFactory.port">465</prop>  
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>  
                <prop key="mail.smtp.port">465</prop>  
            </props>  
        </property>  
    </bean>

    <bean id="mailMail" class="bean.MailMail">  
        <property name="mailSender" ref="mailSender" />  
    </bean>  

    <!--  this bean uses normal jdbc template -->
    <bean id="contactsDAO" class="DAO.ContactsDAO"> 
        <property name="jdbc" ref="template"></property>
    </bean>

    <!--  this bean uses normal jdbc template -->
    <bean id="vacancyDAO" class="DAO.VacancyDAO"> 
        <property name="jdbc1" ref="template"></property>
    </bean>

    <!--  this bean uses normal jdbc template -->
    <bean id="careerDAO" class="DAO.CareerDAO"> 
        <property name="jdbc2" ref="template"></property>
    </bean>

</beans>

viewOneProfile.jsp是:

<%-- 
    Document   : ViewOneProfile
    Created on : 13 Sep, 2017, 2:23:42 PM
    Author     : Anjana
--%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Profile</title>
    </head>
    <body>

        <table>

            <th><h3>${profile.name}</h3></th>
        <tr>
            <td>Application Id :</td>
            <td>${profile.a_id}</td>
        </tr>
        <tr>
            <td>Name :</td>
            <td>${profile.name}</td>
        </tr>
        <tr>
            <td>Address :</td>
            <td>${profile.address}</td>
        </tr>
        <tr>
            <td>Email:</td>
            <td>${profile.email}</td>
        </tr>
        <tr>
            <td>Phone:</td>
            <td>${profile.phone}</td>
        </tr>
        <tr>
            <td>Vacancy id:</td>
            <td></td>
        </tr>
        <tr>
            <td>Date Applied :</td>
            <td>${profile.dateApplied}</td>
        </tr>
        <tr>
        <form:form method="post" action="download">
            <td>Resume :  ${profile.resumePath}${profile.resumeDoc} </td>
            <td><input type="hidden" path="myResName" value="${profile.resumeDoc}" /></td>
            <td> <input type="submit" value="download"/> </td>
        </form:form>
    </tr>

</table>

</body>
</html>

apache日志显示:

21-Sep-2017 16:05:00.467 SEVERE [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log StandardWrapper.Throwable
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'regController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: dao.UserDAO controller.regController.userdao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDAO] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1236)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4910)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5192)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:579)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1744)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: dao.UserDAO controller.regController.userdao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDAO] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 33 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDAO] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 35 more

21-Sep-2017 16:05:00.467 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet /SpringLogin threw load() exception
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dao.UserDAO] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1100)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1236)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1149)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4910)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5192)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:579)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1744)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)

我以为我曾经把文件名从jsp页面传递给spring控制器 是错的。我尝试了很多方法来传递jsp中的值...但没有任何效果。

浏览器只显示错误&#34;客户端发送的请求在语法上不正确。&#34; 但是那个语法错误是什么?

0 个答案:

没有答案