从复选框中获取和添加值

时间:2016-02-18 10:11:29

标签: java servlets

我目前有一个显示一些棒球卡及其值的servlet。我希望能够在复选框中列出它们,并能够选择您想要出售的卡片。然后,servlet将获取您检查的卡片并添加这些值并显示总数。我已经完成了大部分工作,除了我无法从复选框中获取值并总结它们。

这是我的jsp

 <form name="CashOut.jsp" method="post">
<table>   
    <c:forEach var="entry" items="${winingCards}">
        <tr>  
            <td>
                <input type="checkbox" name="cardvalue" value="${entry.price}"><h1>PRICE<h1>${entry.price}$</h1><br><img src="photofolio/images/cardimages/clevelandindians/${entry.name}.png" align="middle" style="max-height:25%; max-width:25% "/>
            </td>   
            </tr>       
    </c:forEach>     
</table>
<table>
    <tr>
        <td>
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>
</form>

这是我的控制器

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
         JFrame parent = new JFrame();
         int sumTotal = 0;
        String[] items =request.getParameterValues("cardvalue");
        for(String item : items){

             sumTotal=+sumTotal;

        }

         JOptionPane.showMessageDialog(parent, "You Cashed Out with: "+sumTotal);
         request.getRequestDispatcher( "/WEB-INF/photofolio/index.html" ).forward(
                 request, response );
        }
    }

1 个答案:

答案 0 :(得分:0)

将循环更改为

var str = "string to be exploded and seperated each item by space";
var res = str.split(" ");