如何将带有ArrayList的表数据从百里香绑定到控制器类

时间:2019-07-17 19:00:41

标签: javascript html model-view-controller thymeleaf

我正在尝试在百里香叶中实现以下过程(我是百里香叶的新手) 1.导入csv文件并显示给用户 2.向用户显示数据后,用户可以选择更新数据库中的数据

当前,我在将表对象传递给控制器​​类时遇到问题

获取

@RequestMapping(value = "/samplepage", method = RequestMethod.GET)
public String bulkupload(Model model, HttpSession session) {

        AccountService wlaccservice = new AccountService();
        //ArrayList<AccountService> accountentry = new ArrayList<AccountService>();
        Accounts accountentry = new Accounts();

        model.addAttribute("accountentry", accountentry.getAccountentry());
        return "samplepage";

}

HTML (示例页面)

<tr class="table-row"  th:each="account : ${accountentry}">

                <td class="table-data" th:text="${account.accountnumber}"></td>
                <td class="table-data" th:text="${account.noofilc}"></td>

我有带按钮的CSV文件和“更新数据库”按钮

Setter and Getter课程     公共AccountService {

private String accountnumber,noofilc;
    public String getAccountnumber() {
        return accountnumber;
    }
    public void setAccountnumber(String accountnumber) {
        this.accountnumber = accountnumber;
    }
    public String getNoofilc() {
        return noofilc;
    }

    .....
}

数组类

public class Accounts {
    public ArrayList<AccountService> accountentry;

    public ArrayList<AccountService> getAccountentry() {
        return accountentry;
    }

    public void setAccountentry(ArrayList<AccountService> accountentry) {
        this.accountentry = accountentry;
    }

}

POST

@RequestMapping(value = "/samplepage", method = RequestMethod.POST)
public String bulkuploadPost(Model model,
        @ModelAttribute("samplepage") AccountService acctservice,
        @ModelAttribute  Accounts accountentry,
        HttpSession session, @RequestParam("file") MultipartFile file,HttpServletRequest request) {
    String action = request.getParameter("action");



        try {
            if (action.equalsIgnoreCase("Import CSV"))  // This part  to load data into html table accountentry which is work fine
            {
            byte[] bytes= file.getBytes();
            ByteArrayInputStream inputFilestream = new ByteArrayInputStream(bytes);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputFilestream));
            String line = "";
            ArrayList<AccountService> arraylist = new ArrayList<AccountService>();
            while ((line = br.readLine()) != null) {
                TypeWhiteListAccountDetails accountDetails = new TypeWhiteListAccountDetails();
                accountDetails.Tablebulkupdate(line,arraylist); //getting array from here to load it in  accountentry variable 

            }
            br.close();
            accountentry.setAccountentry(arraylist);

        model.addAttribute("accountentry", accountentry.getAccountentry());


            }
            else if (action.equalsIgnoreCase("Start Update")) // after I am having data in accountentry start calling data from html to load in DB
            {

                model.addAttribute("accountentry", accountentry);
                System.out.println(accountentry.getAccountentry()); // always am getting as null for attribute accountentry
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "samplepage";

}

我总是在上面提到的那行中得到空值,请在这一部分帮助我

0 个答案:

没有答案