为什么在JSP中的<c:forEach>里面的内容不能显示?
这个是servlet:request.setCharacterEncoding("GB2312");Stringid=request.getParameter("cid"...
这个是servlet:
request.setCharacterEncoding("GB2312");
String id = request.getParameter("cid");
int categoryId = 1;
if(id!=null){
categoryId = Integer.parseInt(id);
}
//获取所有的类别
categoryService cs = new categoryService();
ArrayList<category> clist = cs.getAllCategorys();
//获取该类别的所有商品
productService ps = new productService();
ArrayList<product> plist = ps.getProductsByCategory(categoryId);
request.setAttribute("cl", clist);
request.setAttribute("pl", plist);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request,response);
这个是jsp:
<c:forEach items="${cl}" var="c" >
<li><a href="productListServlet?cid=${c.categoryID}">${c.categoryName}</a></li>
</c:forEach>
下面是两个方法:
public ArrayList<category> getAllCategorys(){
Connection conn=DbConn.getConnction();
try {
String sql="select * from category";
//建立语句对象
Statement ps =conn.createStatement();
//执行查询语句
ResultSet rs = ps.executeQuery(sql);
category c;
ArrayList<category> clist = new ArrayList();
while(rs.next())
{
c =new category();
c.setCategoryID(rs.getInt("categoryID"));
c.setCategoryName(rs.getString("categoryName"));
c.setCategoryDes(rs.getString("categoryDes"));
clist.add(c);
}
rs.close();
ps.close();
if(conn != null) {
conn.close();
conn = null;
}
return clist;
} catch (SQLException e) {
e.printStackTrace();
}
finally{
}
return null;
}
public ArrayList<product> getProductsByCategory(int categoryId){
Connection conn=DbConn.getConnction();
try {
String sql="select * from product where categoryId = ?";
//建立语句对象
PreparedStatement ps =conn.prepareStatement(sql);
ps.setInt(1,categoryId);
//执行查询语句
ResultSet rs = ps.executeQuery();
product p;
ArrayList<product> plist = new ArrayList();
while(rs.next())
{
p =new product();
p.setProductID(rs.getInt("productID"));
p.setProductName(rs.getString("productName"));
p.setProductWriter(rs.getString("productWriter"));
p.setProductPrice(rs.getString("productPrice"));
p.setProductCent(rs.getString("productCent"));
p.setProductImg(rs.getString("productImg"));
p.setCategoryID(rs.getString("categoryID"));
plist.add(p);
}
rs.close();
ps.close();
if(conn != null) {
conn.close();
conn = null;
}
return plist;
} catch (SQLException e) {
e.printStackTrace();
}
finally{
}
return null;
} 展开
request.setCharacterEncoding("GB2312");
String id = request.getParameter("cid");
int categoryId = 1;
if(id!=null){
categoryId = Integer.parseInt(id);
}
//获取所有的类别
categoryService cs = new categoryService();
ArrayList<category> clist = cs.getAllCategorys();
//获取该类别的所有商品
productService ps = new productService();
ArrayList<product> plist = ps.getProductsByCategory(categoryId);
request.setAttribute("cl", clist);
request.setAttribute("pl", plist);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request,response);
这个是jsp:
<c:forEach items="${cl}" var="c" >
<li><a href="productListServlet?cid=${c.categoryID}">${c.categoryName}</a></li>
</c:forEach>
下面是两个方法:
public ArrayList<category> getAllCategorys(){
Connection conn=DbConn.getConnction();
try {
String sql="select * from category";
//建立语句对象
Statement ps =conn.createStatement();
//执行查询语句
ResultSet rs = ps.executeQuery(sql);
category c;
ArrayList<category> clist = new ArrayList();
while(rs.next())
{
c =new category();
c.setCategoryID(rs.getInt("categoryID"));
c.setCategoryName(rs.getString("categoryName"));
c.setCategoryDes(rs.getString("categoryDes"));
clist.add(c);
}
rs.close();
ps.close();
if(conn != null) {
conn.close();
conn = null;
}
return clist;
} catch (SQLException e) {
e.printStackTrace();
}
finally{
}
return null;
}
public ArrayList<product> getProductsByCategory(int categoryId){
Connection conn=DbConn.getConnction();
try {
String sql="select * from product where categoryId = ?";
//建立语句对象
PreparedStatement ps =conn.prepareStatement(sql);
ps.setInt(1,categoryId);
//执行查询语句
ResultSet rs = ps.executeQuery();
product p;
ArrayList<product> plist = new ArrayList();
while(rs.next())
{
p =new product();
p.setProductID(rs.getInt("productID"));
p.setProductName(rs.getString("productName"));
p.setProductWriter(rs.getString("productWriter"));
p.setProductPrice(rs.getString("productPrice"));
p.setProductCent(rs.getString("productCent"));
p.setProductImg(rs.getString("productImg"));
p.setCategoryID(rs.getString("categoryID"));
plist.add(p);
}
rs.close();
ps.close();
if(conn != null) {
conn.close();
conn = null;
}
return plist;
} catch (SQLException e) {
e.printStackTrace();
}
finally{
}
return null;
} 展开
展开全部
jsp中引入c标签了吗?
在action中用request.set 返回一个,然后再在jsp页面中用request.get 试试看。如果这样可以就所以你c标签引入出现了问题。
在action中用request.set 返回一个,然后再在jsp页面中用request.get 试试看。如果这样可以就所以你c标签引入出现了问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询