通过Spring填充请求集合?

时间:2010-07-20 03:39:07

标签: java spring servlets spring-mvc

我有一个可以输入购买的页面,以及所有购买的foos。 我在html文档中有三个元素,这些元素被解析为逗号分隔格式。

function submitStuff()
{
//grab each cells value from dynamically built table based on user entries
//appending comma
    document.form.ids.value=Ids; 
    document.form.price.value=prices; 
    document.form.qtys.value=qtys; 
    document.form.submit(); 
}

一旦分解,每个id / price / qty应该填充到一个对象中......

public class foo{ 
private int id; 
private BigDecimal price; 
private int qty; 
//set&get;
}

可以作为另一个对象的集合......

public class purchase{
private Date date; 
private int buyId;
private List<foo> purchases;
//set&get;
}

我知道我可以抓住请求参数并逐个构建foo对象。我认为在购买对象上执行数据绑定时,有更好的方法来填充该列表,因为它正确地填充了所有其他属性。

3 个答案:

答案 0 :(得分:0)

除了将事物连接到单个commaseparated参数并在服务器端拆分(实际上这是一个非常nasty方法)之外,您还可以沿着相同的参数名称发送多个参数值。然后,它将在参数名称上以HttpServletRequest#getParameterValues()String[]数组形式提供。

我不做Spring,所以这里有一个简单的vanilla HTML / Servlet示例来表达这个想法:

<table>
    <tr>
        <td>ID: 12<input type="hidden" name="id" value="12"></td>
        <td>Qty: <input type="text" name="qty"></td>
        <td>Price: $100.00<input type="hidden" name="price" value="100.00"></td>
    </tr>
    <tr>
        <td>ID: 54<input type="hidden" name="id" value="54"></td>
        <td>Qty: <input type="text" name="qty"></td>
        <td>Price: $200.00<input type="hidden" name="price" value="200.00"></td>
    </tr>
    <tr>
        <td>ID: 8<input type="hidden" name="id" value="8"></td>
        <td>Qty: <input type="text" name="qty"></td>
        <td>Price: $500.00<input type="hidden" name="price" value="500.00"></td>
    </tr>
</table>

的Servlet

String[] ids = request.getParameterValues("id");
String[] qtys = request.getParameterValues("qty");
String[] prices = request.getParameterValues("price");

for (int i = 0; i < ids.length; i++) {
    Long id = Long.parseLong(ids[i]);
    Integer qty = Integer.parseInt(qtys[i]);
    BigDecimal price = new BigDecimal(prices[i]);
    // ...
}
然而,我高度质疑是否需要将价格从客户端发送到服务器。我宁愿(重新)计算服务器端的价格,因为客户端可以完全控制它发送的请求参数,因此可以在表单控件之外更改参数值。

答案 1 :(得分:0)

您需要创建一个List。然后创建一个购买并确保使用setter方法在购买之前设置foo列表,然后将其绑定到表单或将其添加到模型映射(取决于您使用的是simpleform还是注释控制器)。

答案 2 :(得分:0)

您可以使用Spring的“本机”绑定机制。

样本控制器:

@Controller
public class OrderController {
    @ModelAttribute("purchase")
    public Purchase getPurchase(@RequestParam("purchase-id") int purchaseId) {
        // create purchase object
    }

    @RequestMapping(...)
    public void processPosting(@ModelAttribute("purchase") Purchase purchase) {
        // process order
    }
}

示例HTML:

<spring:form commandName="purchase">
    <table>
        <c:forEach var="item" items="${purchase.items}" varStatus="status">
            <spring:nestedPath path="purchase.items[${status.index}]">
                <tr>
                    <td>ID: 8 <form:hidden path="id" />
                    <td>Quantity: 8 <form:text path="quantity" />
                </tr>
            </spring:nestedPath>
        </c:forEach>
    </table>
    <input type="submit" />
</spring:form>

将在每个请求上调用方法getPurchase()并提供默认购买,然后使用请求中的值填充该购买。此方法接受常规注释,如演示@RequestParam,这将有助于您创建正确的购买对象。

购买将作为请求属性提供,名称为purchase

表单遍历所有purchease项目并使用对所购商品列表的索引访问创建嵌套路径,因此请确保商品的顺序始终相同!

对于第一个项目,请求参数将被命名为purchase.items[0].quantity,依此类推。 Spring会将这些参数名称绑定到模型属性purchase,属性items,索引0,属性quantity,就像常规属性路径一样。

希望这可以帮助您完成任务。