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
Redirect Changing Page Url But Not Rendering New Page
Convert Image from Url to Base64
Filter Json Object Array on Multiple Values or Arguments JavaScript
How to Add Active Class/State for Li Element in Reactjs
Detect If User Is Using Webview for Android/Ios or a Regular Browser
How to Clear the Focus in JavaScript
Force Download an Image from Src Using JavaScript
Getting Null Value in List When Passing by Ajax Call to MVC Controller
How to Merge Two Json Object Values by Id With Plain JavaScript (Es6)
Angularjs:Ng-Model Binding Not Updating When Changed With Jquery
How to Add or Update a Query String Parameter
How to Get the Value by a Key from a Super Nested Json
Getting Name of Parent Object from Value of Object
How to Check Whether Session Is Null or Not in JavaScript
Bootstrap Slides Lagging While Using Multi Item Carousel Using Angular 6