How to link an object inside a collection of lists of a command object in Spring MVC

My command object has a list of objects. I want to associate a text box with an attribute of an object inside this list. Can this be done in Spring MVC?

Command Object Class

public class SubDevisonDto { private String devId; private List subDevisions; 

Subdevision Feature Class Listed

 public class SubDivison implements Serializable{ private String subDivisonName; private String createdBy; private String createdDate; private String developerID; private List users; 

I want the text field to have a value set for the subDivisonName field.

I wrote Spring MVC tags like this.

 <spring:bind path="subdivisondto.subDevisions[0].subDivisonName"> <span class="formw"> <input name="subDivisonName" type="text" style="width:350px;" /> </span> </spring:bind> 

Just for testing purposes, I gave it as 0. If it works, I can do it for a variable. My requirement: I have to allow the user to dynamically add objects of subordinate vision. So, initially, when the page loads, I just show one text box. I will give him a button to add if he wants to add more. I will dynamically generate text fields when he clicks the add button. After that I have to submit the list form.

This JSP code gives me an error. It says:

org.springframework.beans.NullValueInNestedPathException

Is there anyway for me to do this in JSP code?

+4
source share
2 answers

I found the answer to my question. But this is not a solution for my requirement, since I need to implement a dynamic list. but I found a solution for this question.

As I understand it, the first time we have to send data from the back to bind input elements. I have not found a way to bind form elements that accept input without sending list data from the end. But when we send data and bind elements, we can enter data from these elements. Therefore, I think, in order to bind an element in such a situation, we must first send the data. Correct me if this statement is incorrect. Because it would be a more suitable solution for me.

We need to use a lazy list, and the jsp code will be changed.

Your command class object must be created as follows.

 import org.apache.commons.collections.list.LazyList; import org.apache.commons.collections.FactoryUtils; public class SubDevisonDto { private String devId; private List subDevisions = LazyList.decorate( new ArrayList(), FactoryUtils.instantiateFactory(SubDivison.class)); 

The JSP code should look like this.

 <c:forEach items="${subs.subDevisions}" var="obj" varStatus="gridRow"> 

Linking an input item text field

 <spring:bind path="subdivisondto.subDevisions[${gridRow.index}].subDivisonName"> <span class="formw"><input name="<c:out value="${status.expression}"/>" type="text" style="width:350px;" /> 

binding an input element. This input element contains a list.

 <spring:bind path="subs.subDevisions[${gridRow.index}].users"> <c:forEach items="${obj.users}" var="dependenttwo" varStatus="dependentRowtwo"> <li> <input name="<c:out value="${status.expression}"/>" type="checkbox" class="users" value="<c:out value="${dependenttwo}"/>"/> <c:out value="${dependenttwo}"/> </li> </c:forEach> </spring:bind> `subs` is a map key name. the value for this key `subs` is a list of my DTO objects which named as `SubDevisonDto ` 

This code works great for me.

Thanks for the support provided.

+2
source

In dto:

 private List<SubDivision> SubDivisions = new AutoPopulatingList<SubDivision>(new SubDivisionFactory()); 

and the factory will look something like this:

 public class SubDivisionFactory implements AutoPopulatingList.ElementFactory<SubDivision> { public String createElement(int index) { SubDivision subDivision = new SubDivision(); return subDivision; } } 

using AutopopulatingList from spring. And your jsp will look the same, you can iterate as much as you want.

+1
source

Source: https://habr.com/ru/post/1383010/


All Articles