【发布时间】:2012-06-06 17:08:32
【问题描述】:
我不是 JSTL 和表达式语言的专家,所以我的问题可能很愚蠢......
我正在使用 Spring MVC,并且在我的控制器中有:
@ModelAttribute("export_types")
public ExportType[] getExportTypes() {
return edService.getTypes();
}
ExportType是自定义接口:
public interface ExportType {
String getName();
//...
}
在我的页面中:
<c:forEach var="item" items="${export_types}">
<tr>
<td><input type="checkbox" value="${item.name}"></td>
<td>${item.name}</td>
</tr>
</c:forEach>
但是当我运行我的网络应用程序时,我得到了这个期望:javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String
奇怪的是,异常是on type java.lang.String,而不是ExportType。所以我的问题是:我不能在接口中使用表达式语言吗?
注意 1
edService.getTypes() 返回一个带有接口具体实现的ExportType[] 数组。
为了清楚起见,我有一个实现ExportType 接口的抽象类。具体类型继承自:
public abstract class AbstractExportType implements ExportType {
protected String name;
protected AbstractExportType() {
this.name = this.getClass().getSimpleName();
}
@Override
String getName(){
return this.name;
}
//...
}
注意 2
转发到export.jsp的控制器方法很简单:
@RequestMapping(value = "/export", method = RequestMethod.GET)
public String getExportForm() {
return "jsp/export";
}
【问题讨论】:
-
edService.getTypes();这究竟返回了什么? -
确保不要在某处覆盖
export_types属性。 -
转发到 JSP 的控制器方法是什么样的?
-
使用
${item.class}而不是${item.name}。这将为您提供item的类型。如果确实是String,那么你的问题出在别处。 -
@doublep 一个具体的类可以以不同的方式实现
getName方法...
标签: java jsp spring-mvc jstl el