1 Cms系统
内容管理系统
右键可以管理分类,添加删除等
先实现内容的分类管理再实现内容管理。
初始化树形视图的url: ‘/content/category/list’,
(function(){(“#contentCategory”).tree({
url : ‘/content/category/list’,
分析参数
参数是id,当前节点id属性,应该根据此id查询子节点列表。
返回值:包含id、text、state三个属性的json数据列表
service
入参是页面传递的parentId
出参:每个节点包含三个属性id、text、state三个属性。可以使用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> resultList = new ArrayList<>();
for (TbContentCategory tbContentCategory : list) {
//创建一个节点
EUTreeNode node = new EUTreeNode();
node.setId(tbContentCategory.getId());
node.setText(tbContentCategory.getName());
node.setState(tbContentCategory.getIsParent()?"closed":"open");
// 根据是不是父节点 设置closed 或 open
resultList.add(node);
}
return resultList;
}
}
controller
@RequestMapping("/list")
@ResponseBody
public List<EUTreeNode> getContentCatList(@RequestParam(value="id", defaultValue="0")Long parentId) {
List<EUTreeNode> list = contentCategoryService.getCategoryList(parentId);
return list;
}
3.1.2 内容分类添加
请求的url:/content/category/create
参数:
1、parentId父节点id
2、name:当前节点的名称
返回值:TaotaoResult。其中包含节点pojo对象。
service
功能:接收两个参数parentId父节点id、name:当前节点的名称。
返回TaoTaoResult包含记录的pojo对象。
页面的信息里有id,所以需要返回主键信息:
public TaotaoResult insertContentCategory(long parentId, String name) {
//创建一个pojo
TbContentCategory contentCategory = new TbContentCategory();
contentCategory.setName(name);
contentCategory.setIsParent(false);
// 新增的内容是叶子
//'状态。可选值:1(正常),2(删除)',
contentCategory.setStatus(1);
contentCategory.setParentId(parentId);
contentCategory.setSortOrder(1);
contentCategory.setCreated(new Date());
contentCategory.setUpdated(new Date());
//添加记录
contentCategoryMapper.insert(contentCategory);
//查看父节点的isParent列是否为true,如果不是true改成true
TbContentCategory parentCat = contentCategoryMapper.selectByPrimaryKey(parentId);
//判断是否为true
if(!parentCat.getIsParent()) {
parentCat.setIsParent(true);
//更新父节点
contentCategoryMapper.updateByPrimaryKey(parentCat);
}
//返回结果
return TaotaoResult.ok(contentCategory);
}
controller
@RequestMapping("/create")
@ResponseBody
public TaotaoResult createContentCategory(Long parentId, String name) {
TaotaoResult result = contentCategoryService.insertContentCategory(parentId, name);
return result;
}
删除分类
$.messager.confirm('确认','确定删除名为 '+node.text +' 的分类吗?',function(r){
if(r){
$.post("/content/category/delete",{parentId:node.parentId,id:node.id},function(){
tree.tree("remove",node.target);
});
}
});
执行报错:
Optional long parameter ‘parentId’ is present but cannot be translated into a null value due to
是因为我们在方法里接口参数parentId,但是实际上页面并没有传入parentId,默认是空,但是又不允许为空,所以把报错
@RequestMapping("/delete")
@ResponseBody
public TaotaoResult deleteContentCategory(long parentId, long id) {
System.out.println("parentId : " + parentId + " [id]" + id);
return categoryService.deleteContentCategory(parentId, id);
}
检查方法
想到一个方法就是parentI的可以通过id查询出来,页面不传递也可以
@Override
public TaotaoResult deleteContentCategory(long id) {
TbContentCategory category = contentcategoryMapper.selectByPrimaryKey(id);
// 查询父节点内容
TbContentCategory parentCategory = contentcategoryMapper.selectByPrimaryKey(category.getParentId());
// 删除本节点
contentcategoryMapper.deleteByPrimaryKey(id);
TbContentCategoryExample example = new TbContentCategoryExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentCategory.getId());
List<TbContentCategory> list = contentcategoryMapper.selectByExample(example);
if(list == null){
//判断父节点是否为parnet 不是设为false更新父节点
parentCategory.setIsParent(false);
contentcategoryMapper.updateByPrimaryKeySelective(parentCategory);
}
// 返回结果
return TaotaoResult.ok();
}
3.2 内容管理
1. 列表
url:’/content/query/list’,queryParams:{categoryId:0}”
业务逻辑:
根据内容分类id查询内容列表。需要实现分页。返回EUDataGridResult
if(tree.tree("isLeaf",node.target)){
datagrid.datagrid('reload', {
categoryId :node.id
});
}
传递 categoryId
service
@Override
public EUDataGridResult getContentList(long categoryId, int page, int rows) {
TbContentExample example = new TbContentExample();
Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(categoryId);
// 分页处理
PageHelper.startPage(page, rows);
List<TbContent> list = contentMapper.selectByExample(example);
PageInfo<TbContent> info = new PageInfo<>(list);
long total = info.getTotal();
EUDataGridResult data = new EUDataGridResult();
data.setRows(list);
data.setTotal(total);
return data;
}
controller 直接调用servic
@RequestMapping("/query/list")
@ResponseBody
public EUDataGridResult queryContentList
(Integer page, Integer rows,long categoryId) {
return contentService.getContentList(categoryId, page, rows);
}
3.2.2 内容添加
@Override
public TaotaoResult addContent(TbContent content) {
content.setCreated(new Date());
content.setUpdated(new Date());
contentMapper.insert(content);
return TaotaoResult.ok();
}
4.1 首页大广告方案
流程
根据内容的分类id查询内容列表,从tb_content表中查询。服务是一个restFul形式的服务。使用http协议传递json格式的数据。
@Service
public class ContentServiceImpl implements ContentService {
@Autowired
private TbContentMapper contentMapper;
@Override
public List<TbContent> getContentList(long contentCid) {
//根据内容分类id查询内容列表
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,
@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));
}
}
上面的地址 没有aciton 前面是rest
http://localhost:8081/rest/content/list/89
4.4 Httpclient的使用
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, "utf-8");
System.out.println(string);
//关闭httpclient
response.close();
httpClient.close();
}
2 、带参数
//创建一个uri对象
URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
uriBuilder.addParameter("query", "花千骨");
HttpGet get = new HttpGet(uriBuilder.build());
注意:对于URL必须使用 http://开始,否则会有如下报错信息:
HttpGet get = new HttpGet(“http://www.sogou.com“);
Caused by: org.apache.http.ProtocolException: Target host is not specified
异常:
4.4.3.3 使用httpclient执行post请求
post方法
@RequestMapping(value="/httpclient/post", method=RequestMethod.POST)
@ResponseBody
public String doPost(String name){
return "name = " + name;
}
1、不带参数
public void doPost() throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个post对象
HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");
//执行post请求
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
response.close();
httpClient.close();
}
注意:taotao-portalweb.xml中配置了
<servlet-mapping>
<servlet-name>taotao-portal</servlet-name>
<!-- 伪静态化 -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
只会拦截。html 所以访问需要加.html
2、 带参数
//创建一个Entity。模拟一个表单
List<NameValuePair> kvList = new ArrayList<>();
kvList.add(new BasicNameValuePair("username", "zhangsan"));
kvList.add(new BasicNameValuePair("password", "123"));
//包装成一个Entity对象
StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
//设置请求的内容
post.setEntity(entity);