java – Spring MVC Web应用程序:找不到默认构造函数

前端之家收集整理的这篇文章主要介绍了java – Spring MVC Web应用程序:找不到默认构造函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

项目ZIP:http://goo.gl/ddhLg5

无法执行Spring Web应用程序,从而出现HTTP Status 500错误.它还写出没有为DSLR找到默认构造函数,但实际上有一个默认构造函数.也许它与应用程序上下文或我的bean声明的方式有关?我的申请无法启动的原因是什么?

DSLR:

 package main.java.com.springapp.mvc.model;

    public class DSLR {

    public DSLR() {
    }
    public void init() {}

    private int dslrId;
    private String model;
    private int price;
    private String description;

    public int getDslrId() {
        return dslrId;
    }

    public void setDslrId(int dslrId) {
        this.dslrId = dslrId;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "DSLR [dslr=" + dslrId + ",model=" + model
                + ",price=" + price+ ",description=" + description+"]";
    }
}

web.xml中

DSLRServletController-servlet.xml中:

 

MVC-调度-servlet.xml中

错误

HTTP Status 500 - Request processing Failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lmain.java.com.springapp.mvc.model.DSLR;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.Failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lmain.java.com.springapp.mvc.model.DSLR;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.Failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [[Lmain.java.com.springapp.mvc.model.DSLR;]: No default constructor found; nested exception is java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.

主控制器类:

package main.java.com.springapp.mvc.controller;

import main.java.com.springapp.mvc.dao.DSLRDAO;
import main.java.com.springapp.mvc.model.DSLR;
import main.java.com.springapp.mvc.pckg.DSLRForm;
import main.java.com.springapp.mvc.pckg.DSLRValidaor;
import org.apache.log4j.Logger;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;

@Controller
public class DSLRServletController extends HttpServlet  {
    static Logger logger = Logger.getLogger(DSLRServletController.class);

    private DSLR DSLR;
    private DSLRDAO dslrDAO;
    private DSLR dslr;

   /* request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=utf-8");
    List list = getActions(request,response);
    response.setContentType("text/html; charset=UTF-8");*/

    //@Autowired   ???
    public DSLRServletController() {
        this.dslrDAO = new DSLRDAO();
    }

    public void init() {logger.error("DSLRServlet.init(): just started"); }
    /*
            /s                                    GET
            /s?id=1                               GET
            /s?action=save                        POST
            /dslrservice?id=1&tm=@modelattribute("dslrs") DSLR dslrs[],@modelattribute("dslr") DSLR dslr,@modelattribute("dslrErrors") HashMap dslrErrors,@modelattribute ("dslrform") DSLRForm dslrForm,@RequestParam("id") String paramId,@RequestParam("action") String paramAction,Model model){

    if(paramId == null || paramId.equals("")){
        //show_all_dslrs
        dslrs = getAllDslrs();    // DSLR adslrs[] -> to MODEL;
       return "dslrs";
    }else{
        //show_this_dslr;
        HashMap@modelattribute("dslrs") DSLR dslrs[],@modelattribute("dslrErrors") HashMap

UPDATE1:

新的mvc-dispatcher-servlet.xml:

并且@Autowired被放入DSLRServletController:

@Autowired
    public DSLRServletController() {
        this.dslrDAO = new DSLRDAO();
    }

错误

exception

javax.servlet.ServletException: Servlet.init() for servlet mvc-dispatcher threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:744)
root cause

java.lang.IllegalStateException: Autowired annotation requires at least one argument: public main.java.com.springapp.mvc.controller.DSLRServletController()
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:243)
    org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapablebeanfactory.java:976)
    org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBeanInstance(AbstractAutowireCapablebeanfactory.java:949)
    org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:490)
    org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:461)
    org.springframework.beans.factory.support.Abstractbeanfactory$1.getObject(Abstractbeanfactory.java:295)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:292)
    org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:194)
    org.springframework.beans.factory.support.DefaultListablebeanfactory.preInstantiateSingletons(DefaultListablebeanfactory.java:607)
    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:647)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:744)

出于某种原因出现了2个错误.应该更改什么才能正确设置项目?

UPDATE3

新的mvc-dispatcher-servlet.xml:

除了在UPDATE1中添加的@Autowired之外,还添加了另一个(在DSLRServletController中):

@Autowired
    public void setDSLR(DSLR DSLR) {
        dslr = DSLR;
    }

UPDATE4:

回到原始错误:无法实例化bean类[[Lmain.java.com.springapp.mvc.model.DSLR;]

我很迷惑.怎么了?

最佳答案
在您的控制器方法中,您已将参数用作如下所示的数组

@modelattribute("dslrs") DSLR dslrs[]

所以使用像list这样的集合

@modelattribute("dslrs") ArrayList

猜你在找的Spring相关文章