List<Customer> customer = manageCustomer.findByNumber(phoneNumber);
request.setAttribute("customers", customer);
这是Action中的代码,customer中只有一个结果集,但我在JSP页面中用forEach c:out输出时却有两个结果,也就是说forEach循环输出了两次相同的内容,请问这怎么解决啊,有没有什么办法让forEach只循环一次? 或者说不用forEach 只用c:out能不能输出customers中的内容?还望不吝指教!

解决方案 »

  1.   

    你用list在页面打一下,看有几条
      

  2.   

    List <Customer> customer = manageCustomer.findByNumber(phoneNumber); 
    上面是从数据库中查询的结果(只有一条)为List类型然后存在request中如下:request.setAttribute("customers", customer);
    然后在jsp页面中作如下输出:
    <html:form action="/displayCustomer">
    <c:forEach items="${requestScope['customers']}" var="customers">
    <input type="hidden" name="customerId" value="${customers.customerId}"/>
    <c:out value="${customers.customerName}"/>
    <c:out value="${customers.phoneNumber}"></c:out>
    <html:select property="customerType">
    <input type="submit" value="确定更改">
    <input type="reset" value="取消">
    </c:forEach>
    </html>
    但是上面输出的结果却有相同的两组,还请帮忙!
      

  3.   


    你再仔细看看你的customers当中真的只有一个结果吗?
    照理说<c:forEach>是不会迭代两次的。
      

  4.   

    各位大哥实在不好意思,小弟着实范了一个低级性的错误,我在action中作了这样一个错误的动作:if(customer.addAll(customer)){
      request.setAttribute("customers", customer);}
    所以用forEach输出时就产生了两个相同的结果!现在终于解决了,谢谢各位的帮忙!