https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】

开发环境:

Eclipse IDE for Enterprise Java Developers
OS: Windows 10, v.10.0, x86_64 / win32
Java version: 1.8.0_221

05.第五天(前台工程搭建、首页商品类目显示)


taotao-rest项目

web.xml

    <!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>taotao-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>taotao-rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

因为springMVC在web.xml里配置的前端控制器的url-pattern拦截的不是:/

所以在开发时resources目录下的springmvc.xml里就不需要配置静态资源的映射,因为这些静态资源在浏览器发送请求时没有被拦截。


03.服务层工程搭建

一、跳过测试的Maven命令:install -DskipTests

install -DskipTests

二、 放置静态资源的目录是 webapp目录

taotao-portal项目模块

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>taotao-portal-web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 加载spring容器,注意修改通配符 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 解决post乱码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<!-- <init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>taotao-portal</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>taotao-portal</servlet-name>
<!-- 伪静态化给搜索引擎看的 -->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

5  商品分类展示

首页左侧有一个商品分类。当鼠标分类上,需要展示出此分类下的子分类。

当鼠标滑动到连接上触发mousemove事件。页面做一个ajax请求,请求json数据包含分类信息,得到json数据后初始化分类菜单,展示。

Eclipse快捷键:Ctrl+L,这样直接弹出Go to line对话框,在里面输入要跳转到的行号,按回车或者单击‘确定’。

或者找到Eclipse界面下的状态栏,找到行号列,双击它,也会弹出Go to line对话框,也是输入行号,然后按回车键。

然后在webapp目录下copy一个category.json

使用JSON Viewer可以Format这个json然后方便查看分析其结构组成。

第一层:u、n(包含a标签)、i

第二层:u、n、i

第三层:字符串

使用ajax访问本工程的json数据(jQuery的方式)

本项目调试前端页面用FireFox火狐浏览器正常显示,但使用搜狗浏览器访问不显示左侧的商品分类。


数据需要从taotao-rest中调用服务获得。

将category.json移动到taotao-rest模块下的webapp目录下,浏览器访问可以请求到该json文件。

http://localhost:8081/category.json

5.3   Ajax跨域请求

ajax是不能跨域请求。出于安全考虑,js设计时不可以跨域。

什么是跨域:

1、域名不同时。

2、域名相同,端口不同。

只有域名相同、端口相同时,才可以访问。

可以使用jsonp解决跨域问题。

Jsonp其实就是一个跨域解决方案。js跨域请求数据是不可以的,但是js跨域请求js脚本是可以的。可以把数据封装成一个js语句,做一个方法的调用。跨域请求js脚本可以得到此脚本。

得到js脚本之后会立即执行。

可以把数据做为参数传递到方法中。就可以获得数据。从而解决跨域问题。


5.4   从数据库中取商品分类列表

5.4.1   Dao层

可以使用逆向工程生成的代码。

5.4.2   Service层

查询所有商品分类生成前台页面要求的json数据格式。返回一个pojo。

需要创建两个pojo

1、分类列表的节点。包含u、n、i属性。

package com.taotao.rest.pojo;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

public class CatNode {

    // @JsonProperty此注解用于pojo成员属性上,作用是把该属性的名称序列化为另外一个名称
@JsonProperty("n")
private String name;
@JsonProperty("u")
private String url;
@JsonProperty("i")
private List<?> item; //Shift+Alt+S 快捷键
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<?> getItem() {
return item;
}
public void setItem(List<?> item) {
this.item = item;
} }

//@JsonProperty此注解用于pojo成员属性上

2、返回值pojo。包含data属性是一个list类型。

放到taotao-rest工程中。其他的项目不用到。

package com.taotao.rest.pojo;

import java.util.List;

public class CatResult {
private List<?> data; public List<?> getData() {
return data;
} public void setData(List<?> data) {
this.data = data;
} }

Service层

//门户左侧商品分类服务
@Service
public class ItemCatServiceImpl implements ItemCatService { @Autowired
private TbItemCatMapper ibItemCatMapper; @Override
public CatResult getItemCatList() {
CatResult catResult = new CatResult();
// 查询商品分类列表
// 调用类中私有方法
catResult.setData(getCatList(0));
return catResult;
} // 查询商品分类列表的私有方法
private List<?> getCatList(long parentId) {
// 创建查询条件
TbItemCatExample example = new TbItemCatExample();
Criteria criteria = example.createCriteria();
criteria.andParentIdEqualTo(parentId); // 执行Example查询
List<TbItemCat> list = ibItemCatMapper.selectByExample(example);
// 返回值list
List resultList = new ArrayList<>(); // 向list中添加节点
// 递归
for (TbItemCat tbItemCat : list) { // 判断是否为父节点
if (tbItemCat.getIsParent()) { CatNode catNode = new CatNode();
if (parentId == 0) {
catNode.setName(
"<a href='/products/" + tbItemCat.getId() + ".html'>" + tbItemCat.getName() + "</a>");
} else {
catNode.setName(tbItemCat.getName());
}
catNode.setUrl("/products/" + tbItemCat.getId() + ".html");
catNode.setItem(getCatList(tbItemCat.getId())); resultList.add(catNode);
// 如果是叶子节点
} else {
resultList.add("/products/" + tbItemCat.getId() + ".html|" + tbItemCat.getName());
} }
return resultList;
}
}

Controller层

接收页面传递过来的参数。参数就是方法的名称。返回一个json数据,需要把json数据包装成一句js代码。返回一个字符串。

参数:回调方法名称

返回值:字符串

为了防止页面请求到的JSON中文乱码,需要添加,produces="MediaType.APPLICATION_JSON_VALUE"+";charset=utf-8"

//商品分类列表
@Controller
public class ItemCatController { @Autowired
private ItemCatService itemCatService; @RequestMapping(value="/itemcat/list", produces="MediaType.APPLICATION_JSON_VALUE"+";charset=utf-8")
@ResponseBody
public String getItemCatList(String callback) {
CatResult catResult = itemCatService.getItemCatList();
//把pojo转换成字符串
String json = JsonUtils.objectToJson(catResult);
//拼装返回值
String result = callback + "(" + json + ");";
return result;
}
}

taotao-rest的查询商品分类列表Controller

//商品分类列表
@Controller
public class ItemCatController { @Autowired
private ItemCatService itemCatService; @RequestMapping("/itemcat/list")
@ResponseBody
public Object getItemCatList(String callback) {
CatResult catResult = itemCatService.getItemCatList(); MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(catResult); mappingJacksonValue.setJsonpFunction(callback);
return mappingJacksonValue;
} }

http://localhost:8081/rest/itemcat/list?callback=myfunction

修改lib-v1.js

============================================

参考资料:

Java:关于List类中的add、addAll和set方法

produces在@requestMapping中的使用方式

Java集合与泛型中的陷阱

end

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】的更多相关文章

  1. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  2. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第一天】

    本人做过一年的MATLAB编程和简单维护过VB和C++的项目.是跟着网上获得的黑马的Java双元视频课来自学入门Java知识和常用框架的使用. 淘淘商城(SpringMVC+Spring+Mybati ...

  3. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  4. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  5. 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  6. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十天】(单点登录系统实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  7. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  8. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  9. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第七天】(redis缓存)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

随机推荐

  1. 关于torch.norm函数的笔记

    先看一下它的参数: norm(p='fro', dim=None, keepdim=False, dtype=None) p: the order of norm. 一般来说指定 $p = 1, 2$ ...

  2. django-替代为自定义的User model

    https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model Subs ...

  3. Sequence Models Week 2 Emojify

    Emojify! Welcome to the second assignment of Week 2. You are going to use word vector representation ...

  4. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  5. 数组 bash shell

    http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html #!/bin/bash # 脚本检测到离线主机会发送邮件通知,可一直运行. ...

  6. UVM实战[二]

    本期将讲解UVM环境构成和启动方式.主要参考资料为 http://bbs.eetop.cn/thread-320165-1-1.html http://rockeric.com/ 环境构成 进行仿真验 ...

  7. VS2017中使用ReportViewer控件,vs2017找不到Microsoft Rdlc Report Designer for Visual Studio

    VS2017中没有ReportViewer控件,这个控件用来实现在项目中显示和打印关系数据库中的表比较容易,特别是想要打印的时候,这个比用DataGridView和PrintDocument要简单一些 ...

  8. input只允许输入数字,并且小数点后保留4位

    <input type="text" value="" name="should_send_num" id="should_ ...

  9. Java 接口理解

    学习Spring有一段时间了,对java也有了一点了解,最不能理解的就是接口, 即使是写了接口并实现了它,依然无法理解它到底有什么用?看了其他几篇博客,总结了一下自己的理解. 在JAVA编程语言中是一 ...

  10. delphi save .dfm to .txt

    procedure TForm2.saveDfm; var inStream,outStream:TMemoryStream; begin inStream:=TMemoryStream.Create ...