0

I'm learning Spring MVC + Thymeleaf in school and I have run into an issue with an assignment. As you can see I use th:each to iterate through a list of deliverables. I want the user to enter the grade that they receive for each deliverable that is displayed. The issue is getting access to the grade data when the form posts back to the mapping controller method. I am not sure how to access each deliverable.grade individually. I can get access to the first it seems. Any help is appreciated. Thanks.

<form th:object="${deliverable}" action="#" th:action="@{/process/inputGrades}" method="POST" class="form-horizontal" role="form">

<div class="form-group" th:each="deliverable : ${deliverableList}">
    <p>Deliverable Name<span th:text="${deliverable.name}" class="badge tab-space"></span></p>
    <p>Deliverable Weight<span th:text="${deliverable.weight}" class="badge tab-space"></span></p>

    <h3><span class="label">Grade:</span></h3>
    <input type="text" th:value="${deliverable.grading}" id="grading" name="grading" class="form-control" required="required"/>
</div>

krystofurr
  • 137
  • 1
  • 1
  • 7

1 Answers1

0

<form th:object="${deliverable}"

and th:each="deliverable :

both are deliverable !!!! This may conflict ( I am not sure enough I am trying to help you just). You change the variable name please. Hope it works. Would u kindly share deliverable model or class (attributes)? also

name="grading" will be generated multiple times when list is more than two.In this case it should be unique.

Saiful Azad
  • 1,823
  • 3
  • 17
  • 27
  • Thanks Saiful for the response. If it's unique for each instance created, how do you get access to each one? The Deliverable model basically has 3 private attributes => name, weight and grading , with setters and getters. – krystofurr Nov 01 '15 at 02:40
  • Sorry I can not describe you correctly. Your code ` ` will be generated multiple time . Am I right ? But form POST use unique (key ,value) (name , value) except radio. Debug the controller side which value it is received ? Hope It helps you . – Saiful Azad Nov 01 '15 at 02:48
  • That's alright. I don't completely understand what I'm doing ,haha. So yes, it will generate multiple times with th:value="${deliverable.grading}". How would I proceed to debug the controller side to check what values are being received? I know that I can get the first text field by called deliverable.getGrading() because deliverable is a parameter in the controller method. – krystofurr Nov 01 '15 at 02:55
  • http://stackoverflow.com/questions/17964841/how-to-get-param-in-method-post-spring-mvc – Saiful Azad Nov 01 '15 at 03:05
  • Thanks Saiful. I checked actually in chrome developer tools and I see the form data listed. Every parameter is the same name with different values. http://www.tiikoni.com/tis/view/?id=e5ff324 – krystofurr Nov 01 '15 at 03:58
  • I found using ' request.getParameterValues("grading") ' I can get each value from the POST form data. I'm not sure if this is a good idea though because I can't differentiate each, other than the order that they are in. I suppose somehow I need to name each uniquely based on the deliverable they are associated with? – krystofurr Nov 01 '15 at 04:15
  • Actually it depends on your requirement. If ur grades are fixed (suppose 5) then easily u define 5 separate variables. I am not aware of ur design / requirement eventually. Thank you. – Saiful Azad Nov 01 '15 at 05:18