实战项目商城(第六天)

内容管理系统
思路:
1、 分析每个模块的共性
a) 链接
b) 图片
c) 标题
d) 子标题
2、 使用两张表来管理
a) 内容分类表,管理内容的大分类

b) 内容表,存储每个分类下的明细信息内容。

初始化树形视图的url:/content/category/list

dao层:


service层:

功能:接收parentid。根据parentid查询节点列表,返回一个EasyUI异步Tree要求的节点列表。每个节点包含三个属性id,text,state三个属性。可以使用EUTreeNode。

参数:id

返回值:List<EUTreeNode>

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{

	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	
	@Override
	public List<EUTreeNode> getCategoryList(long parentId) {
		//根据parentId查询节点列表
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		List<EUTreeNode> treeNode = new ArrayList<>();
		for (TbContentCategory tbContentCategory : list) {
			EUTreeNode eutNode = new EUTreeNode();
			eutNode.setId(tbContentCategory.getId());
			eutNode.setText(tbContentCategory.getName());
			eutNode.setState(tbContentCategory.getIsParent()?"closed":"open");
			treeNode.add(eutNode);
		}
		return treeNode;
	}
Controller层:
接收页面传递过来的parentid,根据parentid查询 节点 列表。 返回List<EUTreeNode>。需要响应json数据。

@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {

	@Autowired
	private ContentCategoryService contentCategoryService;
	
	@RequestMapping("/list")
	@ResponseBody
	public List<EUTreeNode> getContentCatList(@RequestParam(value="id", defaultValue="0") long parentId){
		List<EUTreeNode> categoryList = contentCategoryService.getCategoryList(parentId);
		
		return categoryList;
		
	}
}
内容分类添加
需求分析

请求的url:/content/category/create

参数:

1.parentId父节点id 2.name:当前节点的名称

返回值:TaotaoResult。其中包含节点pojo对象。

Dao层:

使用逆向工程生成的代码

Service层:

功能:接收两个参数parentId父节点id,name:当前节点的名称。向tb_content_category表中添加一条记录。返回TaotaoResult包含记录的pojo对象。

@Override
	public TaotaoResult insertContentCategory(long parentId, String name) {
		//创建一个pojo
		TbContentCategory category = new TbContentCategory();
		category.setName(name);
		category.setIsParent(false);
		category.setStatus(1);
		category.setParentId(parentId);
		category.setSortOrder(1);
		category.setCreated(new Date());
		category.setUpdated(new Date());
		//添加记录
		contentCategoryMapper.insert(category);
		//查看父节点的isParent列是否为true,如果不是true改为true
		TbContentCategory parentCat = contentCategoryMapper.selectByPrimaryKey(parentId);
		if(!parentCat.getIsParent()){
			parentCat.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKey(parentCat);
		}
		return TaotaoResult.ok(category);
	}

controller层:

接收两个参数parentid,name。调用Service添加记录。返回TaotaoResult。应该返回json数据。

@RequestMapping("/create")
	@ResponseBody
	public TaotaoResult createContentCategory(Long parentId, String name){
		
		TaotaoResult result = contentCategoryService.insertContentCategory(parentId, name);
		return result;
		
	}
内容分类删除
需求分析:
请求的url:/content/category/delete/

参数:parentId  ,id

返回值:TaotaoResult

业务逻辑:

接收parentid、id两个参数。删除id对应的记录。需要判断parentid对应的记录下是否有子节点。如果没有子节点。需要把parentid对应的记录的isparent改成false。

注意:删除直接是物理删除。

重命名节点

请求的url:/content/category/update
参数:id、name
返回值:返回TaotaoResult。Json格式
业务逻辑:根据id更新记录的name列即可。

内容管理

内容管理表


内容添加

请求的url:/content/save

请求的方法:post

请求内容:表单中的内容。

返回的结果:TaotaoResult。

 Dao层

向tb_content表中插入数据。可以使用逆向工程生成的代码。

Service层 
接收表tb_content对应的pojo对象。把pojo对象插入到tb_content表中。
返回TaotaoResult。

@Service
public class ContentServiceImpl implements ContentService{

	@Autowired
	private TbContentMapper contentMapper;
	@Override
	public TaotaoResult insertContent(TbContent tbContent) {
		tbContent.setCreated(new Date());
		tbContent.setUpdated(new Date());
		contentMapper.insert(tbContent);
		
		return TaotaoResult.ok();
	}

controller层

接收表单中的内容,使用pojo接收,要求pojo的属性要和表单中的name一致。调用service插入内容信息。返回TaotaoResult。应该是一个json格式的数据。

@Controller
@RequestMapping("/content")
public class ContentController {

	@Autowired
	private ContentService contentService;
	
	@RequestMapping("/save")
	@ResponseBody
	public TaotaoResult insertContent(TbContent content) {
		TaotaoResult result = contentService.insertContent(content);
		return result;
	}
}

展示商城首页大广告位


优点:有利于seo优化。可以在taotao-portal中对数据进行加工。
缺点:系统直接需要调用服务查询内容信息。多了一次http请求。

系统直接服务的调用,需要使用httpclient来实现。taotao-protal和taotao-rest是在同一个局域网内。速度非常的快,调用时间可以忽略不计。

内容服务发布

需求分析

根据内容的分类id查询内容列表,从tb_content表中查询。服务是一个restFul形式的服务。使用http协议传递json格式的数据。

Dao层

从tb_content表中查询,根据内容分类id查询。是单表查询。可以使用逆向工程生成的代码。

Service层

接收内容分类id,根据分类id查询分类列表。返回一个内容pojo列表。

参数:分类id

返回值:pojo列表

@Service
public class ContentServiceImpl implements ContentService{

	@Autowired
	private TbContentMapper contentMapper;
	@Override
	public List<TbContent> getContentList(long contentCid) {
		TbContentExample example = new TbContentExample();
		Criteria criteria = example.createCriteria();
		criteria.andCategoryIdEqualTo(contentCid);
		List<TbContent> list = contentMapper.selectByExample(example);
		
		return list;
	}

}

Controller层

发布服务。接收查询参数。Restful风格内容分类id应该从url中取。

/rest/content/list/{contentCategoryId}

从url中取内容分类id,调用Service查询内容列表。返回内容列表。返回一个json格式的数据。可以使用TaotaoResult包装此列表。

@Controller
@RequestMapping("/content")
public class ContentController {

	@Autowired
	private ContentService contentService;
	@RequestMapping("/list/{contentCategoryId}")
	@ResponseBody
	public TaotaoResult getContentList(@PathVariable Long contentCategoryId){
		try {
			List<TbContent> list = contentService.getContentList(contentCategoryId);
			return TaotaoResult.ok(list);
			
		} catch (Exception e) {

			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		
	}

什么是httpclient:

HttpClient是Apache Jakarta Common下的子项目,用来提供高效的,最新的,功能丰富的支持HTTP协议的客户端编程工具包,并且它支持http协议最新的版本和建议。

添加依赖:

添加httpclient的jar包,

执行get的方法:

@Test
	public void doGet() throws Exception{
		//创建一个httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//创建一个GET对象
		HttpGet get = new HttpGet("http://www.sogou.com");
		//执行请求
		CloseableHttpResponse response = httpClient.execute(get);
		//取响应的结果
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println(statusCode);
		HttpEntity entity = response.getEntity();
		String string = EntityUtils.toString(entity);
		System.out.println(string);
		
		//关闭httpclient
		response.close();
		httpClient.close();
	}

使用带参数的GET请求:

@Test
	public void doGetParam() throws Exception{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		URIBuilder uriBuild = new URIBuilder("https://www.sogou.com/web");
		uriBuild.addParameter("query", "花千骨");
		HttpGet get = new HttpGet(uriBuild.build());
		//执行请求
		CloseableHttpResponse response = httpClient.execute(get);
		//取响应的结果
		int statusCode = response.getStatusLine().getStatusCode();
		System.out.println(statusCode);
		HttpEntity entity = response.getEntity();
		String string = EntityUtils.toString(entity);
		System.out.println(string);
		
		//关闭httpclient
		response.close();
		httpClient.close();
使用post方法:

@Test
	public void doPost() throws Exception{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = new HttpPost("http://127.0.0.1:8082/httpclient/post.html");
		//执行post请求
		CloseableHttpResponse response = httpClient.execute(post);
		String string = EntityUtils.toString(response.getEntity());
		System.out.println(string);
		response.close();
		httpClient.close();
		
	}
使用post方法带参数:
@Test
	public void doPostWithParam() throws Exception{
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = new HttpPost("http://127.0.0.1:8082/httpclient/post.html");
		//设置请求的内容
		List<NameValuePair> kvList = new ArrayList<>();
		kvList.add(new BasicNameValuePair("username", "张三"));
		kvList.add(new BasicNameValuePair("password","223"));
		StringEntity entity = new UrlEncodedFormEntity(kvList);
		post.setEntity(entity);
		//执行post请求
		CloseableHttpResponse response = httpClient.execute(post);
		String string = EntityUtils.toString(response.getEntity());
		System.out.println(string);
		response.close();
		httpClient.close();
	}

大广告位展示

Json字符串如何传递给jsp:使用modelAndView对象把json字符串传递给jsp。

如何获得json字符串:获得一个广告位对应的内容列表,需要调用taotao-rest的服务。把列表转换成json数据格式要求的pojo对象列表。

需要使用httpclient调用taotao-rest的服务。

 Dao层:

没有

Service层:

根据内容分类id查询分类的内容列表,需要使用httpclient调用taotao-rest的服务。得到一个json字符串。需要把字符串转换成对象taotaoResult对象。从taotaoResult对象中取data属性,得到内容列表。把内容列表转换成jsp页面要求的json格式。返回一个json字符串。

参数:没有

返回值:json字符串

@Service
public class ContentServiceImpl implements ContentService {

	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	@Value("${REST_INDEX_AD_URL}")
	private String REST_INDEX_AD_URL;
	
	@Override
	public String getContentList() {
		String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL);
		try {
			TaotaoResult taotaoResult = TaotaoResult.formatToList(result,TbContent.class);
			List<TbContent> list = (List<TbContent>) taotaoResult.getData();
			List<Map> resultList = new ArrayList<>();
			for (TbContent tbContent : list) {
				Map map = new HashMap<>();
				map.put("src", tbContent.getPic());
				map.put("height", 240);
				map.put("width", 670);
				map.put("srcB", tbContent.getPic2());
				map.put("widthB", 550);
				map.put("heightB", 240);
				map.put("href", tbContent.getUrl());
				map.put("alt", tbContent.getSubTitle());
				resultList.add(map);

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

Controller

@Controller
public class IndexController {

	@Autowired
	private ContentService contentService;
	@RequestMapping("/index")
	public String showIndex(Model model){
		String list = contentService.getContentList();
		model.addAttribute("ad1", list);
		return "index";
	}
	
	@RequestMapping(value="/httpclient/post",method=RequestMethod.POST)
	@ResponseBody
	public String testPost(String username,String password){
		
		return "username:"+username + "\tpassword:" + password;
	}
	
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值