下面的jstl代码应根据此控制器方法解析json文件:
控制器
@RequestMapping(value="listagem.json", method=RequestMethod.GET)
@PreAuthorize("hasPermission(#user, 'listagem_'+#this.this.name)")
public ModelAndView listagem_json(@RequestParam("pagina") String pagina, @RequestParam("items") String items, @RequestParam("ordem") String ordem) {
ModelAndView mav = new ModelAndView();
mav.setViewName("listagem");
mav.addObject("lista", serv.listagem(pagina, items, ordem));
mav.addObject("map", serv.getListaAtributos());
return mav;
}
jstl代码
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>
<json:object>
<json:array name="item" var="item" items="${lista}">
<json:object>
<c:forEach var="attr" items="${map}">
<c:choose>
<c:when test="${empty attr.value}">
<json:property name="${attr.key}" value="${item[attr.key]}"/>
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${item.class.name == 'List'}">
<json:array name="${attr.key}" var="attr2" items="${item[attr.key]}">
<json:object>
<c:forEach var="attr3" items="${attr.value}">
<json:property name="${attr3}" value="${attr2[attr3]}"/>
</c:forEach>
</json:object>
</json:array>
</c:when>
<c:otherwise>
<json:array name="${attr.key}">
<json:object>
<c:forEach var="attr2" items="${attr.value}">
<c:set value="${attr.key}" var="object"/>
<json:property name="${attr2}" value="${object}.${attr2}"/>
</c:forEach>
</json:object>
</json:array>
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
</c:forEach>
</json:object>
</json:array>
</json:object>
但是当我运行应用程序并请求json文件时,会发生以下错误:
org.apache.jasper.JasperException: /WEB-INF/json/listagem.jsp (line: 13, column: 9) "${item.class.name == 'List'}" contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${item.class.name == 'List'}]
任何人都可以告诉我写这个表达式${item.class.name == 'List'}
的正确方法是什么来实现预期的结果?
答案 0 :(得分:3)
您可以使用以下两种方法之一:
${item.getClass().simpleName == "List"}
${item['class'].simpleName == "List"}
有关详细信息,请参阅此post