在春天发布多个表单记录

时间:2013-08-17 11:34:44

标签: java spring spring-mvc

我对spring框架很新,我遇到了以下问题: 我试图使用Spring MVC 3.0在单个帖子请求中插入多个记录 我成功绑定了List对象,它在JSP上填充,当我提交时 表单请求到达控制器方法(post)但返回的对象不包含正确的值,其打印为null。

我的代码如下:

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 + "]";
}
}

2 个答案:

答案 0 :(得分:1)

发布方法

需要@ModelAttribute
@RequestMapping(method = RequestMethod.POST)
public String postForm(@ModelAttribute("teamBean") TeamBean teamBean) {
    System.out.println("DynaminFormController.postForm()");
    System.out.println(teamBean);//printing null
    return "view";
}

答案 1 :(得分:0)

我已经解决了我的问题,问题出在spring的输入标签上,我不知道为什么它不起作用但只是用html的输入标签替换f:输入,尽管你可能无法从控制器获取填充值但是没关系。

     <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>