Reputation: 465
I am quite new to spring framework and I'm stuck with the following issues: I am trying to insert multiple records on a single post request using Spring MVC 3.0 I successfully bound the List object and it is populating on JSP and when I submit the form the request is reaching on the controller method(post) but the returned object does not contain proper values, its printing null.
My code is as follows:
form.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:form commandName="teamBean" method="post">
<f:input path="players[0].fname" />
<f:input path="players[0].lname" />
<f:input path="players[0].phone" />
<f:input path="players[0].email" />
<input type="submit" value="submit" />
</f:form>
</body>
</html>
DynaminFormController.java
@Controller
@RequestMapping("/form")
public class DynaminFormController {
List<Player> players = new ArrayList<>();
@RequestMapping(method = RequestMethod.GET)
public String getForm(Map<String, TeamBean> map) {
TeamBean teamBean = new TeamBean();
players.add(new Player("dd", "dd", "dd", "dd"));
players.add(new Player("cc", "cc", "cc", "cc"));
teamBean.setPlayers(players);
map.put("teamBean", teamBean);
return "form";
}
@RequestMapping(method = RequestMethod.POST)
public String postForm(TeamBean teamBean) {
System.out.println("DynaminFormController.postForm()");
System.out.println(teamBean);//printing null
return "view";
}
}
TeamBean.java
public class TeamBean {
private List<Player> players;
public List<Player> getPlayers() {
return players;
}
public void setPlayers(List<Player> players) {
this.players = players;
}
@Override
public String toString() {
return "TeamBean [players=" + players + "]";
}
}
Player.java
public class Player {
private String fname;
private String lname;
private String phone;
private String email;
public Player(String fname, String lname, String phone, String email) {
this.fname = fname;
this.lname = lname;
this.phone = phone;
this.email = email;
}
///getters setters...
@Override
public String toString() {
return "Player [fname=" + fname + ", lname=" + lname + ", phone="
+ phone + ", email=" + email + "]";
}
}
Upvotes: 1
Views: 624
Reputation: 465
I have resolved my issue, the problem was with spring's input tag ,i don't know why it was not working but just replace the f:input with html's input tag,though u may not get the populated values from controller but it's fine.
<f:form commandName="teamBean" method="post">
<tr>
<td><input class="dy" name="players[0].fname" /></td>
<td><input class="dy" name="players[0].lname" /></td>
<td><input class="dy" name="players[0].phone" /></td>
<td><input class="dy" name="players[0].email" /></td>
</tr>
<input type="submit" value="submit" />
</f:form>
Upvotes: 0
Reputation: 38583
You need @ModelAttribute
on you Post Method
@RequestMapping(method = RequestMethod.POST)
public String postForm(@ModelAttribute("teamBean") TeamBean teamBean) {
System.out.println("DynaminFormController.postForm()");
System.out.println(teamBean);//printing null
return "view";
}
Upvotes: 1