How to convert html to a Map or array?

Is post jsp, inside which is generated a specified number of forms
<c:foreach var="inputLine" begin="1" end="${countSprints}">
 <form action="sprints" modelattribute="sprintForm" name="first" method="post">
 <input name="name" type="text" placeholder="Name"><br>
 <input name="phone" type="text" placeholder="Phone"><br>
 <input name="email" type="text" placeholder="email"><br>
</form>
 </c:foreach>


I need to convert in the List> that would have been a sheet with maps of these values
How to convert using Spring?

I searched the forums - found nothing yet
Tried and tried using @RequestParam
@RequestMapping(value = "/create-form", method = RequestMethod.POST)
 public ModelAndView createProject(
 @ModelAttribute("sprintForm") ModelAndView contactForm,
 Model model) {
 ModelAndView m = new ModelAndView("show_project", "contactForm",
contactForm);

 return m;
 }
June 10th 19 at 16:17
1 answer
June 10th 19 at 16:19
Solution
First, the form tag there is no attribute modelAttribute.
Second, as a result of the cycle will be a lot of forms with the same name, which makes the html invalid.
Third, sent at a time can have only one form. Accordingly, the controller will receive only one copy of fields name, phone and email, there is nothing to do List.

It is better to make so:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

...

<form:form modelattribute="formData" method="post">
 <c:foreach items="${formData.sprints}" var="sprint" varstatus="status">
 <input type="text" name="sprints[${status.index}]['name']" placeholder="Name"><br>
 <input type="text" name="sprints[${status.index}]['phone']" placeholder="Phone"><br>
 <input type="text" name="sprints[${status.index}]['email']" placeholder="email"><br>
</c:foreach>
 <input type="submit" value="Save">
</form:form>

public class SprintsForm {
 private List<map<string, string>> sprints;

 public List<map<string, string>> getSprints() {
 sprints return;
}

 public void setSprints(List<map<string, string>> sprints) {
 this.sprints = sprints;
}
}</map<string,></map<string,></map<string,>

@Controller
@RequestMapping("/")
public class FormController {
 @RequestMapping(method = RequestMethod.GET)
 public ModelAndView showForm() {
 SprintsForm formData = new SprintsForm();
 return new ModelAndView("form" , "formData", formData);
}

 @RequestMapping(method = RequestMethod.POST)
 public ModelAndView handleForm(@ModelAttribute SprintsForm formData, BindingResult result) {
 if (result.hasErrors())
 return new ModelAndView("form", "formData", formData);

...
}
}
Almost everything is OK, just a question, what I do wrong:

@RequestMapping(value = "/test",method = RequestMethod.GET)
 public ModelAndView showForm() {
 SprintsForm formData = new SprintsForm();
formData.setSprints(sprints);
 return new ModelAndView("sprint", "formData", formData);
}

 @RequestMapping(value = "/test",method = RequestMethod.POST)
 public ModelAndView handleForm(@ModelAttribute("formData") SprintsForm formData,
 BindingResult result) {
System.out.println(formData);
System.out.println(formData.getSprints());
 List<testsprint> sprints = formData.getSprints();

 if(null != sprints && sprints.size() > 0) {
 ProjectController.sprints = sprints;
}

 return new ModelAndView("show_sprint", "formData", formData);

 }</testsprint>


Yourself jsp:
show_sprint
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<title>Show</title>


<c:foreach items="${contactForm.contacts}" var="contact" varstatus="status">
 </c:foreach><table width="50%">
<tbody><tr>
<th>Name</th>
<th>Lastname</th>
<th>Email</th>
<th>Phone</th>
</tr>
the <tr>
<td>${sprint.name}</td>
<td>${sprint.phone}</td>
<td>${sprint.email}</td>
</tr>

</tbody></table>
<br / >


<input type="button" value="Back" onclick="javascript:history.back()">


sprint
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8" %>


<title>Sprint</title>


<%--@elvariable id="formData" type="main.com.netcracker.project.controllers.SprintsForm"--%>
<form:form modelattribute="formData" method="post">
 <c:foreach items="${formData.sprints}" var="sprint" varstatus="status">
 <input type="text" name="sprint[${status.index}]['name']" value="${sprint.name}" placeholder="Name"><br>
 <input type="text" name="sprint[${status.index}]['phone']" value="${sprint.phone}" placeholder="Phone"><br>
 <input type="text" name="sprint[${status.index}]['email']" value="${sprint.email}" placeholder="email"><br>
</c:foreach>
 <input type="submit" value="Save">
</form:form>




In the post method within the controller it comes null, nothing comes back from first jsp - ova.Hilll58 commented on June 10th 19 at 16:22
the parameter formData is null? - samir39 commented on June 10th 19 at 16:25
no , formData.getSprints() -> returns null - ova.Hilll58 commented on June 10th 19 at 16:28
try name="sprint... fix for name="sprints... - samir39 commented on June 10th 19 at 16:31
inside sprint.jsp?
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8" %>


<title>Sprint</title>


<%--@elvariable id="formData" type="main.com.netcracker.project.controllers.SprintsForm"--%>
<form:form modelattribute="formData" method="post">
 <c:foreach items="${formData.sprints}" var="sprint" varstatus="status">
 <input type="text" name="sprints[${status.index}]['name']" value="${sprint.name}" placeholder="Name"><br>
 <input type="text" name="sprints[${status.index}]['phone']" value="${sprint.phone}" placeholder="Phone"><br>
 <input type="text" name="sprints[${status.index}]['email']" value="${sprint.email}" placeholder="email"><br>
</c:foreach>
 <input type="submit" value="Save">
</form:form>

- ova.Hilll58 commented on June 10th 19 at 16:34
yeah - samir39 commented on June 10th 19 at 16:37
Dada, just do it only falls 500 error, now it returns this place not collection, and the object
HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'sprints[0][email]' of bean class [main.com.netcracker.project.controllers.SprintsForm]: Property referenced in indexed property path 'sprints[0][email]' is neither an array nor a List nor a Map; returned value was [main.com.netcracker.project.controllers.TestSprint@d68ca83] - ova.Hilll58 commented on June 10th 19 at 16:40
Now this kind of form:
import java.util.List;

public class SprintsForm {

 private List<testsprint> sprints;

 public List<testsprint> getSprints() {
 sprints return;
}

 public void setSprints(List<testsprint> sprints) {
 this.sprints = sprints;
}
}</testsprint></testsprint></testsprint>
- ova.Hilll58 commented on June 10th 19 at 16:43
judging by the error (and code) you are not a List<map<string, string>></map<string,>as it was in the original statement, and List<testsprint></testsprint>. Accordingly, instead of sprints[${status.index}]['name'] should be sprints[${status.index}].name, if I understand correctly the structure of the class TestSprint. - samir39 commented on June 10th 19 at 16:46
Sergey , thank you very much! I understand how it works inside, now returns the correct number :)) - ova.Hilll58 commented on June 10th 19 at 16:49
always please :) - samir39 commented on June 10th 19 at 16:52
Sergey, I'm sorry, but the question is still one small...
On the page it was necessary to transfer two different types of objects: Sprint Workers
Now the entry looks like this, it works, but it is necessary now to get that data back in the POST request. How to pull out both? they were placed there through the Map<> which has an attribute called "model". Here on this attribute returns a normal Sprint, but the second returns null.

Mapou passed via a POST request, since there had previously volunteered another request:
POST Map
@RequestMapping(value = "/create", method = RequestMethod.POST)
 public ModelAndView ProjectSizePost(
 @RequestParam("countSprints") Integer countSprints,
 @RequestParam("countWorkers") Integer countWorkers,
 Model model) {
 numberOfSprint = countSprints;
 numberOfWorkers = countWorkers;
 Map<string, object> models = new HashMap<>();

 SprintsForm formSprintData = new SprintsForm();
 List<sprintformdata> sprints = createEmptyList(numberOfSprint);
formSprintData.setSprints(sprints);

 WorkPeriodForm workPeriodForm = new WorkPeriodForm();
 List<workperiodformdata> workers = createEmptyList(numberOfWorkers);
workPeriodForm.setWorkers(workers);

 models.put("formSprintData", formSprintData);
 models.put("formWorkPeriodData", workPeriodForm);

 return new ModelAndView("project/create-form", "model", models);
 }</workperiodformdata></sprintformdata></string>
POST get Map
@RequestMapping(value = "/create-form", method = RequestMethod.POST)
 public String createProject(
 @RequestParam("projectId") Integer id,
 @RequestParam("name") String name,
 @RequestParam("startDate") String startDate,
 @RequestParam("endDate") String endDate,
 @RequestParam("projectStatus") OCStatus projectStatus,
 @RequestParam("projectManagerId") projectManagerId Integer,
 @ModelAttribute("model") SprintsForm sprintsForm,
 @ModelAttribute("model") WorkPeriodForm workPeriodForm) {

 List<sprintformdata> sprints =sprintsForm.getSprints();
 List<workperiodformdata> workers =workPeriodForm.getWorkers();

 MapperDateConverter mdc = new MapperDateConverter();

 Project project = new Project.ProjectBuilder()
.projectId(BigInteger.valueOf(id))
.name(name)
.startDate(mdc.convertStringToDate(startDate))
.endDate(mdc.convertStringToDate(endDate))
.build();
project.setProjectManagerId(BigInteger.valueOf(projectManagerId));
project.setProjectStatus(projectStatus);
projectDao.createProject(project);

// List<sprintformdata> sprintFormDataList = formSprintData.getSprints();
// List<workperiodformdata> workPeriodFormDataList = formWorkPeriodData
// .getWorkers();

 return "response_status/success";
 }</workperiodformdata></sprintformdata></workperiodformdata></sprintformdata>
create-form.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8" %>



 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Create Project</title>


<div align="center">
 <form action="/project/create-form" method="post" name="/project/create-form" commandname="projectForm">
 <h2>CreateProject</h2><form:form modelattribute="model" method="post">
 <c:foreach items="${model.formSprintData.sprints}" var="sprint" varstatus="status">
 <input type="text" name="sprints[${status.index}].name" value="${sprint.name}" placeholder="Name"><br>
 <input type="text" name="sprints[${status.index}].startDate" value="${sprint.startDate}" placeholder="Start Date"><br>
 <input type="text" name="sprints[${status.index}].plannedEndDate" value="${sprint.plannedEndDate}" placeholder="End date"><br>
<br / >
</c:foreach>
 </form:form>the<form:form modelattribute="model" method="post">
 <c:foreach items="${model.formWorkPeriodData.workers}" var="worker" varstatus="status">
 <input type="text" name="workers[${status.index}].name" value="${worker.name}" placeholder="Name"><br>
<br / >
</c:foreach>
 </form:form><input type="submit" value="Create"><table border="0">
<tbody><tr>

</tr>
the <tr>
 <td>Project Id:</td>
 <td><input type="text" name="projectId" size="70" value="${projectId}"></td>
</tr>
the <tr>
 <td>Project Name:</td>
 <td><input type="text" name="name" size="70" value="${projectName}"></td>
</tr>
the <tr>
 <td>StartDate (mm/dd/yyyy):</td>
 <td><input type="text" name="startDate" size="70" value="${startDate}"></td>
</tr>
the <tr>
 <td>EndDate (mm/dd/yyyy):</td>
 <td><input type="text" name="endDate" size="70" value="${endDate}"></td>
</tr>
the <tr>
<td>Status:</td>
 <td><input type="text" name="projectStatus" size="70" value="${status}"></td>
</tr>
the <tr>
 <td>Project Manager:</td>
 <td><input type="text" name="projectManagerId" size="70" value="${pmId}"></td>
</tr>
the <tr>
<td>Sprints</td>

</tr>
the <tr>
<td>Workers:</td>


</tr>
the <tr>

</tr>
</tbody></table>
</form>
</div>


- ova.Hilll58 commented on June 10th 19 at 16:55
as I already wrote, to go at once on a form. To solve it, if we combine both forms into one and have the second field in the DTO. But this is a bad decision, the controller receiving data of two different types violates the principle of single responsibility. - samir39 commented on June 10th 19 at 16:58
just so that the Project object, which is collected from this jsp, the inside should accommodate and List and List
And just like then together on one page that you could fill both objects with a single page? Or should I do so that it was a different page? - ova.Hilll58 commented on June 10th 19 at 17:01
better different. - samir39 commented on June 10th 19 at 17:04
Got it, thanks - ova.Hilll58 commented on June 10th 19 at 17:07

Find more questions by tags SpringJSPJava