Spring无法使用ModelAttribute处理列表 [英] ModelAttribute not working with lists in spring

查看:309
本文介绍了Spring无法使用ModelAttribute处理列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ModelAttribute绑定列表.该列表包含Transaction类型的对象,每个对象都包含transactionid(int类型).这是我的控制器代码:

I want to bind a List using ModelAttribute. The list contains objects of type Transaction each of which contains transactionid (type int). This is my controller code:

@RequestMapping(value = "/approvecreditdebit.do", method = RequestMethod.POST)
    public ModelAndView doActions(HttpServletRequest request,
            @ModelAttribute("clc") Clc transactionList, BindingResult result,
            ModelMap model) {
        /*
         * switch (action) { case "approve":
         */

        System.out.println("Obj = " + transactionList.getClass());
        System.out.println("Val = " + transactionList.getTransactionList());
        Users users = new Users();
        return new ModelAndView("internal","internal",users);
    }

这是我的jsp代码:

<form:form action="approvecreditdebit.do" method="POST"
    modelAttribute="clc">

    <table border="1">
        <tr>
            <th>no</th>
            <th>ID</th>
        </tr>

        <c:forEach items="${clc.transactionList}"
            var="transaction" varStatus="status">
            <tr>
                <td>${status.index}</td>
                <td><input name = "transaction[${status.index}].transactionId"
                        value="${transaction.transactionId}" /></td>
            </tr>
        </c:forEach>
    </table>

    <br>
    <br>

    <center>
        <input type="submit" value="approve" />
    </center>


</form:form>

这是Clc类:

public class Clc{

    private List<Transaction> transactionList;

    public List<Transaction> getTransactionList() {
        return transactionList;
    }

    public void setTransactionList(List<Transaction> transactionList) {
        this.transactionList = transactionList;
    }    
}

transactionList的值未设置为从表单接收的值.我收到以下错误:

The value of transactionList is not being set to the values received from the form. I receive the following error:

Request processing failed; nested exception is java.lang.NullPointerException

我尝试在Google上搜索解决方案,并从stackoverflow获得了很多解决方案,但是似乎都没有.

I tried searching for the solution on google and got a lot of solutions from stackoverflow but none of them seem to work.

推荐答案

尝试类似方法(注意使用<form:input>).我只是在一个简单的Spring MVC应用程序上尝试了它,并且它可以工作(列表不为null,并且当我尝试通过POST方法访问它时,它具有来自表单的值).

Try something like this (notice the use of <form:input>). I just tried it on a simple Spring MVC app and it works (list is not null and has the values from the form when I try to access it in my POST method).

<c:forEach var="transaction" varStatus="status" items="${clc.transactionList}">
    <tr>
        <td>${status.index}</td>
        <td><form:input path="transactionList[${status.index}].id" /></td>
    </tr>
</c:forEach>

这篇关于Spring无法使用ModelAttribute处理列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆