Diego Estrada
Diego Estrada

Reputation: 87

Spring MVC - Not able to retrieve values from list on bean after page submit

I have been working on this project to learn Spring MVC, so I have this table where I select the rows I want to process, but every time I click submit, I check my backend (java console) and all of the values in the list are null. Here is my Java and JSP codes.

JSP:

<form:form method='POST' commandName="customerRequestsBean" action="customerRequests">
        Select the type of Request<br />
        <form:radiobutton id="Registration" path="typeOfRequest" value="R"/>Request for registration
        <form:radiobutton id="Deregistration" path="typeOfRequest" value="D"/>Request for deregistration
        <br>
        <div id="customerRequestsInfo">
        <c:if test="${not empty customerRequestsBean.list}">
            <table>
                <tr>
                    <td>Customer Id</td>
                    <td>Biller Id</td>
                    <td>Mode of Payment</td>
                    <td>Selected</td>
                </tr>
                <c:forEach var="cr"  items="${customerRequestsBean.list}" varStatus="crLoop">
                    <tr>
                        <td><c:out value="${cr.customerId}" /></td>
                        <td><c:out value="${cr.billerId}" /></td>
                        <td><c:out value="${cr.modeOfPayment}" /></td>
                        <td> <form:checkbox path="list[${crLoop.index}].selected" /> </td>
                    </tr>
                </c:forEach>
            </table>
            <input type="submit" value="Submit"/>
        </c:if>

My controller:

@Controller
@RequestMapping("/admin/customerRequests")
public class CustomerRequestsController {

    private CustomerRequestsService customerRequestsService;

    @Autowired
    public void setCustomerRequestsService(CustomerRequestsService customerRequestsService){
        this.customerRequestsService = customerRequestsService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initForm(Model model) {
        CustomerRequestsBean customerRequestsBean = new CustomerRequestsBean();

        customerRequestsBean.setList(customerRequestsService.getCustomerRequests("R"));
        model.addAttribute("customerRequestsBean", customerRequestsBean);

        return "/admin/customerRequests";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String validateRequests(Model model, @ModelAttribute("customerRequestsBean") CustomerRequestsBean customerRequestsBean, BindingResult result){

        System.out.println(customerRequestsBean.getList().size());

        for(CustomerRequestsTo cr : customerRequestsBean.getList())
            System.out.println("The Id is: " + cr.getCustomerId() + " The status is: " + cr.getStatus());

        customerRequestsBean.setList(customerRequestsService.getCustomerRequests(customerRequestsBean.getTypeOfRequest()));

        return "/admin/customerRequests";
    }

    @RequestMapping(value = "setRequestsType", method = RequestMethod.GET)
    public String processAjax(Model model, @RequestParam("typeOfRequest") String typeOfRequest){
        System.out.println("The type of request is: " + typeOfRequest);

        CustomerRequestsBean customerRequestsBean = new CustomerRequestsBean();
        customerRequestsBean.setMsg("The type of request is: " + typeOfRequest);
        customerRequestsBean.setList(customerRequestsService.getCustomerRequests(typeOfRequest));

        model.addAttribute("customerRequestsBean", customerRequestsBean);
        return "/admin/customerRequests";
    }
}   

CustomerRequestsBean.java

public class CustomerRequestsBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private String typeOfRequest;
    private String msg;
    private List<CustomerRequestsTo> list;
    public String getTypeOfRequest() {
    return typeOfRequest;
    }
    public void setTypeOfRequest(String typeOfRequest) {
        this.typeOfRequest = typeOfRequest;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public List<CustomerRequestsTo> getList() {
        return list;
    }
    public void setList(List<CustomerRequestsTo> list) {
        this.list = list;
    }

}

CustomerRequestsTo.java

public class CustomerRequestsTo implements Serializable {

    private static final long serialVersionUID = 1L;
    private int requestId;
    private String customerId;
    private String billerId;
    private char modeOfPayment;
    private Date DOP;
    private int maximumBillAmount;
    private String typeOfRequest;
    private Integer remarkId;
    private String status;
    private boolean selected;
    private String remarks;
    private String billNumber;
    private String customerName;
    private Integer transactionLimit;
    private Integer paymentId;

    public Integer getPaymentId() {
        return paymentId;
    }

    public void setPaymentId(Integer paymentId) {
        this.paymentId = paymentId;
    }

    public Integer getTransactionLimit() {
        return transactionLimit;
    }

    public void setTransactionLimit(Integer transactionLimit) {
        this.transactionLimit = transactionLimit;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getBillNumber() {
        return billNumber;
    }

    public void setBillNumber(String billNumber) {
        this.billNumber = billNumber;
    }

    public int getRequestId() {
        return requestId;
    }

    public void setRequestId(int requestId) {
        this.requestId = requestId;
    }

    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public String getBillerId() {
        return billerId;
    }

    public void setBillerId(String billerId) {
        this.billerId = billerId;
    }

    public char getModeOfPayment() {
        return modeOfPayment;
    }

    public void setModeOfPayment(char modeOfPayment) {
        this.modeOfPayment = modeOfPayment;
    }

    public Date getDOP() {
        return DOP;
    }

    public void setDOP(Date dOP) {
        DOP = dOP;
    }

    public int getMaximumBillAmount() {
        return maximumBillAmount;
    }

    public void setMaximumBillAmount(int maximumBillAmount) {
        this.maximumBillAmount = maximumBillAmount;
    }

    public String getTypeOfRequest() {
        return typeOfRequest;
    }

    public void setTypeOfRequest(String typeOfRequest) {
        this.typeOfRequest = typeOfRequest;
    }

    public Integer getRemarkId() {
        return remarkId;
    }

    public void setRemarkId(Integer remarkId) {
        this.remarkId = remarkId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

}

I am using the latest version of Spring MVC. Only what is on my list variable on my customerRequestsBean, which is customerRequestsTo attributes are being set null, everything else on my Bean is preserved.

Upvotes: 0

Views: 1311

Answers (1)

CupawnTae
CupawnTae

Reputation: 14580

You're not preserving your model attribute from request to request - you either need to include all the values from your bean in the page as inputs, POST them back with the next request, and have Spring bind them to the new bean on the POST request, or tell Spring to store it in the session with @SessionAttributes:

@Controller
@RequestMapping("/admin/customerRequests")
@SessionAttributes("customerRequestsBean")
public class CustomerRequestsController {

Documentation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib

Upvotes: 1

Related Questions