我有简单的应用程序来管理橄榄球队和比赛。我正在使用JPA,格式为editMatch.jsp,我有属性team_1,team_2(班级团队的实例)从列表中选择团队。问题在于编辑匹配时,team_1和team_2不在列表中选择,并且在提交错误消息之后是:属性team_1抛出异常;嵌套异常是java.lang.NullPointerException。在控制器中,我绑定team_1,team_2,并且我认为错误在表单的绑定和初始化之间。spring-mvc + jpa:数据绑定
editMatch.jsp
<form:select path="team_1">
<form:options items="${teamList}" itemLabel="name" itemValue="id"/>
</form:select>
EditMatchController
public class EditMatchController extends SimpleFormController {
private MatchManager manager;
public EditMatchController() {}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
Match match = (Match)binder.getTarget();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try{
binder.registerCustomEditor(Date.class, "datum", new CustomDateEditor(sdf, false));
} catch(Exception e){}
binder.registerCustomEditor(Team.class, new TeamPropertyEditor());
binder.registerCustomEditor(Team.class, new TeamPropertyEditor());
}
@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
Map<Object, Object> dataMap = new HashMap<Object, Object>();
dataMap.put("teamList", manager.getTeams());
return dataMap;
}
@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
int idMatch = Integer.parseInt(request.getParameter("id"));
Match match_d = manager.getMatchById(idMatch);
if (match_d == null) {
throw new GenericException("Neplatný záznam.");
}
return match_d;
}
@Override
protected ModelAndView onSubmit(
HttpServletRequest request,
HttpServletResponse response,
Object command,
BindException errors) throws Exception {
Match match = (Match)command;
manager.updateMatch(match);
RedirectView redirect = new RedirectView(getSuccessView());
return new ModelAndView(redirect).addObject("message", match);
}
public void setManager(MatchManager manager) {
this.manager = manager;
}
}
TeamPropertyEditor
public class TeamPropertyEditor extends PropertyEditorSupport {
private MatchManager manager;
public void setManager(MatchManager manager) {
this.manager = manager;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text != null && text.length() > 0) {
try {
Team team = this.manager.getTeamById(new Integer(text));
super.setValue(team);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException();
}
} else {
super.setValue(null);
}
}
@Override
public String getAsText() {
Team team = (Team) super.getValue();
return (team != null ? (team.getId()+"").toString(): "");
}
}
编辑:
errors.getFieldError( “TEAM_1”):在对象 '匹配' 现场 'TEAM_1'
字段错误:拒绝值[6];代码[methodInvocation.match.team_1,methodInvocation.team_1,methodInvocation.model.Team,methodInvocation];参数[org.springframework.context.support.DefaultMessageSourceResolvable:codes [match.team_1,team_1];参数[];预设讯息[team_1]];默认讯息[Property'team_1'引发异常;嵌套的例外是显示java.lang.NullPointerException]
请显示完整的堆栈追踪。 – axtavt 2010-10-23 12:03:39