Spring MVC应用程序中的长URL

时间:2018-02-06 19:11:40

标签: java spring jsp spring-mvc

我正在创建一个Spring MVC应用程序,使用大量的表单输入和带有多个List字段的模型属性。在我的jsp页面上,我有类似目录的东西,用户可以指定他想要的不同大小的产品&#39 ;订购。 index.jsp的:

    <div class="catalog_element">
                    <img src="/resources/img/fb.png" width="250px" height="250px" class="product_image"/>
                    <h4>Фундаментные блоки</h4>
                    <div class="product_picker">
                        <div class="show_fb_content">
                            <img width="80px" height="40px" src="/resources/img/pointer_down.png">
                        </div>
                        <div class="fb_content">
                            <div class="product_item">
                                <h3>Размер L H B </h3>
                                <h3 class="amount_input">Кол</h3>
                            </div>
                            <c:forEach items="${FBContainer.products}" varStatus="i" var="product">
                                <div class="product_item">
                                    <h3>${product.toString()}</h3>
                                    <form:input cssClass="amount_input"
                                                path="fbContainer.products[${i.index}].amount"/>
                                </div>
                            </c:forEach>
                        </div>
                    </div>
                </div>
 <div class="catalog_element">
                    <img src="/resources/img/roadplates.png" width="250px" height="250px" class="product_image"/>
                    <h4>Плиты перекрытия</h4>
                    <div class="product_picker">
                        <div class="show_fb_content">
                            <img width="80px" height="40px" src="/resources/img/pointer_down.png">
                        </div>
                        <div class="fb_content">
                            <div class="product_item">
                                <h3>Размер L H B </h3>
                                <h3 class="amount_input">Кол</h3>
                            </div>
                            <c:forEach items="${CPContainer.products}" varStatus="i" var="product">
                                <div class="product_item">
                                    <h3>${product.toString()}</h3>
                                    <form:input cssClass="amount_input"
                                                path="cpContainer.products[${i.index}].amount"/>
                                </div>
                            </c:forEach>
                        </div>
                    </div>
                </div>

因此,在forEach循环中,我为每种产品创建了一个金额输入。每种产品类型(其中有6种)都有自己的容器。 类似于CPContainer:

public class CPContainer implements Container {

    private List<CPProduct> products;
    private int id;
    private int amount;

    @Override
    public List<CPProduct> getProducts() {
        return products;
    }

    @Override
    public CPProduct getProductById(int id) {
        return products.get(id);
    }

    public void setProducts(List<CPProduct> products) {
        this.products = products;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "CPContainer{" +
                "products=" + products +
                ", id=" + id +
                ", amount=" + amount +
                '}';
    }
}

不确定这是否有问题,但当我填写表单时,在订单页面上,我看到一个很长的网址,其中包含许多参数: http://localhost:8080/calculate?fbContainer.products%5B0%5D.amount=3&fbContainer.products%5B1%5D.amount=2&fbContainer.products%5B2%5D.amount=0&cpContainer.products%5B0%5D.amount=0&cpContainer.products%5B1%5D.amount=0&cpContainer.products%5B2%5D.amount=0&jumperContainer.products%5B0%5D.amount=0&jumperContainer.products%5B1%5D.amount=0&jumperContainer.products%5B2%5D.amount=0&vbContainer.products%5B0%5D.amount=0&vbContainer.products%5B1%5D.amount=0&vbContainer.products%5B2%5D.amount=0&bbContainer.products%5B0%5D.amount=0&bbContainer.products%5B1%5D.amount=0&bbContainer.products%5B2%5D.amount=0&pbContainer.products%5B0%5D.amount=0&pbContainer.products%5B1%5D.amount=0&pbContainer.products%5B2%5D.amount=0。 这是不好的,还是我应该这样做?

1 个答案:

答案 0 :(得分:2)

使用POST方法代替GET。 GET请求生成请求参数,该参数在URL中可见。 像这样: