spring-mvc + jpa:数据绑定

时间:2010-10-23 09:21:29

标签: spring jpa binding spring-mvc

我有简单的应用程序来管理足球队和比赛。我正在使用JPA,以editMatch.jsp的形式,我有属性team_1,team_2(类Team的实例),用于从列表中选择团队。问题是编辑匹配时,team_1和team_2不在列表中选择,并且在提交错误消息后:属性team_1抛出异常;嵌套异常是java.lang.NullPointerException。在控制器中,我绑定了team_1,team_2,我认为错误介于表单的绑定和初始化之间。

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]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:codes [match.team_1,team_1];参数[];默认消息[team_1]];默认消息[属性'team_1'抛出异常;嵌套异常是java.lang.NullPointerException]

1 个答案:

答案 0 :(得分:0)

您实例化TeamPropertyEditor但不要在其上调用setManager(),因此其manager字段为null,因此您在尝试调用{{1}时获得NPE }。