I am new to spring mvc framework, and I ran into the problem of getting values from a view in a controller. Below is the code.
Here is the jsp:
<form:form action="envDetails" method="POST" commandName="enviromentForm">
<c:forEach items="${enviromentForm.environments}" varStatus="i" var="env">
Name of Environment<c:out value="${(i.index)+1}"/>:
<form:input path="environments[${i.index}].name" type="text"/>
<br>
Path of Environment<c:out value="${(i.index)+1}"/> :
<form:input path="environments[${i.index}].path" type="text"/>
<br><br>
</c:forEach>
<input class="submitStyle" type="submit" value="SUBMIT" />
</form:form>
This is my controller:
@RequestMapping(value="envDetails",method=RequestMethod.GET)
public ModelAndView setBackingForm(HttpServletRequest request) {
EnviromentForm envf=new EnviromentForm();
envf.setProjectName("Test");
for(int i=0;i<2;i++) {
envf.add(new Enviroment());
}
return new ModelAndView("envDetails","enviromentForm",envf);
}
@RequestMapping(value = "envDetails", method = RequestMethod.POST)
public ModelAndView viewFolderInput(
@ModelAttribute("enviromentForm") EnviromentForm enviromentForm,BindingResult binding,WebRequest request, SessionStatus status) {
***
}
This is the EnviromentForm class:
public class EnviromentForm {
private String projectName;
private List<Enviroment> environments;
public EnviromentForm() {
environments = new ArrayList<>();
}
public EnviromentForm(String projectName, List<Enviroment> environments) {
this.projectName = projectName;
this.environments = new ArrayList<>();
this.environments.addAll(environments);
}
public void add(Enviroment env) {
this.environments.add(env);
}
}
and this is the Enviroment class:
public class Enviroment {
private String name;
private String path;
}
Any help is greatly appreciated.
source
share