【问题标题】:spring-mvc + jpa: data bindingspring-mvc + jpa:数据绑定
【发布时间】:2010-10-23 09:21:28
【问题描述】:

我有一个管理足球队和比赛的简单应用程序。我正在使用 JPA,格式为 editMatch.jsp,我有属性 team_1、team_2(Team 类的实例)用于从列表中选择团队。问题是编辑比赛时,team_1和team_2没有在列表中选择,提交后报错信息是:Property team_1 throw exception;嵌套异常是 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;
}

}

团队属性编辑器

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:代码 [match.team_1,team_1];论据 [];默认消息 [team_1]];默认消息 [Property 'team_1' 抛出异常;嵌套异常是 java.lang.NullPointerException]

【问题讨论】:

  • 请显示完整的堆栈跟踪。

标签: spring jpa binding spring-mvc


【解决方案1】:

你实例化了TeamPropertyEditor,但没有在它上面调用setManager(),所以它的manager字段是null,因此你在尝试调用manager.getTeamById(...)时得到NPE。

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 2012-04-29
    • 2012-12-23
    • 2011-10-23
    • 2015-09-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2014-09-05
    相关资源
    最近更新 更多