Spring MVC - 在页面提交后无法从bean上的列表中检索值

时间:2015-02-19 21:36:37

标签: java spring jsp spring-mvc

我一直在研究这个项目来学习Spring MVC,所以我有这个表,我选择要处理的行,但每次单击提交时,我都会检查我的后端(java控制台)和所有值在列表中为空。这是我的Java和JSP代码。

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>

我的控制器:

@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;
    }

}

我使用的是最新版本的Spring MVC。只有我的customerRequestsBean上的list变量上的内容,即customerRequestsTo属性被设置为null,我的Bean上的其他所有内容都被保留。

1 个答案:

答案 0 :(得分:1)

您从请求到请求不保留您的模型属性 - 您需要在页面中包含bean中的所有值作为输入,将它们与下一个请求一起发回,并让Spring将它们绑定到POST请求中的新bean,或告诉Spring将其存储在@SessionAttributes的会话中:

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

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