How to Pass Array Variable from View to Controller in Spring MVC

Passing Array from Jsp to Controller [Spring MVC]

You could convert the javascript array to JSON using following code.

JSON.stringify(yourArray);

And in controller, accept it as String parameter and then convert it back to array using Jackson library as below:

ObjectMapper mapper = new ObjectMapper();
String [] array = mapper.readValue(jsonString, String[].class):

Hope it helps!

Passing Array of Objects to Controller using Spring MVC

Assuming you're using maven add something like the following to you pom so Jackson is in the classpath:

   <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>

As long as you pass the correct json to the controller it will de-serialize it.

To test it you can use the following:

public class JacksonTest {
@Test
public void test1() throws IOException {
String test = "[{\"id\" : null, \"listId\" : 1, \"wwid\" : \"abc123\"}]";
UserAccessListMembers[] userAccessListMemberses = new ObjectMapper().readValue(test, UserAccessListMembers[].class);
for (UserAccessListMembers members : userAccessListMemberses) {
System.out.println(members);
}
}
}

Pass array to ajax request in $.ajax() to controller Spring mvc

change

var b = {
"lab" : labels
"keys": keyArray
};

to

var b = {
"lab" : labels,
"keys": keyArray
};

How to pass a list from the view to the controller in Spring MVC with thymeleaf?

Spring MVC with Thymeleaf does indeed use model attributes to store data for the Thymeleaf pages. This guide shows how to load data from a form into the application. Try wrapping the ArrayList inside of a custom object and access the list via that object as a ModelAttribute rather than working with an ArrayList directly.

The saveEditPerson method should probably be named "saveEditPersons" since you are parsing a list of persons rather than just one.

Spring MVC passing ArrayList back to controller

Thanks to minion, i found the answer

Wrapper:

public class UserListWrapper {

private ArrayList<User> users;

public ArrayList<User> getUsers() {
return users;
}

public void setUsers(ArrayList<User> users) {
this.users = users;
}

Controller:

@Controller
public class AdminController {

@Autowired
private UserDao userDao;

@RequestMapping(value = "/admin", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
UserListWrapper wrapper = new UserListWrapper();
wrapper.setUsers(new ArrayList<User>(userDao.findAll()));
model.addObject("userListWrapper",wrapper);

model.setViewName("admin");
return model;

}

@RequestMapping(value = "admin/remove", method = RequestMethod.POST)
public ModelAndView removeUser(@ModelAttribute(value = "userListWrapper") UserListWrapper userListWrapper) {
ModelAndView model = new ModelAndView();
userDao.removeFlaggedUsers(userListWrapper.getUsers());
model.setViewName("redirect:/admin");
return model;

}

}

View:

<form:form action="/admin/remove" method="POST"  modelAttribute="userListWrapper">
<table class="table table-striped">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Email/login</th>
<th>Profession</th>
<th>Select<th>
</tr>
</thead>
<tbody>
<c:forEach varStatus="us" var="user" items="${userListWrapper.users}" >
<tr>
<td><form:input type="hidden" path="users[${us.index}].firstName"/>${user.firstName}</td>
<td><form:input type="hidden" path="users[${us.index}].lastName"/> ${user.lastName}</td>
<td><form:input type="hidden" path="users[${us.index}].login"/>${user.login}</td>
<td><form:input type="hidden" path="users[${us.index}].profession"/>${user.profession}</td>
<td><form:checkbox path="users[${us.index}].delete" value="${user.delete}"/></td>
<form:input type="hidden" path="users[${us.index}].id"/>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="Delete user(s)" class="btn-danger" />
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form:form>

Thank you!

EDIT: Dont forget to also add the fields you are not displaying.

For example:

If you dont add the id, your delete will not work because the id in the returned User object will be NULL.



Related Topics



Leave a reply



Submit