2011-04-22 51 views
0

我对Spring的注释和JSTL相当陌生。如何将varStatus计数从JSP传递到控制器?

我试图从JSP向Spring Controller传递索引计数的值。但是,我不知道我该怎么做。我无法将此计数设置为路径变量或请求参数。 JSP中有没有将模型中的属性设置为特定值的方法?

示例代码(来自here

... 
<form:form modelAttribute="${questionForm}" ... > 
<%-- render HTML for your question, etc. --%> 
${questionForm.question} 
... 
</p> 

<%-- below list your answer fields (your collection) --%> 
     <c:forEach var="answer" items="${questionForm.answers}" varStatus="counter"> 
     <%-- display your single answer field (text area) here, 
     each element of your list may be accessed as ${answer}, 
     and you can also access the index of the element in the list via ${counter.index} --%> 

     </c:forEach> 
     ... other fields, submit buttons, etc. 
</form:form> 
... 

在这个例子中我可以设置${counter.index}到一个属性在模型{questionForm}

回答

2

您误解了JSP正在做什么。

Spring控制器生成一个完整的模型,并将其转发给JSP。 JSP然后从模型中提取数据并呈现它。

JSP没有,也不能“回调”到控制器。 JSP需要完成其工作的所有内容都应该事先由控制器提供给模型。

<c:forEach>中的注释为您提供了完成练习所需的所有线索。

相关问题