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

标签 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 :

@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 上的列表变量中的内容(即 customerRequestsTo 属性)被设置为 null,Bean 上的其他所有内容都被保留。

最佳答案

您不会在请求之间保留模型属性 - 您需要将页面中 Bean 的所有值作为输入包含在内,在下一个请求中将它们 POST 回来,然后让 Spring 将它们绑定(bind)到新 Bean在 POST 请求上,或者使用 @SessionAttributes 告诉 Spring 将其存储在 session 中:

@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

关于java - Spring MVC - 页面提交后无法从 bean 列表中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28617405/

相关文章:

java - 另一个类的静态函数不会修改传入的引用

java - Spark ClassCastException 无法将 FiniteDuration 的实例分配给 Scala 2.10.5 上的字段 RpcTimeout.duration

java - 从 Spring 批处理 ItemProcessor 返回多个项目

java - 我是否需要一个字符集过滤器来仅将某些页面显示为带有 tomcat 的 UTF8

java - 在带有 URI 的 HTTP 请求中找不到映射 [springcodes/functions.jsp]

java - 什么原因指定InitialHeapSize大于1m?

java - 初始化和 spring 集成 channel

java - Spring + GWT 或 Spring 与 GWT

java - 以编程方式从 Camel 上下文访问 Camel 属性

java - 如何将输入流转换为文件