【问题标题】:Not able to get values from object within JSP无法从 JSP 中的对象获取值
【发布时间】:2014-12-17 14:08:24
【问题描述】:

尝试从我的 jsp 中的项目获取值时出现以下异常。

以下是我的错误:

SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/Dashboard] threw exception [An exception occurred processing JSP page /WEB-INF/jsp/chartListFromDB.jsp at line 9

6:      <c:if test="${not empty chart_items}"></c:if>
7: 
8:      <c:forEach var="chartList" items="${chart_items}">
9:          <li><a href="/Dashboard/chart/displayChart/${chart_items.chartId}">${chart_items.chartName}</a>
10:             </li>
11:         </c:forEach>
12:     </ul>


Stacktrace:] with root cause
java.lang.NumberFormatException: For input string: "chartId" at java.lang.NumberFormatException.forInputString(Unknown Source)

这是我的控制器

@RequestMapping(value = "/availableChartsFromDB", method = RequestMethod.GET)
public String availableChartsFromDB(ModelMap model) {
    IChartBehaviour chartService = new ChartService();
    List<Chart> chart_items = chartService.getAvilableChartsFromDB();
    model.addAttribute("chart_items", chart_items);
    return "chartListFromDB";
}

这是我的 JSP

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@include file="header.jsp"%>
<div id="content_main">
<h3>Chart Selector ...</h3>
<ul>
    <c:if test="${not empty chart_items}"></c:if>

    <c:forEach var="chartList" items="${chart_items}">
        <li><a href="/Dashboard/chart/displayChart/${chart_items.chartId}">${chart_items.chartName}</a>
        </li>
    </c:forEach>
</ul>
</div>
<%@include file="footer.jsp"%>

【问题讨论】:

    标签: jsp spring-mvc jstl


    【解决方案1】:

    使用 chartList.chartId。 chartList 是chart_items 列表中的迭代器变量。

    【讨论】:

    • 感谢您的 cmets。我已尝试使用 chartList.chartId,但返回相同的错误。
    【解决方案2】:

    所以经过更多尝试并在@Rahul Vedpathak 的初步帮助下 我现在已经能够修复这个错误了。

    正如 Rahul 所指出的 - 我最初并没有使用迭代器变量 我更正了这个错误,但仍然出现同样的异常。

    第二个问题是我试图通过引用而不是 getter 方法来访问 Chart 对象。

    通过改变

    chartList.chartId
    

    chartList.getChartId()
    

    这解决了我的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 2021-05-19
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多