I am fairly new to Spring with annotations and JSTL.
I am trying to pass the value of index count from JSP to Spring Controller. However, I do not know how I can do so. I cannot set this count as a path variable or a request parameter. Is there a way in JSP to set a property in the Model to a particular value?
Sample code (from 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>
...
In this example can I set ${counter.index} to a property in Model {questionForm} ?
You’re misunderstanding what the JSP is doing.
The Spring controller generates a complete model, which is forwarded to the JSP. The JSP then extracts the data from the model and renders it.
The JSP does not, and cannot, “call back into” to the controller. Everything the JSP needs to do its job should be out into the model by the controller in advance.
The comments inside the
<c:forEach>are giving you all the clues you need to complete the exercise.