第九天-商品详情页面的展示

1.商品搜索的实现

1.1. 需求分析

用户在首页中输入查询条件,点击查询向taotao-portal发送请求,参数就是查询的条件,页码。Taoto-portal调用taotao-search发布的服务进行搜索,参数应该是查询条件和页码及每页显示的记录数(参数可选)。Taotao-search返回一个json格式的数据(TaotaoResult包装一个SearchResult对象)。Taotao-portal接收json数据需要把json数据转换成java对象。把java对象传递给jsp页面,jsp渲染java对象得到商品查询结果页面。

请求url:http://localhost:8082/search.html?q=查询条件

1.2 网页搜索框

header.jsp

/taotao-portal/src/main/webapp/WEB-INF/jsp/commons/header.jsp

base-v1.js

/taotao-portal/src/main/webapp/js/base-v1.js

参数:q:查询条件

返回结果:商品列表页面(jsp)

search.jsp

/taotao-portal/src/main/webapp/WEB-INF/jsp/search.jsp

Query:回显的查询条件

totalPages:总页数

itemList:商品列表

Page:当前页码

翻页处理:

1.3. Dao层

木有

1.4. Service层

接收两个参数1、查询条件2、页码。调用taotao-search的搜索服务。接收返回的json数据,把json转换成java对象返回SearchResult对象。

从taotao-search中复制SearchResult和Item两个pojo类。

SearchService.java

/taotao-portal/src/main/java/com/taotao/portal/service/SearchService.java

package com.taotao.portal.service;

import com.taotao.portal.pojo.SearchResult;

public interface SearchService {
	SearchResult search(String queryString, int page);
}

SearchServiceImpl.java

/taotao-portal/src/main/java/com/taotao/portal/service/impl/SearchServiceImpl.java

package com.taotao.portal.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.HttpClientUtil;
import com.taotao.common.utill.TaotaoResult;
import com.taotao.portal.pojo.SearchResult;
import com.taotao.portal.service.SearchService;

/*
 *  商品搜索Service
 */
@Service
public class SearchServiceImpl implements SearchService {
	@Value("${SEARCH_BASE_URL}")
	private String SEARCH_BASE_URL;

	@Override
	public SearchResult search(String queryString, int page) {
		// 调用taotao-search的服务
		// 查询参数
		Map<String, String> param = new HashMap<>();
		param.put("q", queryString);
		param.put("page", page + "");
		try {
			// 调用服务
			String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
			// 把字符串转换成java对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, SearchResult.class);
			if (taotaoResult.getStatus() == 200) {
				SearchResult result = (SearchResult) taotaoResult.getData();
				return result;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}


resource.properties

/taotao-portal/src/main/resources/resource/resource.properties

#搜索服务基础url
SEARCH_BASE_URL=http://localhost:8083/search/query

1.5. Controller层

功能:接收请求的参数查询条件和页码。调用Service查询商品列表得到SearchResult对象。

需要把

Query:回显的查询条件

totalPages:总页数

itemList:商品列表

Page:当前页码

传递到页面。返回一个逻辑视图search字符串。

SearchController.java

/taotao-portal/src/main/java/com/taotao/portal/controller/SearchController.java

package com.taotao.portal.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.portal.pojo.SearchResult;
import com.taotao.portal.service.SearchService;

/**
 * 商品搜索Controller
 * @author Lenovo
 *
 */
@Controller
public class SearchController {

	@Autowired
	private SearchService searchService;

	@RequestMapping("/search")
	public String search(@RequestParam("q") String queryString, @RequestParam(defaultValue = "1") Integer page,
			Model model) {
		if (queryString != null) {
			try {
				queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		SearchResult searchResult = searchService.search(queryString, page);
		// 向页面传递参数
		model.addAttribute("query", queryString);
		model.addAttribute("totalPages", searchResult.getPageCount());
		model.addAttribute("itemList", searchResult.getItemList());
		model.addAttribute("page", page);
		return "search";
	}
}


访问:http://localhost:8082/search.html?q=奶粉

1.6. 容易存在的问题

搜索结果中图片展示不出来,image字段中存储的图片是多张,使用逗号分隔。

修改方法:

Item.java

/taotao-portal/src/main/java/com/taotao/portal/pojo/Item.java

public String[] getImages() {
	if (image!=null) {
		String[] images=image.split(",");
		return images;
	}
	return null;
}

search.jsp

/taotao-portal/src/main/webapp/WEB-INF/jsp/search.jsp

2. 商品详情页面展示

2.1. 需求分析

需要在taotao-portal中调用taotao-rest发布的服务,查询商品详情。

1、商品的基本信息

2、商品的描述

3、商品的规格

当用户请求商品详情页面时,只需要把商品基本信息展示出来,为了快速响应用户。商品的描述可以延迟加载,延迟一秒钟加载。商品的规格参数按需加载,当用户点击商品规格参数的标签页此时加载。

2.2. 服务发布

需要在taotao-rest工程中发布服务

1、取商品基本信息的服务

2、取商品描述的服务

3、取商品规格的服务

需要把商品信息添加到缓存中。设置商品的过期时间,过期时间为一天。需要缓存同步。

2.2.1. 取商品基本信息

2.2.1.1 Dao层

查询的表tb_item:

2.2.1.2Service层

接收商品id,根据商品id查询商品基本信息。返回一个商品的pojo,使用taotaoResult包装返回。

ItemService.java

/taotao-rest/src/main/java/com/taotao/rest/service/ItemService.java

package com.taotao.rest.service;

import com.taotao.common.utill.TaotaoResult;

public interface ItemService {
	TaotaoResult getItemBaseInfo(long itemId);
}

ItemServiceImpl.java

/taotao-rest/src/main/java/com/taotao/rest/service/impl/ItemServiceImpl.java

package com.taotao.rest.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.TaotaoResult;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.rest.service.ItemService;

/**
 * 商品信息管理Service
 * @author Lenovo
 *
 */
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
	@Override
	public TaotaoResult getItemBaseInfo(long itemId) {
		// 根据商品id查询商品信息
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		// 使用TaotaoResult包装一下
		return TaotaoResult.ok(item);
	}
}


2.2.1.3 Controller层

接收商品id调用Service查询商品信息,返回商品对象,使用TaotaoResult包装。

Url:/rest/item/info/{itemId}

ItemController.java

/taotao-rest/src/main/java/com/taotao/rest/controller/ItemController.java

package com.taotao.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.utill.TaotaoResult;
import com.taotao.rest.service.ItemService;

/**
 * 商品信息Controller
 * @author Lenovo
 *
 */
@Controller
@RequestMapping("/item")
public class ItemController {

	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/info/{itemId}")
	@ResponseBody
	public TaotaoResult getItemBaseInfo(@PathVariable Long itemId) {
		TaotaoResult result = itemService.getItemBaseInfo(itemId);
		return result;
	}
}


访问http://localhost:8081/rest/item/info/1029944528

2.2.1.4 添加缓存逻辑

Redis的hash类型中的key是不能设置过期时间。如果还需要对key进行分类可以使用折中的方案。

Key的命名方式:

Itheima:javaee16:01=袁飞

Itheima:javaee16:02=张飞

商品key的定义:

基本信息:

REDIS_ITEM_KEY:商品id:base=json

描述:

REDIS_ITEM_KEY:商品id:desc=json

规格参数:

REDIS_ITEM_KEY:商品id:param=json

resource.properties

【/taotao-rest】—【/src/main/resources】—【resource/】—【resource.properties】

#商品信息在redis中保存的key
REDIS_ITEM_KEY=REDIS_ITEM_KEY
#商品信息在redis中的过期时间,默认为一天时间60*60*24
REDIS_ITEM_EXPIRE=86400
ItemServiceImpl.java

/taotao-rest/src/main/java/com/taotao/rest/service/impl/ItemServiceImpl.java

package com.taotao.rest.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.JsonUtils;
import com.taotao.common.utill.TaotaoResult;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.ItemService;

/**
 * 商品信息管理Service
 * @author Lenovo
 *
 */
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;
	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Override
	public TaotaoResult getItemBaseInfo(long itemId) {
		try {
			//添加缓存逻辑
			//从缓存中取商品信息,商品id对应的信息
			String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":base");
			//判断是否有值
			if (!StringUtils.isBlank(json)) {
				//把json转换成java对象
				TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
				return TaotaoResult.ok(item);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//根据商品id查询商品信息
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		//使用TaotaoResult包装一下
		try {
			//把商品信息写入缓存
			jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":base", JsonUtils.objectToJson(item));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":base", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok(item);
	}

}

2.2.2. 取商品描述信息

根据商品id取商品描述信息。单表查询tb_item_desc。

2.2.2.1.Dao层

使用逆向工程

2.2.2.2.Service层

接收商品id根据商品id查询商品描述。返回商品描述的pojo。使用TaotaoResult包装。

需要添加缓存逻辑。

ItemService.java

/taotao-rest/src/main/java/com/taotao/rest/service/ItemService.java

package com.taotao.rest.service;

import com.taotao.common.utill.TaotaoResult;

public interface ItemService {
//	取商品基本信息
	TaotaoResult getItemBaseInfo(long itemId);
//	取商品描述信息
	TaotaoResult getItemDesc(long itemId);
}

ItemServiceImpl.java

/taotao-rest/src/main/java/com/taotao/rest/service/impl/ItemServiceImpl.java

package com.taotao.rest.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.JsonUtils;
import com.taotao.common.utill.TaotaoResult;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.ItemService;

/**
 * 商品信息管理Service
 * @author Lenovo
 *
 */
@Service
public class ItemServiceImpl implements ItemService {

//	取商品基本信息
	@Autowired
	private TbItemMapper itemMapper;
	
//	取商品描述信息
	@Autowired
	private TbItemDescMapper itemDescMapper;
	
	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;
	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;
	
	@Autowired
	private JedisClient jedisClient;
	
//	取商品基本信息
	@Override
	public TaotaoResult getItemBaseInfo(long itemId) {
		try {
			//添加缓存逻辑
			//从缓存中取商品信息,商品id对应的信息
			String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":base");
			//判断是否有值
			if (!StringUtils.isBlank(json)) {
				//把json转换成java对象
				TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
				return TaotaoResult.ok(item);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//根据商品id查询商品信息
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		//使用TaotaoResult包装一下
		try {
			//把商品信息写入缓存
			jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":base", JsonUtils.objectToJson(item));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":base", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok(item);
	}

//	取商品描述信息
	@Override
	public TaotaoResult getItemDesc(long itemId) {
		//添加缓存
		try {
			//添加缓存逻辑
			//从缓存中取商品信息,商品id对应的信息
			String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":desc");
			//判断是否有值
			if (!StringUtils.isBlank(json)) {
				//把json转换成java对象
				TbItemDesc itemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
				return TaotaoResult.ok(itemDesc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//创建查询条件
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		
		try {
			//把商品信息写入缓存
			jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":desc", JsonUtils.objectToJson(itemDesc));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":desc", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return TaotaoResult.ok(itemDesc);
	}


}

2.2.2.3 Controller层

接收商品id调用Service查询商品信息,返回商品对象,使用TaotaoResult包装。

Url:/rest/item/info/{itemId}

ItemController.java

/taotao-rest/src/main/java/com/taotao/rest/controller/ItemController.java

package com.taotao.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.utill.TaotaoResult;
import com.taotao.rest.service.ItemService;

/**
 * 商品信息Controller
 * @author Lenovo
 *
 */
@Controller
@RequestMapping("/item")
public class ItemController {

	@Autowired
	private ItemService itemService;
	
//	取商品基本信息
	@RequestMapping("/info/{itemId}")
	@ResponseBody
	public TaotaoResult getItemBaseInfo(@PathVariable Long itemId) {
		TaotaoResult result = itemService.getItemBaseInfo(itemId);
		return result;
	}
	
//	取商品描述信息
	@RequestMapping("/desc/{itemId}")
	@ResponseBody
	public TaotaoResult getItemDesc(@PathVariable Long itemId) {
		TaotaoResult result = itemService.getItemDesc(itemId);
		return result;
	}

}


访问地址http://localhost:8081/rest/item/desc/1029944528

2.2.3 取商品规格参数

需要从tb_item_param_item表中根据商品id取出商品的规格参数信息。返回pojo对象,使用TaotaoResult包装。

2.2.3.1.Dao层

使用逆向工程

2.2.3.2.Service层

接收商品id调用mapper查询商品规格参数,返回规格参数pojo使用TaotaoResult包装。

添加缓存逻辑。

ItemService.java

/taotao-rest/src/main/java/com/taotao/rest/service/ItemService.java

package com.taotao.rest.service;

import com.taotao.common.utill.TaotaoResult;

public interface ItemService {
//	取商品基本信息
	TaotaoResult getItemBaseInfo(long itemId);
//	取商品描述信息
	TaotaoResult getItemDesc(long itemId);
//	取商品规格参数
	TaotaoResult getItemParam(long itemId);
}

ItemServiceImpl.java

/taotao-rest/src/main/java/com/taotao/rest/service/impl/ItemServiceImpl.java

package com.taotao.rest.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.JsonUtils;
import com.taotao.common.utill.TaotaoResult;
import com.taotao.mapper.TbItemDescMapper;
import com.taotao.mapper.TbItemMapper;
import com.taotao.mapper.TbItemParamItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.pojo.TbItemParamItemExample;
import com.taotao.pojo.TbItemParamItemExample.Criteria;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.ItemService;

/**
 * 商品信息管理Service
 * @author Lenovo
 *
 */
@Service
public class ItemServiceImpl implements ItemService {

//	取商品基本信息
	@Autowired
	private TbItemMapper itemMapper;
	
//	取商品描述信息
	@Autowired
	private TbItemDescMapper itemDescMapper;
	
//	取商品规格参数
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper;
	
	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;
	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;
	
	@Autowired
	private JedisClient jedisClient;
	
//	取商品基本信息
	@Override
	public TaotaoResult getItemBaseInfo(long itemId) {
		try {
			//添加缓存逻辑
			//从缓存中取商品信息,商品id对应的信息
			String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":base");
			//判断是否有值
			if (!StringUtils.isBlank(json)) {
				//把json转换成java对象
				TbItem item = JsonUtils.jsonToPojo(json, TbItem.class);
				return TaotaoResult.ok(item);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//根据商品id查询商品信息
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		//使用TaotaoResult包装一下
		try {
			//把商品信息写入缓存
			jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":base", JsonUtils.objectToJson(item));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":base", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TaotaoResult.ok(item);
	}

//	取商品描述信息
	@Override
	public TaotaoResult getItemDesc(long itemId) {
		//添加缓存
		try {
			//添加缓存逻辑
			//从缓存中取商品信息,商品id对应的信息
			String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":desc");
			//判断是否有值
			if (!StringUtils.isBlank(json)) {
				//把json转换成java对象
				TbItemDesc itemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
				return TaotaoResult.ok(itemDesc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//创建查询条件
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		
		try {
			//把商品信息写入缓存
			jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":desc", JsonUtils.objectToJson(itemDesc));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":desc", REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return TaotaoResult.ok(itemDesc);
	}

//	取商品规格参数
	@Override
	public TaotaoResult getItemParam(long itemId) {
		//添加缓存
		try {
			//添加缓存逻辑
			//从缓存中取商品信息,商品id对应的信息
			String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":param");
			//判断是否有值
			if (!StringUtils.isBlank(json)) {
				//把json转换成java对象
				TbItemParamItem paramItem = JsonUtils.jsonToPojo(json, TbItemParamItem.class);
				return TaotaoResult.ok(paramItem);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//根据商品id查询规格参数
		//设置查询条件
		TbItemParamItemExample example = new TbItemParamItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		//执行查询
		List<TbItemParamItem> list = itemParamItemMapper.selectByExampleWithBLOBs(example);
		if (list != null && list.size()>0) {
			TbItemParamItem paramItem = list.get(0);
			try {
				//把商品信息写入缓存
				jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":param", JsonUtils.objectToJson(paramItem));
				//设置key的有效期
				jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":param", REDIS_ITEM_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return TaotaoResult.ok(paramItem);
		}
		return TaotaoResult.build(400, "无此商品规格");
	}

}

2.2.2.3 Controller层

接收商品id调用Service查询商品信息,返回商品对象,使用TaotaoResult包装。

Url:/rest/item/info/{itemId}

ItemController.java

/taotao-rest/src/main/java/com/taotao/rest/controller/ItemController.java

package com.taotao.rest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.utill.TaotaoResult;
import com.taotao.rest.service.ItemService;

/**
 * 商品信息Controller
 * @author Lenovo
 *
 */
@Controller
@RequestMapping("/item")
public class ItemController {

	@Autowired
	private ItemService itemService;
	
//	取商品基本信息
	@RequestMapping("/info/{itemId}")
	@ResponseBody
	public TaotaoResult getItemBaseInfo(@PathVariable Long itemId) {
		TaotaoResult result = itemService.getItemBaseInfo(itemId);
		return result;
	}
	
//	取商品描述信息
	@RequestMapping("/desc/{itemId}")
	@ResponseBody
	public TaotaoResult getItemDesc(@PathVariable Long itemId) {
		TaotaoResult result = itemService.getItemDesc(itemId);
		return result;
	}

//	取商品规格参数
	@RequestMapping("/param/{itemId}")
	@ResponseBody
	public TaotaoResult getItemParam(@PathVariable Long itemId) {
		TaotaoResult result = itemService.getItemParam(itemId);
		return result;
	}

}


访问地址http://localhost:8081/rest/item/param/1029944528

2.3 使用taotao-portal调用服务

2.3.1. 需求分析

当用户访问商品详情页面时,需要加载商品基本信息。延迟加载商品描述、按需加载商品的规格参数。

2.3.2. 商品基本信息的查询

当商品页面展示时,数据已经到位。

请求的url:/item/{itemId}.html

search.jsp

/taotao-portal/src/main/webapp/WEB-INF/jsp/search.jsp

商品详情页面链接

2.3.2.1 Dao层

没有

2.3.2.2 Service层

接收商品id,调用taotao-rest的服务,查询商品的基本信息。得到一个json字符串。需要把json转换成java对象。然后在jsp页面渲染。

ItemInfo.java

/taotao-portal/src/main/java/com/taotao/portal/pojo/ItemInfo.java

package com.taotao.portal.pojo;

import com.taotao.pojo.TbItem;

public class ItemInfo extends TbItem {
//	商品基本信息的查询
	public String[] getImages() {
		String image = getImage();
		if (image != null) {
			String[] images = image.split(",");
			return images;
		}
		return null;
	}
}


ItemService.java

/taotao-portal/src/main/java/com/taotao/portal/service/ItemService.java

package com.taotao.portal.service;

import com.taotao.portal.pojo.ItemInfo;

public interface ItemService {
//	商品基本信息的查询
	ItemInfo getItemById(Long itemId);
}

ItemServiceImpl.java

/taotao-portal/src/main/java/com/taotao/portal/service/impl/ItemServiceImpl.java

package com.taotao.portal.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.HttpClientUtil;
import com.taotao.common.utill.TaotaoResult;
import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;

/**
 * 商品信息管理Service
 * @author Lenovo
 *
 */
@Service
public class ItemServiceImpl implements ItemService {
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${ITEM_INFO_URL}")
	private String ITEM_INFO_URL;
	
//	商品基本信息的查询
	@Override
	public ItemInfo getItemById(Long itemId) {
		try {
			//调用rest的服务查询商品基本信息
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_INFO_URL + itemId);
			if (!StringUtils.isBlank(json)) {
				TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, ItemInfo.class);
				if (taotaoResult.getStatus() == 200) {
					ItemInfo item = (ItemInfo) taotaoResult.getData();
					return item;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}


resource.properties

【/taotao-portal】—【/src/main/resources/】—【resource/】—【resource.properties】

#商品基本信息
ITEM_INFO_URL=/item/info/
2.3.2.3 Controller层

接收页面传递过来的商品id,调用Service查询商品基本信息。传递给jsp页面。返回逻辑视图,展示商品详情页面。

ItemController.java

/taotao-portal/src/main/java/com/taotao/portal/controller/ItemController.java

package com.taotao.portal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;

/*
 *  商品详情页面展示
 */
@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;

//	商品基本信息的查询
	@RequestMapping("/item/{itemId}")
	public String showItem(@PathVariable Long itemId, Model model) {
		ItemInfo item = itemService.getItemById(itemId);
		model.addAttribute("item", item);
		
		return "item";
	}
}


http://localhost:8082

2.3.3 . 商品描述延迟加载

当商品详情页面加载完毕后延迟一秒钟ajax请求商品详情。

请求的URL:/item/desc/{itemId}.html

参数:商品id

返回值:商品描述信息(html片段)

item.jsp

/taotao-portal/src/main/webapp/WEB-INF/jsp/item.jsp

2.3.3.1 Dao层

没有

2.3.3.2.Service层

接收商品id,调用taotao-rest的服务根据商品id查询商品描述信息。得到json数据。把json转换成java对象从java对象中把商品描述取出来。返回商品描述字符串。

参数:商品id

返回值:字符串(商品描述的html片段)

ItemService.java

/taotao-portal/src/main/java/com/taotao/portal/service/ItemService.java

package com.taotao.portal.service;

import com.taotao.portal.pojo.ItemInfo;

public interface ItemService {
//	商品基本信息的查询
	ItemInfo getItemById(Long itemId);
	
//	商品描述延迟加载
	String getItemDescById(Long itemId);
}

ItemServiceImpl.java

/taotao-portal/src/main/java/com/taotao/portal/service/impl/ItemServiceImpl.java

package com.taotao.portal.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.HttpClientUtil;
import com.taotao.common.utill.TaotaoResult;
import com.taotao.pojo.TbItemDesc;
import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;

/**
 * 商品信息管理Service
 * @author Lenovo
 *
 */
@Service
public class ItemServiceImpl implements ItemService {
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${ITEM_INFO_URL}")
	private String ITEM_INFO_URL;
	
	@Value("${ITEM_DESC_URL}")
	private String ITEM_DESC_URL;

	
//	商品基本信息的查询
	@Override
	public ItemInfo getItemById(Long itemId) {
		try {
			//调用rest的服务查询商品基本信息
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_INFO_URL + itemId);
			if (!StringUtils.isBlank(json)) {
				TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, ItemInfo.class);
				if (taotaoResult.getStatus() == 200) {
					ItemInfo item = (ItemInfo) taotaoResult.getData();
					return item;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	
//	商品描述延迟加载
	@Override
	public String getItemDescById(Long itemId) {
		try {
			//查询商品描述
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_DESC_URL + itemId);
			//转换成java对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemDesc.class);
			if (taotaoResult.getStatus() == 200) {
				TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
				//取商品描述信息
				String result = itemDesc.getItemDesc();
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}


resource.properties

【/taotao-portal】—【/src/main/resources/】—【resource/】—【resource.properties】

#商品描述的url
ITEM_DESC_URL=/item/desc/
2.3.3.3 Controller层

接收商品id,调用Service查询商品的描述信息,返回一个字符串,是商品描述的片段。需要使用@ResponseBody。

ItemController.java

/taotao-portal/src/main/java/com/taotao/portal/controller/ItemController.java

package com.taotao.portal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;

/*
 *  商品详情页面展示
 */
@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;

//	商品基本信息的查询
	@RequestMapping("/item/{itemId}")
	public String showItem(@PathVariable Long itemId, Model model) {
		ItemInfo item = itemService.getItemById(itemId);
		model.addAttribute("item", item);

		return "item";
	}

//	商品描述延迟加载
	@RequestMapping(value = "/item/desc/{itemId}", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
	@ResponseBody
	public String getItemDesc(@PathVariable Long itemId) {
		String string = itemService.getItemDescById(itemId);
		return string;
	}

}

http://localhost:8082

2.3.4 商品规格参数展示

按需加载。当用户点击规格参数tab页时触发一个单击事件,在事件中异步加载规格参数信息。规格参数内容是html片段。返回字符串。

2.3.4.1.Dao层

没有

2.3.4.2.Service层

接收商品id,根据商品id查询规格参数的数据,调用服务端的方法,返回json数据。把json转换成java对象,根据java对象生成html片段,返回。

参数:商品id

返回值:字符串(规格参数html)

ItemService.java

/taotao-portal/src/main/java/com/taotao/portal/service/ItemService.java

package com.taotao.portal.service;

import com.taotao.portal.pojo.ItemInfo;

public interface ItemService {
//	商品基本信息的查询
	ItemInfo getItemById(Long itemId);
	
//	商品描述延迟加载
	String getItemDescById(Long itemId);
	
//	商品规格参数展示
	String getItemParam(Long itemId);
}

ItemServiceImpl.java

/taotao-portal/src/main/java/com/taotao/portal/service/impl/ItemServiceImpl.java

package com.taotao.portal.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.utill.HttpClientUtil;
import com.taotao.common.utill.JsonUtils;
import com.taotao.common.utill.TaotaoResult;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;

/**
 * 商品信息管理Service
 * @author Lenovo
 *
 */
@Service
public class ItemServiceImpl implements ItemService {
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${ITEM_INFO_URL}")
	private String ITEM_INFO_URL;
	
	@Value("${ITEM_DESC_URL}")
	private String ITEM_DESC_URL;
	
	@Value("${ITEM_PARAM_URL}")
	private String ITEM_PARAM_URL;


	
//	商品基本信息的查询
	@Override
	public ItemInfo getItemById(Long itemId) {
		try {
			//调用rest的服务查询商品基本信息
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_INFO_URL + itemId);
			if (!StringUtils.isBlank(json)) {
				TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, ItemInfo.class);
				if (taotaoResult.getStatus() == 200) {
					ItemInfo item = (ItemInfo) taotaoResult.getData();
					return item;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	
//	商品描述延迟加载
	@Override
	public String getItemDescById(Long itemId) {
		try {
			//查询商品描述
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_DESC_URL + itemId);
			//转换成java对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemDesc.class);
			if (taotaoResult.getStatus() == 200) {
				TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
				//取商品描述信息
				String result = itemDesc.getItemDesc();
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

//	 根据商品id查询规格参数
//	商品规格参数展示
	@Override
	public String getItemParam(Long itemId) {
		try {
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_PARAM_URL + itemId);
			//把json转换成java对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemParamItem.class);
			if (taotaoResult.getStatus() == 200) {
				TbItemParamItem itemParamItem = (TbItemParamItem) taotaoResult.getData();
				String paramData = itemParamItem.getParamData();
				//生成html
				// 把规格参数json数据转换成java对象
				List<Map> jsonList = JsonUtils.jsonToList(paramData, Map.class);
				StringBuffer sb = new StringBuffer();
				sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n");
				sb.append("    <tbody>\n");
				for(Map m1:jsonList) {
					sb.append("        <tr>\n");
					sb.append("            <th class=\"tdTitle\" colspan=\"2\">"+m1.get("group")+"</th>\n");
					sb.append("        </tr>\n");
					List<Map> list2 = (List<Map>) m1.get("params");
					for(Map m2:list2) {
						sb.append("        <tr>\n");
						sb.append("            <td class=\"tdTitle\">"+m2.get("k")+"</td>\n");
						sb.append("            <td>"+m2.get("v")+"</td>\n");
						sb.append("        </tr>\n");
					}
				}
				sb.append("    </tbody>\n");
				sb.append("</table>");
				//返回html片段
				return sb.toString();
			}
				 
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "";
	}


}


resource.properties

【/taotao-portal】—【/src/main/resources/】—【resource/】—【resource.properties】

#商品规格的url
ITEM_PARAM_URL=/item/param/
2.3.4.3 Controller层

页面的ajax请求Controller,请求的url://item/param/{itemId}.html

响应一个字符串。规格参数的片段。@ResponseBody

ItemController.java

/taotao-portal/src/main/java/com/taotao/portal/controller/ItemController.java

package com.taotao.portal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;

/*
 *  商品详情页面展示
 */
@Controller
public class ItemController {
	@Autowired
	private ItemService itemService;

//	商品基本信息的查询
	@RequestMapping("/item/{itemId}")
	public String showItem(@PathVariable Long itemId, Model model) {
		ItemInfo item = itemService.getItemById(itemId);
		model.addAttribute("item", item);

		return "item";
	}

//	商品描述延迟加载
	@RequestMapping(value = "/item/desc/{itemId}", produces = MediaType.TEXT_HTML_VALUE + ";charset=utf-8")
	@ResponseBody
	public String getItemDesc(@PathVariable Long itemId) {
		String string = itemService.getItemDescById(itemId);
		return string;
	}

	
//	商品规格参数展示
	@RequestMapping(value="/item/param/{itemId}", produces=MediaType.TEXT_HTML_VALUE+";charset=utf-8")
	@ResponseBody
	public String getItemParam(@PathVariable Long itemId) {
		String string = itemService.getItemParam(itemId);
		return string;
	}


}

2.3.4.4 jsp

响应规格参数标签页的点击事件,在事件中进行ajax请求规格参数。只需要请求一次即可。

给tab页dom节点绑定单击事件:

请求规格参数的方法:

绑定事件

http://localhost:8082

おすすめ

転載: blog.csdn.net/weixin_46267445/article/details/121292201