我正在做一个小型的春季项目,当我尝试显示数据迭代resultList时,我遇到了一个问题,结果列表被设置并且foreach循环超过4次作为项目,但是给出了异常,经过我意识到项目是retrive但是如果它们只是Object而不是porper cast class这是因为当我尝试调用getUrl()方法时崩溃(如果我调用toString工作正常)。
是控制器
@RequestMapping(value = "/search", method = RequestMethod.GET)
public ModelAndView home(ModelMap model,@RequestParam(required=false,value="") String name) {
List<Result> result = googleSearchService.doSearch(name);
modelAndView.addObject("resultList",result);//<- after debub list is full of Result items
return modelAndView;
}
视图
<c:forEach items="${resultList}" var="item">
<li><c:out value="${item.getUrl()}"/></li>
</c:forEach>
结果类是一个内部类:
public class GoogleResults {
private ResponseData responseData;
public ResponseData getResponseData() {
return responseData;
}
public void setResponseData(ResponseData responseData) {
this.responseData = responseData;
}
public String toString() {
return "ResponseData[" + responseData + "]";
}
static class ResponseData {
private List<Result> results;
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
public String toString() {
return "Results[" + results + "]";
}
}
static class Result {
public String url;
private String title;
public String getUrl() {
return url;
}
public String getTitle() {
return title;
}
public void setUrl(String url) {
this.url = url;
}
public void setTitle(String title) {
this.title = title;
}
public String toString() {
return "Result[url:" + url + ",title:" + title + "]";
}
}
}
跟踪:
ava.lang.NullPointerException javax.el.BeanELResolver.invoke(BeanELResolver.java:159) org.apache.jasper.el.JasperELResolver.invoke(JasperELResolver.java:147) org.apache.el.parser.AstValue.getValue(AstValue.java:157) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:187)
答案 0 :(得分:3)
课程Result
应标记为public