自己写了一个小的javaweb项目,用的jsp+servlet。
想实现商品的分类显示,但是不知道怎样从jsp页面传值给dao层,以及查询语句怎么写?
求大神帮助。
这是jsp页面:
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=utf-8"%>
<%@ page import="cn.edu.qust.bookcitys.entity.Items"%>
<%@ page import="cn.edu.qust.bookcitys.dao.ItemsDao"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>展示</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/common.css" rel="stylesheet">
<link href="css/show.css" rel="stylesheet">
</head>
<body>
<header>
<!-- logo -->
<div class="index-header-top">
<img src="images/logo.png" class="logo">
</div>
<!--导航栏-->
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<ul class="nav navbar-nav">
<li><a href="index.jsp">首页</a></li>
<li class="active"><a href="show.jsp" target="_self">展示</a></li>
<li><a href="cart.jsp" target="_self">购物车</a></li>
<li><a href="login.jsp" target="_self">登录</a></li>
</ul>
</div>
</nav>
</header>
<div class="details_container">
<div class="details_left">
<dl>
<dt class="book">
<a href="show.jsp?sort=1">文学</a>
</dt>
<dd>
<a href="#">作品集</a>
</dd>
<dd>
<a href="#">名著</a>
</dd>
<dd>
<a href="#">小说</a>
</dd>
</dl>
<dl>
<dt class="xbook">
<a href="show.jsp?sort=2">青春</a>
</dt>
<dd>
<a href="#">青春</a>
</dd>
<dd>
<a href="#">励志</a>
</dd>
<dd>
<a href="#">校园</a>
</dd>
</dl>
<dl>
<dt class="book">
<a href="show.jsp?sort=3">生活</a>
</dt>
<dd>
<a href="#">作品集</a>
</dd>
<dd>
<a href="#">名著</a>
</dd>
<dd>
<a href="#">小说</a>
</dd>
</dl>
</div>
<div class="details_right">
<div style="width: 1000px;">
<%
ItemsDao itemsDao = new ItemsDao();
String sort = request.getParameter("sort");
ArrayList<Items> list = itemsDao.getAllItems();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
Items item = list.get(i);
request.setAttribute("item",item);
%>
<div style="width: 180px; height: 220px; float: left;">
<a href="details.jsp?id=${item.id }"><img
style="float: left;" src="images/${item.picture }"
width="170px" height="120px" border="1" /></a> <span
style="float: left; margin: 5px 0px 0px 5px; color: blue">${item.name }</span>
<span style="float: left; margin: 5px 0px 0px 5px; color: #000">作者:${item.author }
价格: ${item.price }¥
</span>
</div>
<%
}
}
%>
</div>
</div>
</div>
<div class="details_bottom">
<footer> Copyright © Made By 泥石流 </footer>
</div>
<!-- 如果要使用Bootstrap的js插件,必须先调入jQuery -->
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.min.js"></script>
<!-- 包括所有bootstrap的js插件或者可以根据需要使用的js插件调用 -->
<script
src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>
这是dao层:
package cn.edu.qust.bookcitys.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import cn.edu.qust.bookcitys.entity.Items;
import cn.edu.qust.bookcitys.util.DBHelper;
//商品的业务逻辑类
public class ItemsDao {
//获得所有的商品信息
public ArrayList<Items> getAllItems(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList<Items> list = new ArrayList<Items>();//商品集合
try{
conn = DBHelper.getConnection();
// String sql = "select * from items where sort=?;";//sql语句
String sql = "select * from items ;";//sql语句
stmt = conn.prepareStatement(sql);
// stmt.setInt(1, sort);
rs = stmt.executeQuery();
while(rs.next()){
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setAuthor(rs.getString("author"));
item.setNumber(rs.getInt("number"));
item.setPrice(rs.getInt("price"));
item.setPicture(rs.getString("picture"));
item.setSort(rs.getInt("sort"));
list.add(item);//每次遍历把一个商品加入集合
}
return list;
}
catch(Exception e){
e.printStackTrace();
return null;
}
finally{
//释放数据集对象
if(rs!=null){
try{
rs.close();
rs = null;
}
catch(Exception e){
e.printStackTrace();
}
}
//释放语句对象
if(stmt!=null){
try{
stmt.close();
stmt = null;
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
//根据商品编号获得商品资料
public Items getItemsById(int id){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
conn = DBHelper.getConnection();
String sql = "select * from items where id=?;";//sql语句
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
if(rs.next()){
Items item = new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setAuthor(rs.getString("author"));
item.setNumber(rs.getInt("number"));
item.setPrice(rs.getInt("price"));;
item.setPicture(rs.getString("picture"));
item.setSort(rs.getInt("sort"));
return item;
}
else{
return null;
}
}
catch(Exception e){
e.printStackTrace();
return null;
}
finally{
//释放数据集对象
if(rs!=null){
try{
rs.close();
rs = null;
}
catch(Exception e){
e.printStackTrace();
}
}
//释放语句对象
if(stmt!=null){
try{
stmt.close();
stmt = null;
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
//获取最近浏览的前3条信息
public ArrayList<Items> getViewList(String list){
System.out.println("list:"+list);
ArrayList<Items> itemlist = new ArrayList<Items>();
int iCount = 3;
if(list!=null&&list.length()>0){
String[] arr = list.split("#");
System.out.println("arr.length="+arr.length);
//如果商品记录大于等于3条
if(arr.length>=3){
for(int i=arr.length-1;i>=arr.length-iCount;i--){
itemlist.add(getItemsById(Integer.parseInt(arr[i])));
}
}
else{
for(int i = arr.length-1;i>=0;i--){
itemlist.add(getItemsById(Integer.parseInt(arr[i])));
}
}
return itemlist;
}
else{
return null;
}
}
}
想把sort的值传到dao层并实现分类显示。