servlet取到值后传到jsp输出却是空值,哪里出错了?
jsp的商城系统,在后台servlet取数据库的商品数据后,转发到jsp渲染出来,但是前台渲染始终是空白的,找了好久都没找到问题,也不报错,下面是代码:
ListProductServlet.java
package sh.shop.web.servlet.manager;
import java.io.IOException;
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 sh.shop.domain.Product;
import sh.shop.exception.ListProductException;
import sh.shop.service.ProductService;
@WebServlet("/listProduct")
/**
* 后台
* 查询所有商品信息的servlet
*/
public class ListProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 1.创建service层的对象
ProductService service = new ProductService();
// 2.调用service层的listAll()
List<Product> list = service.listAll();
// 3.将查询出的所有商品放进request域中
request.setAttribute("list", list);
// 4.将请求转发到list.jsp
request.getRequestDispatcher("/admin/products/list.jsp").forward(
request, response);
return;
} catch (ListProductException e) {
e.printStackTrace();
response.getWriter().write(e.getMessage());
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
ProductService.java
package sh.shop.service;
import java.io.IOException;
import java.sql.SQLException;
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 sh.shop.exception.AddProductException;
import sh.shop.dao.ProductDao;
import sh.shop.domain.Product;
import sh.shop.exception.ListProductException;
@WebServlet("/ProductService")
public class ProductService {
private ProductDao dao = new ProductDao();
public List<Product> listAll() throws ListProductException {
try {
List<Product> list = dao.listAll();
return list;
} catch (SQLException e) {
e.printStackTrace();
throw new ListProductException("查询商品失败");
}
}
public void addProduct(Product p) throws AddProductException {
try {
dao.addProduct(p);
} catch (SQLException e) {
e.printStackTrace();
throw new AddProductException("添加商品失败");
}
}
}
ProductDao.java
package sh.shop.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import sh.shop.domain.Product;
import sh.shop.utils.DataSourceUtils;
public class ProductDao {
// 查询所有商品
public List<Product> listAll() throws SQLException {
String sql = "select * from products";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
List<Product> list = runner.query(sql, new BeanListHandler<Product>(Product.class));
return list;
}
//添加商品
public void addProduct(Product p) throws SQLException {
String sql = "insert into products values(?,?,?,?,?,?,?)";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
runner.update(sql, p.getId(), p.getName(), p.getPrice(),
null, p.getImgurl(), p.getDescription());
}
}
list.jsp
<tbody>
<c:forEach items="${list}" var="p">
<tr class="text-c va-m">
<td><input name="" type="checkbox" value=""></td>
<td>${p.id }</td>
<td><a onClick="" href="javascript:;"><img width="60" class="product-thumb" src=""></a></td>
<td class="text-l"><a style="text-decoration:none" onClick="" href="javascript:;">${p.name }</a></td>
<td class="text-l">${p.description }</td>
<td>${p.price }</td>
<td>${p.category }</td>
<td class="td-manage"><a style="text-decoration:none" class="ml-5" onClick="product_edit('产品编辑','product-add.html','10001')" href="javascript:;" title="编辑"><i class="Hui-iconfont"></i></a> <a style="text-decoration:none" class="ml-5" onClick="product_del(this,'10001')" href="javascript:;" title="删除"><i class="Hui-iconfont"></i></a></td>
</tr>
</c:forEach>
</tbody>
后台取到的值是有数据的:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
${requestScope.list} 试一下? 一楼的意思应该是在浏览器debug一下看看吧...
放session吧周期长
用debug标签看下前端值栈里面的list呢,是null就是传值问题,否则就是前端取值的问题
@鸟德大师 是这个吗?看不懂哦