如何从Controller中的JSP获取对象列表

时间:2019-04-15 09:05:27

标签: java spring spring-boot jsp java-ee

我的代码有一些问题。我尝试从控制器中的jsp获取对象列表,然后通过POST将其发送到服务中

当我尝试使用注释@ModelAttribute在控制器中获取列表时,它将返回一个空列表。这是我的控制器类和我的jsp

控制器


    @GetMapping("/OvedraftsCustomer")
    public String getOverdraftsCustomers(ModelMap model){
        List<AccountDTO> overdraftAccounts = overdraftService.getOverdraftAccounts();
        model.addAttribute("overdraftAccounts",overdraftAccounts);
        return "agios/displayOverdraftCustomers";
    }


    @PostMapping("/agioDetails")
    public String getOverDraftDetails(@ModelAttribute("account") ArrayList<AccountDTO> account, BindingResult binding, ModelMap model) throws ParseException {
        model.addAttribute("accounts",account);
        logger.info("taille : " + account.size());
        return "redirect:agiosInfo";
    }


JSP

<table class="w3-table-all w3-hoverable">

        <tr class="w3-hover-blue w3-blue">
            <th>Nom</th>
            <th>Prénom </th>
            <th>iban</th>
            <th>email</th>
            <th>Découvert Autorisé</th>
            <th>Découvert actuel</th>
            <th>Taux débiteurs</th>
        </tr>
        <c:forEach items="${overdraftAccounts}" var="overdraftAccounts">
            <tr class="w3-hover-blue">
                <td><c:out value="${overdraftAccounts.getCustomer().getName()}" /></td>
                <td><c:out value="${overdraftAccounts.getCustomer().getSurname()}" /></td>
                <td><c:out value="${overdraftAccounts.getIban()}" /></td>
                <td><c:out value="${overdraftAccounts.getCustomer().getEmail()}" /></td>
                <td><c:out value="${overdraftAccounts.getCustomer().getAgioParameters().getAuthorizedOverdraft()}" /></td>
                <td><c:out value="${overdraftAccounts.getOldBalances().get(overdraftAccounts.getOldBalances().size()-1).getBalance()}" /></td>
                <td><c:out value="${overdraftAccounts.getCustomer().getAgioParameters().getBorrowingRate()}" /></td>
            </tr>
        </c:forEach>
    </table>
        </br>
        </br>

    <form method="POST" action="/agioDetails" modelAttribute="account">
       <c:forEach var="test" items="${accounts}" varStatus="status">
            <form:input path="accounts[${status.index}].getIban()" name="iban" id="iban" value="test.getIban()" />
            <form:input path="accounts[${status.index}].getBalance()" name="balance" id="balance" value="test.getBalance()" />
            <form:input path="accounts[${status.index}].oldBalances()" name="oldBalances" id="oldBalances" value="test.getOldBalances()" />
            <form:input path="accounts[${status.index}].getCustomer()" name="customer" id="customer" value="test.getCustomer()" />
            <form:input path="accounts[${status.index}].getAgios()" name="agios" id="agios" value="test.getAgios()" />
        </c:forEach>
        </br>
        <table align="center">
            <tr>
                <td colspan="2"><input class="w3-button w3-padding-large w3-blue" type="submit" value="Consulter le détail"/></td>
            </tr>
        </table>
    </form>

当前,我只是试图从jsp中获取控制器中的列表,因此,我只是尝试查看控制器中的列表是否不为空。如果有人可以帮助我解决这个问题并解释我做错了什么。.

1 个答案:

答案 0 :(得分:1)

嗨,我认为您向控制器发出发布请求的方式有点混乱,因此,我建议您尝试执行此操作:

首先在java中为您的Details创建一个Entity类, 例如

Details.java

public class Details {
    private String iban;
    private Integer balance;
    private Integer oldBalances;
    private String customer;
    private String agios;
    public String getIban() {
        return iban;
    }
    public void setIban(String iban) {
        this.iban = iban;
    }
    public Integer getBalance() {
        return balance;
    }
    public void setBalance(Integer balance) {
        this.balance = balance;
    }
    public Integer getOldBalances() {
        return oldBalances;
    }
    public void setOldBalances(Integer oldBalances) {
        this.oldBalances = oldBalances;
    }
    public String getCustomer() {
        return customer;
    }
    public void setCustomer(String customer) {
        this.customer = customer;
    }
    public String getAgios() {
        return agios;
    }
    public void setAgios(String agios) {
        this.agios = agios;
    }
    @Override
    public String toString() {
        return "Details [iban=" + iban + ", balance=" + balance + ", oldBalances=" + oldBalances + ", customer="
                + customer + ", agios=" + agios + "]";
    }

}

将此ID添加到您的按钮“ btnSubmit”中,并将其放入您的javascipt(我正在使用jquery)

var details = {};
$("#btnSubmit").click(function(){
details.iban = $("#iban").val();
details.balance = $("#balance ").val();
details.oldBalances = $("#oldBalances").val();
details.customer = $("#customer ").val();
details.agios = $("#agios ").val();
$.ajax({
          method: "POST",
          headers: {
            "Content-Type":"application/json"  
          },
          url: "/agioDetails",
          data: JSON.stringify(details ),
          success: function( result ) {
           alert("okay");
          }
          });
})

并将其发送给您的控制器

@PostMapping(value = "/agioDetails", consumes = "application/json")
    ModelAndView getRecord(@RequestBody Details details) throws SQLException, Exception {
        System.out.println(details);
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

数据应该正确无误地传递到Java cotroller。