如何在Spring Boot中使用<spring:nestedPath>和<spring:bind>标签将数据绑定到选择元素?

时间:2019-11-20 23:59:40

标签: java spring-boot spring-mvc jsp

我想使用<spring:nestedPath path="XXX"><spring:bind path="XXX">标签选择从控制器发送的适当选项,因为我的程序使用自定义类作为过滤器。

这就是我得到的:

查看

<%@ page language="java" contentType="text/html"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<div class="modal fade messages-modal-filter" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Filters</h5>
                <button type="button" class="close" data-dismiss="modal"
                    aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <spring:nestedPath path="filters">
            <form method="POST" name="messages-filter-form"
                id="messages-filter-form" 
                action="${pageContext.servletContext.contextPath}/filterMessages"
                class="form-horizontal">
                <div class="modal-body">
                    <div class="container-fluid">
                        <div class="form-group row">
                            <label class="col col-form-label data-filter-label">Model</label>
                            <div class="col-sm no-padding">
                                <spring:bind path="car.model">
                                <select class="form-control data-filter-input"
                                    id="model-filter" placeholder="filter">
                                    <option selected="selected" value="">-- Please choose --</option>
                                    <c:forEach items="${carModelList}" var="model">
                                        <option value="${model}">${model}</option>
                                    </c:forEach>
                                </select>
                                </spring:bind>
                            </div>
                        </div>
                        <!-- More elements -->
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

控制器

@Controller
@Scope("session")
public class MessagesController {

    private Criteria criteria;


    @RequestMapping("/messages")
    public String messages(Model model,
            @RequestParam(value = "model", defaultValue = "") final String modelParam)  {

        if(criteria == null) {
            criteria = new Criteria();
        }

        criteria.getCar().setModel(Strings.isNotBlank(modelParam) ? modelParam : "");

        model.addAttribute("carModelList", service.getModels());
        model.addAttribute("filters", criteria);
        return "messages";
    }

    /* 
        More controller methods 
    */

}

条件

public class Criteria {

    private Message message;
    private Car car;

    /*
        Getters and Setters
    */

}

仅供参考-汽车具有名称,型号,年份和制造属性。

我知道还有另一种选择

<form:form commandName="products[0]">
   <form:select path="XXX"/>
</form:form>

但是问题是我无法绑定嵌套对象。

知道我缺少什么吗?将bind标签放在选项上方也不能解决问题。

谢谢。

0 个答案:

没有答案