JAVAEE model1模型实现商品浏览记录(去除重复的浏览记录)(一)

网友投稿 210 2023-06-29


JAVAEE model1模型实现商品浏览记录(去除重复的浏览记录)(一)

在javaee中Model1模型是以jsp页面为中心的,jsp既要对浏览器的request做出逻辑处理(使用javabean),访问数据库也要显示出相关的页面。

在model1模型中,没有servlet。

Model1结果图如下:

Model1的可维护性  可扩展性都是较差的  只适合小项目。

首先运行结果

goods.jsp

<%@page import="entity.Items"%>

<%@page import="dao.ItemsDao"%>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

border="0">

<%

ItemsDao dao = new ItemsDao();

ArrayList list = new ArrayList();

//从dao中获取所有的商品 并保存到list集合中

list = dao.getAllItems();

if (list != null && list.size() > 0) {

//循环遍历集合 并显示

for (int i = 0; i < list.size(); i++) {

Items item = list.get(i);

%>

src="images/<%=item.getPicture()%>" width="120" height="90"

border="1" />

src="images/<%=item.getPicture()%>" width="120" height="90"

border="1" />

产地:<%=item.getCity()%> 价格:¥

<%=item.getPrice()%>

}

}

%>

http://

在代码中 表示商品的图片

通过点击商品的图片  把当前商品的id传值给details页面

details.jsp通过商品的id来显示详细商品  ,而浏览记录由cookies维护

<%@page import="org.apache.taglibs.standard.tag.common.xml.ForEachTag"%>

<%@page import="entity.Items"%>

<%@page import="dao.ItemsDao"%>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

<%

ItemsDao dao = new ItemsDao();

//根据request传来的商品id 向dao中获得相对应的商品对象

Items item = dao.getItemById(Integer.parseInt(request

.getParameter("id")));

if (item != null) {

%>

width="200" height="150">

<%

}

//将该商品加入cookies

Cookie[] cookies = request.getCookies();

String historyStr = "";

for (Cookie c : cookies) {

if (c.getName().equals("history")) {

historyStr = c.getValue();

}

}

historyStr += item.getId() + ",";

Cookie c = new Cookie("history", historyStr);

//重新设置cookies

response.addCookie(c);

%>

<%

//根据cookie 从dao获取最后浏览的三次记录 并保存到list集合

ArrayList historyItems = dao.getHistoryView(historyStr);

if (historyItems != null && historyItems.size() > 0) {

//遍历集合

for (Items historyItem : historyItems) {

%>

src="images/<%=historyItem.getPicture()%>" width="100"

height="80" border="1">

<%

}

}

%>

dao层  负责商品在数据库中的查询操作

package dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import util.DBHelper;

import entity.Items;

//商品的业务逻辑类

public class ItemsDao {

// 获得所有商品信息

public ArrayList getAllItems() {

// 商品集合

ArrayList list = new ArrayList();

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = DBHelper.getConnection();

String sql = "select * from items";// sql 语句

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

// 将查询的结果依次加入集合

while (rs.next()) {

Items item = new Items();

item.setId(rs.getInt("id"));

item.setName(rs.getString("name"));

item.setCity(rs.getString("city"));

item.setPrice(rs.getDouble("price"));

item.setPicture(rs.getString("picture"));

item.setNumber(rs.getInt("number"));

list.add(item);

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

// 关闭资源

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return list;

}

// 根据商品编号获取商品资料

public Items getItemById(int id) {

Items item = new Items();

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

String sql = "select * from items where id = ?";

try {

con = DBHelper.getConnection();

ps = con.prepareStatement(sql);

ps.setInt(1, id);

rs = ps.executeQuery();

// 如果找到该id 为item对象初始化

if (rs.next()) {

item.setId(rs.getInt("id"));

item.setName(rs.getString("name"));

item.setCity(rs.getString("city"));

item.setPrice(rs.getDouble("price"));

item.setPicture(rs.getString("picture"));

item.setNumber(rs.getInt("number"));

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

// 关闭资源

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return item;

}

// 根据cookie 获得浏览的最后三个商品

public ArrayList getHistoryView(String cookie) {

ArrayList list = new ArrayList();

String ids[] = cookie.split(",");

int counts = 3;// 浏览的最后三条记录

if (ids != null && ids.length > 0) {

for (int i = ids.length - 1; i >= 0 && i > ids.length - counts - 1; i--) {

Items item = getItemById(Integer.parseInt(ids[i]));

/*

* 首先判断集合中是否存在当前物品 如果存在 counts+1 多读取一次(保证list集合中有3个对象) 不添加此物品

*/

if (list.contains(item)) {

counts++;

continue;

}

list.add(item);

}

}

return list;

}

}

商品的实体类 Items

package entity;

public class Items {

private int id;

private String name;

private String city;

private double price;

private int number;

private String picture;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public int getNumber() {

return number;

}

public void setNumber(int number) {

this.number = number;

}

public String getPicture() {

return picture;

}

public void setPicture(String picture) {

this.picture = picture;

}

@Override

public int hashCode() {

// TODO Auto-generated method stub

return this.getId()+this.getName().hashCode();

}

@Override

public boolean equals(Object obj) {

if(this==obj)

{

return true;

}

else

{

if(obj instanceof Items)

{

Items item=(Items) obj;

if(this.getId()==item.getId()&&this.getName().equals(item.getName()))

{

return true;

}

}

}

return false;

}

}

在这里  重写了hasCode和equals方法  来修改比较方式(所有的item都是一个新的对象 即使两个商品的内容全部一样也不会相等  。所以要修改比较方式)

因为对于浏览记录而言  我们不能通过刷新当前商品  浏览记录全部都是该商品 我们只要保证该商品在浏览记录中 只有一个即可

所以在dao层中的getHistoryView方法有这句代码

if (list.contains(item)) {

counts++;

continue;

}

然后是工具类

DBHelpher 单例模式获得connection对象

package util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class DBHelper {

private static final String driver = "com.mysql.jdbc.Driver";

private static final String url = "jdbc:mysql://localhost:3306/shopping?useUnicode=true&charcterEncoding=UTF-8";

private static final String username = "root";

private static final String password = "123";

private static Connection con = null;

// 静态块代码负责加载驱动

static {

try {

Class.forName(driver);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static Connection getConnection() {

if (con == null) {

try {

con = DriverManager.getConnection(url, username, password);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return con;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mysql存储java对象实例详解
下一篇:Mybatis源码分析之存储过程调用和运行流程
相关文章

 发表评论

暂时没有评论,来抢沙发吧~