views:

72

answers:

1

I've got a page where a purchase can be entered, along with all of the foos bought. I've got three elements in a html document that are parsed into a comma-separated format.

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(); 
}

Once broken up,each id/price/qty should populate into an an object...

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

which can belong as a collection to another object...

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

I know I can just grab the request parameters and build the foo objects one by one. I figured there is a better way to populate that list while the data binding is performed on the purchase object, since it populates all other properties correctly.

A: 

Apart from concatenating the thing into a single commaseparated parameter and splitting it in the server side (which is a pretty nasty approach to be honest), you could also just send multiple parameter values along the same parameter name. It would then be available as a String[] array by HttpServletRequest#getParameterValues() on the parameter name.

I don't do Spring, so here's a plain vanilla HTML/Servlet example to give the idea:

<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]);
    // ...
}

I however highly question the need to send the price from client to server as well. I'd rather (re)calculate the price at the server side since the client has full control over the request parameters it sends and is thus able to change the parameter values outside the control of your form.

BalusC
I'll definitely change it to a single parameter name to save me putting the commas in client side. I already have similar code for the controller and can validate it.This is for a pure data entry screen, so no worries about the client modifying the price...since they are entering it!
ClutchDude