我有簡單的應用程序來管理橄欖球隊和比賽。我正在使用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