java list jsp页面_java servlet 输出一个list 到jsp页面 循环在table中

publicListfind_meiricq_smc(Stringsql){Connectioncon=DbConnection_tcc.getConnection();sql="SELECTxuehao,name,kahaoFROMICCO.dbo.V_xsdaoru";Listlis=newArrayList

public List find_meiricq_smc(String sql) {

Connection con = DbConnection_tcc.getConnection();

sql = "SELECT xuehao, name,kahao FROM ICCO.dbo.V_xsdaoru ";

List lis = new ArrayList();

int id = 0;

try {

PreparedStatement stmt = con.prepareStatement(sql);

ResultSet rs = stmt.executeQuery();

while (rs.next()) {

lis.add(rs.getString(1));

lis.add(rs.getString(2));

lis.add(rs.getString(3));

lis.add("");

}

} catch (Exception e) {

Log.log(this, "queryUser:" + e);

e.printStackTrace();

} finally {

ConnTools.close(con);

}

return lis;

}

}

java Struts + Spring +servlet 我从执行这个sql 返回一个lis 而我在Action层 获取list 输出list到jsp页面

List list = service.ussyzbt(sql);

try {

request.setAttribute("list", list);

request.getRequestDispatcher("table-jsp").forward(request,response);

} catch (Exception e) {

e.printStackTrace();

}}

而我在jsp页面 这样获取

ArrayList list = (ArrayList)request.getAttribute("list");

我要怎么遍历这个list 循环在一个table中

展开

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给您提供一个简单的购物车例子,只使用ServletJSP实现。 首先,在购物车需要实现添加商品、删除商品和修改商品数量的功能。我们可以通过在Session保存购物车的信息来实现这些功能。 在添加商品时,我们可以在Servlet获取商品的信息,将商品信息保存在一个JavaBean,并将JavaBean对象添加到Session。在JSP,我们可以通过JSTL标签库来遍历Session保存的JavaBean对象,将购物车的商品展示出来。 在删除商品时,我们需要获取要删除的商品的信息,然后遍历Session保存的JavaBean对象,找到对应的商品并将其从Session移除。在JSP,我们可以通过JSTL标签库来重新展示购物车的商品列表。 在修改商品数量时,我们可以通过在JSP使用表单来获取用户输入的数量,并将数量信息保存在Session。在展示购物车的商品列表时,我们可以根据Session保存的数量信息来展示对应商品的数量,并在用户修改数量时更新Session保存的数量信息。 这里是一个简单的购物车示例代码,您可以参考一下: CartServlet.java: ```java import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @WebServlet("/cart") public class CartServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); HttpSession session = request.getSession(); Cart cart = (Cart) session.getAttribute("cart"); if (cart == null) { cart = new Cart(); session.setAttribute("cart", cart); } if ("add".equals(action)) { int id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); double price = Double.parseDouble(request.getParameter("price")); int quantity = Integer.parseInt(request.getParameter("quantity")); Product product = new Product(id, name, price); cart.addItem(product, quantity); response.sendRedirect(request.getContextPath() + "/cart.jsp"); } else if ("delete".equals(action)) { int id = Integer.parseInt(request.getParameter("id")); Product product = new Product(id, "", 0); cart.removeItem(product); response.sendRedirect(request.getContextPath() + "/cart.jsp"); } else if ("update".equals(action)) { List<CartItem> items = new ArrayList<CartItem>(); for (Product product : cart.getItems().keySet()) { int quantity = Integer.parseInt(request.getParameter("quantity_" + product.getId())); items.add(new CartItem(product, quantity)); } cart.updateItems(items); response.sendRedirect(request.getContextPath() + "/cart.jsp"); } else { response.sendRedirect(request.getContextPath() + "/cart.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ``` Cart.java: ```java import java.util.HashMap; import java.util.List; import java.util.Map; public class Cart { private Map<Product, Integer> items; public Cart() { items = new HashMap<Product, Integer>(); } public void addItem(Product product, int quantity) { int newQuantity = quantity; if (items.containsKey(product)) { newQuantity += items.get(product); } items.put(product, newQuantity); } public void removeItem(Product product) { items.remove(product); } public Map<Product, Integer> getItems() { return items; } public double getTotal() { double total = 0; for (Product product : items.keySet()) { total += product.getPrice() * items.get(product); } return total; } public void updateItems(List<CartItem> cartItems) { for (CartItem cartItem : cartItems) { Product product = cartItem.getProduct(); int quantity = cartItem.getQuantity(); if (quantity <= 0) { items.remove(product); } else { items.put(product, quantity); } } } } ``` CartItem.java: ```java public class CartItem { private Product product; private int quantity; public CartItem(Product product, int quantity) { this.product = product; this.quantity = quantity; } public Product getProduct() { return product; } public int getQuantity() { return quantity; } } ``` Product.java: ```java public class Product { private int id; private String name; private double price; public Product(int id, String name, double price) { this.id = id; this.name = name; this.price = price; } public int getId() { return id; } public String getName() { return name; } public double getPrice() { return price; } } ``` cart.jsp: ```jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>购物车</title> </head> <body> <h1>购物车</h1> <table border="1"> <tr> <th>ID</th> <th>名称</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> <c:forEach items="${cart.items}" var="entry"> <tr> <td>${entry.key.id}</td> <td>${entry.key.name}</td> <td>${entry.key.price}</td> <td> <form method="post" action="${pageContext.request.contextPath}/cart"> <input type="hidden" name="action" value="update"> <input type="hidden" name="id" value="${entry.key.id}"> <input type="text" name="quantity_${entry.key.id}" value="${entry.value}"> <input type="submit" value="更新"> </form> </td> <td>${entry.key.price * entry.value}</td> <td> <form method="post" action="${pageContext.request.contextPath}/cart"> <input type="hidden" name="action" value="delete"> <input type="hidden" name="id" value="${entry.key.id}"> <input type="submit" value="删除"> </form> </td> </tr> </c:forEach> <tr> <td colspan="4">总计:</td> <td>${cart.total}</td> <td></td> </tr> </table> <h2>添加商品</h2> <form method="post" action="${pageContext.request.contextPath}/cart"> <input type="hidden" name="action" value="add"> ID:<input type="text" name="id"><br> 名称:<input type="text" name="name"><br> 单价:<input type="text" name="price"><br> 数量:<input type="text" name="quantity"><br> <input type="submit" value="添加"> </form> </body> </html> ``` 在这个例子,我们创建了一个Cart类来保存购物车的信息,其包括了添加商品、删除商品和修改商品数量的方法。在Servlet,我们根据用户的操作来调用Cart类的相应方法,并将修改后的购物车信息保存在Session。在JSP,我们使用JSTL标签库来遍历Session保存的购物车信息,并展示购物车的商品列表。同时,我们还使用了表单来获取用户输入的信息,并将其传递给Servlet来完成添加商品的操作。 这个例子比较简单,仅供参考。在实际开发,还需要考虑数据校验、安全性等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值