我是Spring新手,正在将Spring 2.5网络应用升级到3.2.3。我在应用程序的首页导航时出错。 错误是bean类的Invalid属性'commandClass'。这个网络应用已经运行了大约5年,所以 问题必须是Spring 2.5到3.2的变化。我必须有一些错误,有任何想法吗?
完整错误是:
Error creating bean with name '/new_candidate.html' defined in ServletContext resource [/WEB-INF/webapp-servlet.xml]:
设置属性值时出错; 嵌套异常是org.springframework.beans.NotWritablePropertyException: bean类[org.myorg.app.web.ScoreChangeController]的属性'commandClass'无效: Bean属性“commandClass”不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?
这是来自webapp-sevlet.xml的bean def:
<bean name="/new_candidate.html" class="org.myorg.app.web.ScoreChangeController" scope="session">
<property name="commandClass" value="org.myorg.app.model.Database"/>
<property name="formView" value="generic"/>
<property name="candidateManager" ref="candidateManager"/>
</bean>
控制器是:
package org.myorg.app.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.myorg.app.model.Database;
import org.myorg.app.service.CandidateManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.validation.BindException;
import org.springframework.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
public class ScoreChangeController
{
private CandidateManager candidateManager;
protected Object formBackingObject(HttpServletRequest request) throws Exception {
Database defaultDatabase = new Database();
defaultDatabase.setApptNo("16 digits");
defaultDatabase.setAccessionNo("8 digits");
defaultDatabase.setTstPkgId("12345678912345678912345");
return defaultDatabase;
}
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
Database database = (Database)binder.getTarget();
}
public CandidateManager getCandidateManager() {
return candidateManager;
}
public void setCandidateManager(CandidateManager candidateManager) {
this.candidateManager = candidateManager;
}
public ModelAndView onSubmit(HttpServletRequest req, HttpServletResponse res, Object command, BindException errors) throws Exception {
if(WebUtils.hasSubmitParameter(req, "retrieve"))
{
candidateManager.retrieveData((Database)command);
}
return new ModelAndView(new RedirectView("success.jsp"));
}
}
答案 0 :(得分:1)
您正在尝试在commandClass
bean中注入属性ScoreChangeController
,但我无法在该控制器中看到具有该名称的任何属性,而且它也不是任何特定控制器的子类所以也没有继承的机会。
解决方案:从commandClass
bean中删除ScoreChangeController
属性的注入。
<bean name="/new_candidate.html" class="org.myorg.app.web.ScoreChangeController" scope="session">
<property name="formView" value="generic"/>
<property name="candidateManager" ref="candidateManager"/>
或者在ScoreChangeController中创建一个名为as的新属性,并为其创建setter和getter。