- 积分
- 40165
- 好友
- 记录
- 主题
- 帖子
- 听众
- 收听
|
发表于 2013-1-11 11:29:11
|
显示全部楼层
这个你可以选择自己读取后排序,或者存储Json数据
你说的这个有例子如下:
[code=csharp]
//商品历史浏览记录
public class HistoryCookieUtil {
private final static String HISTORY_PRE = "history_"; //Cookie中,历史浏览
private final static String HISTORY_PATH = "/"; //Cookie中,历史浏览的Cookie的路径
//清除所有历史浏览记录
public static void removeAllHistoryCookie(HttpServletRequest request,HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
if (null == cookies || cookies.length == 0) {
return;
}
for (Cookie thisCookie : cookies) {
if (thisCookie.getName().startsWith(HISTORY_PRE)) {
thisCookie.setMaxAge(0); // 删除这个cookie
thisCookie.setPath(HISTORY_PATH);
response.addCookie(thisCookie);
}
}
}
//清除单个商品的浏览记录
public static void removeHistoryCookie(HttpServletRequest request,HttpServletResponse response, Long goodsId) {
if (goodsId == null) {
return;
}
Cookie[] cookies = request.getCookies();
if (null == cookies || cookies.length == 0) {
return;
}
for (Cookie thisCookie : cookies) {
if (thisCookie.getName().equals(HISTORY_PRE + goodsId)) {
thisCookie.setMaxAge(0); // 删除这个cookie
thisCookie.setPath(HISTORY_PATH);
response.addCookie(thisCookie);
}
}
}
//加入单个商品浏览记录到cookie(有效期30天)
public static void setHistoryCookie(HttpServletRequest request,HttpServletResponse response, Long goodsId) {
if (goodsId == null) {
return;
}
String nameString = HISTORY_PRE + goodsId.toString();
Cookie cookie = new Cookie(nameString, goodsId.toString());
cookie.setPath(HISTORY_PATH);
cookie.setMaxAge(3600 * 24 * 30); // 30 days
response.addCookie(cookie);
}
public static void addHistoryCookie(HttpServletRequest request,HttpServletResponse response,ServletContext servletContext, Long goodsId) {
if (goodsId == null ) {
return;
}
Integer viewNum = 10;//页面商品显示的个数(默认显示10)
String thetype = EnumBoardName.toMap().get("browse_history");//汉字表示
BoardDao boardDao = (BoardDao) getBean(servletContext, "boardDao");
Board board = boardDao.getBoardByName(thetype);
if(null != board && null != board.getAmount()){
viewNum = board.getAmount();
}
String nameString = HISTORY_PRE + goodsId.toString();
Cookie[] cookies = request.getCookies();
int historyCookieNum = 0;//历史浏览的当前 cookie 数目
if (null != cookies && cookies.length > 0) {
for (Cookie thisCookie : cookies) {
if (thisCookie.getName().startsWith(HISTORY_PRE)) {
historyCookieNum++;
}
}
}
if(historyCookieNum<viewNum){//小于显示数目时候
String flagexist ="no";//标记cookie中是否存在相同的历史浏览商品
for (Cookie thisCookie : cookies) {
if (thisCookie.getName().equalsIgnoreCase(nameString)) {//如果有相同的记录先移去 再加上
removeHistoryCookie(request, response, goodsId);
setHistoryCookie(request, response, goodsId);
flagexist ="yes";
break;
}
}
if(flagexist.equals("no")){//cookie中不存在则直接加入cookie
setHistoryCookie(request, response, goodsId);
}
}
if(historyCookieNum == viewNum){//等于显示数目的时候移去最旧一个记录 加入新的记录
List<String> list = new ArrayList<String>();//存放当前的历史cookie
for (Cookie thisCookie : cookies) {
if (thisCookie.getName().startsWith(HISTORY_PRE)) {//
String oldGoodsId = thisCookie.getValue();//
list.add(oldGoodsId);
}
}
if(list.contains(goodsId.toString())){//判断(防止详情页面不断刷新调用addcookie方法)
removeHistoryCookie(request, response, goodsId);
setHistoryCookie(request, response, goodsId);
}else{
for (Cookie thisCookie : cookies) {
if (thisCookie.getName().startsWith(HISTORY_PRE)) {//去掉第一条旧的历史记录
String oldGoodsId = thisCookie.getValue();//旧的商品ID
removeHistoryCookie(request, response, Long.valueOf(oldGoodsId));
setHistoryCookie(request, response, goodsId);
break;
}
}
}
}
if(historyCookieNum > viewNum){//考虑后台重新设置历史显示数目变小后的情况,历史cookie中存在的数目大于设置显示的数目后要重新获取前viewNum条历史记录
removeAllHistoryCookie(request, response);//先移除所有历史浏览记录
int addNewNum = 0 ;
for (int i=cookies.length-1;i>0;i-- ) {
if(viewNum == addNewNum){//获取到设置数目的记录后跳出
break;
}
if (cookies.getName().startsWith(HISTORY_PRE)) {
String getnewgoodsId = cookies.getValue();
if(null != getnewgoodsId && getnewgoodsId.length()>0){
setHistoryCookie(request, response, Long.valueOf(getnewgoodsId));
addNewNum++ ;
}
}
}
}
}
public static List<RankGoods> getHistoryRankList(HttpServletRequest request,ServletContext servletContext) {
List<RankGoods> list = new ArrayList<RankGoods>();
Cookie[] cookies = request.getCookies();
if (null == cookies || cookies.length < 1) {
return null;
}
String thetype = EnumBoardName.toMap().get("browse_history");//汉字表示
String rankTitle = "历史浏览";//榜行标题
Integer viewlistNum = 10;//页面商品显示的个数(默认显示10)
BoardDao boardDao = (BoardDao) getBean(servletContext, "boardDao");
Board board = boardDao.getBoardByName(thetype);
if(null != board && null != board.getAmount()){
viewlistNum = board.getAmount();
rankTitle = board.getTitle();
}
GoodsDao goodsDao = (GoodsDao) getBean(servletContext, "goodsDao");
int viewHistoryNum = 0 ;
for (int i=cookies.length-1;i>0;i-- ) {//倒序排列
if(viewlistNum == viewHistoryNum){//等于后台设置显示数目时不在显示
break;
}
if (cookies.getName().startsWith(HISTORY_PRE)) {
String goodsId = cookies.getValue();
if (goodsId == null || goodsId.trim().length()<1) {
continue;
}
Long goodsId2 = Long.valueOf(goodsId);
Goods goods = new Goods();
try {
goods = goodsDao.getGoods(goodsId2);
} catch (Exception e) {
e.printStackTrace();
}
RankGoods hgs = new RankGoods();
hgs.setGoodsId(String.valueOf(goods.getId()));//商品Id
............;
list.add(hgs);
viewHistoryNum ++ ;
}
}
return list;
}
}
[/code] |
|