在Tomcat 7上尝试执行Spring MVC应用程序时遇到了麻烦。
这是我的 DSLRServletController :
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.util.*;
@Controller
public class DSLRServletController extends HttpServlet {
static Logger logger = Logger.getLogger(DSLRServletController.class);
private DSLRDAO dslrDAO;
/* request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
List list = getActions(request, response);
response.setContentType("text/html; charset=UTF-8");*/
//@Autowired // need?
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=<timestamp> GET
*/
@RequestMapping(value = "/s", method = RequestMethod.GET)
public String showHTMLResponse(@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; HOW?
//getServletConfig().getServletContext().getRequestDispatcher("/dslrs.jsp").forward(request, response);
return "dslrs";
}else{
//show_this_dslr;
HashMap<String,Object> dslrHashMap = getDSLRById(paramId);
dslr = (DSLR) dslrHashMap.get("dslr");
dslrForm = (DSLRForm)dslrHashMap.get("dslrForm");
dslrErrors = (HashMap)dslrHashMap.get("dslrErrors");
if(dslr != null){
// getServletConfig().getServletContext().getRequestDispatcher("/dslr.jsp").forward(request, response);
return "dslr";
}
else{
// getServletConfig().getServletContext().getRequestDispatcher("/error.jsp").forward(request, response);
return "error";
}
}
}
@RequestMapping(value = "/s", method = RequestMethod.POST)
public String showHTMLResponsePOST(@ModelAttribute("dslrs") DSLR dslrs[],
@ModelAttribute("dslrErrors") HashMap<?,?> dslrErrors,
@ModelAttribute ("dslrform") DSLRForm dslrForm,
@RequestParam("id") String paramId,
@RequestParam("action") String paramAction,
@RequestParam("dslr_model") String paramModel,
@RequestParam("price") String paramPrice,
@RequestParam("description") String paramDescription,
Model model){
int iStatusCode = 0;
if(paramAction.equals("save") )
iStatusCode = saveDSLR(paramId, paramModel, paramPrice, paramDescription, dslrErrors, dslrForm); // POST
return "dslrs";
}
private int saveDSLR(String paramId,
String paramModel,
String paramPrice,
String paramDescription,
HashMap<?,?> context_dslrErrors,
DSLRForm context_dslrForm
) {
int byte0 = 1;
try {
DSLRValidaor dslrValidaor = new DSLRValidaor();
DSLRForm dslrForm = new DSLRForm();
dslrForm.setDslrId(paramId);
dslrForm.setModel(paramModel);
dslrForm.setPrice(paramPrice);
dslrForm.setDescription(paramDescription);
HashMap hashmap = dslrValidaor.Validate(dslrForm);
if(hashmap.size() > 0) {
context_dslrForm = dslrForm;
context_dslrErrors = hashmap;
byte0 = -1;
} else{
DSLRDAO planedao = new DSLRDAO();
DSLR dslr = new DSLR();
dslr.setDslrId(Integer.parseInt(paramId));
dslr.setModel(paramModel);
dslr.setPrice(Integer.parseInt(paramPrice));
dslr.setDescription(paramDescription);
planedao.update(dslr);
}
}
catch(Exception exception)
{
logger.error((new StringBuilder()).append("DSLRServlet.saveDSLR():").append(exception.getMessage()).toString());
byte0 = -1;
}
return byte0;
}
private DSLR[] getAllDslrs(){
DSLR adslrs[] = null;
try
{
DSLRDAO DSLRDAO = new DSLRDAO();
adslrs = (DSLR[])DSLRDAO.findAll();
}
catch(Exception exception)
{
logger.error((new StringBuilder()).append("PlaneServlet.getAllPlanes():").append(exception.getMessage()).toString());
}
// request.setAttribute("dslrs", adslrs);
return adslrs;
}
private HashMap<String, Object> getDSLRById(String s)
{
HashMap<String,Object> map = new HashMap<String, Object>();
DSLR dslr = null;
try {
int i = Integer.parseInt(s);
DSLRDAO DSLRDAO = new DSLRDAO();
dslr = (DSLR)DSLRDAO.findById(i);
DSLRForm dslrForm = new DSLRForm();
dslrForm.setDslrId(Integer.toString(dslr.getDslrId()));
dslrForm.setModel(dslr.getModel());
dslrForm.setPrice(Integer.toString(dslr.getPrice()));
dslrForm.setDescription(dslr.getDescription());
map.put("dslr", dslr);
map.put("dslrform", dslrForm);
map.put("dslrErrors", new HashMap());
// request.setAttribute("dslrform", dslrForm);
// HashMap hashmap = new HashMap();
// request.setAttribute("dslrErrors", hashmap);
}
catch(Exception exception)
{
logger.error((new StringBuilder()).append("DSLRServlet.getDSLRById():").append(exception.getMessage()).toString());
}
return map;
}
}
DSLRServletController-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-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="main.java.com.springapp.mvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<!--<bean name="/s" class="main.java.com.springapp.mvc.controller.DSLRServletController"/>-->
</beans>
DSLRDAO:
package main.java.com.springapp.mvc.dao;
import main.java.com.springapp.mvc.model.DSLR;
import main.java.com.springapp.mvc.util.DbUtil;
import org.apache.log4j.Logger;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DSLRDAO {
Connection connection = null;
// static Logger logger = Logger.getLogger(ttu/idu0200/db/PlaneDAO);
public DSLRDAO() {
connection = DbUtil.getConnection();
}
public DSLR[] findAll() {
// connection = DbUtil.getConnection();
List<DSLR> dslrs = new ArrayList<DSLR>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from DSLR");
while (rs.next()) {
DSLR dslr = new DSLR();
dslr.setDslrId(rs.getInt("dslrid"));
dslr.setModel(rs.getString("model"));
dslr.setPrice(rs.getInt("price"));
dslr.setDescription(rs.getString("description"));
dslrs.add(dslr);
}
} catch (SQLException exception) {
Logger.getLogger(DSLRDAO.class).error((new StringBuilder()).append("DSLRDAO:").append(exception.getMessage()).toString());
}finally {
// DbUtil.close();
}
return dslrs.toArray(new DSLR[dslrs.size()]);
}
public DSLR findById(int dslrId){
// connection = DbUtil.getConnection();
DSLR dslr = new DSLR();
try{
PreparedStatement preparedStatement = connection.prepareStatement("select * from DSLR where dslrid=?");
preparedStatement.setInt(1, dslrId);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
dslr.setDslrId(resultSet.getInt("dslrid"));
dslr.setModel(resultSet.getString("model"));
dslr.setPrice(resultSet.getInt("price"));
dslr.setDescription(resultSet.getString("description"));
}
} catch (SQLException exception) {
Logger.getLogger(DSLRDAO.class).error((new StringBuilder()).append("DSLRDAO:").append(exception.getMessage()).toString());
} finally {
// DbUtil.close();
// connection = null;
}
return dslr;
}
public void update(DSLR dslr){
// connection = DbUtil.getConnection();
try{
PreparedStatement preparedStatement =
connection.prepareStatement("update dslr set model=?, price=?, description=?" + " where dslrid=?");
preparedStatement.setString(1, dslr.getModel());
preparedStatement.setInt(2, dslr.getPrice());
preparedStatement.setString(3, dslr.getDescription());
preparedStatement.setInt(4, dslr.getDslrId());
preparedStatement.executeUpdate();
} catch (SQLException exception) {
Logger.getLogger(DSLRDAO.class).error((new StringBuilder()).append("DSLRDAO:").append(exception.getMessage()).toString());
} finally {
// DbUtil.close();
// connection = null;
}
}
}
的web.xml:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
错误:
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;.<init>()
type Exception report
message 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;.<init>()
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: 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;.<init>()
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
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;.<init>()
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:108)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:770)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:363)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:439)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:427)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>()
java.lang.Class.getConstructor0(Class.java:2810)
java.lang.Class.getDeclaredConstructor(Class.java:2053)
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:770)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:363)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:439)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:427)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
UPADTE4:
使用默认构造函数的新 DSLR类仍会返回相同的错误
答案 0 :(得分:2)
在弹簧中实现依赖注入是常见的例外。
在您的情况下,您没有将属性和ref bean放在xml配置中。
<bean name="s" class="main.java.com.springapp.mvc.controller.DSLRServletController"/>
<property name="dslrDAO" ref="dslrDAO" />
</bean>
@Autowired是用于它的注释。
答案 1 :(得分:0)
根本原因表明问题与DSLR类有关。
[[Lmain.java.com.springapp.mvc.model.DSLR;]:没有默认构造函数 实测值;
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;.<init>()
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:108)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:770)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:363)