1.用户模块实现
1.1 用户信息回显
1.1.1 页面URL分析
1.1.2 检查页面JS
1.1.3 编辑JT-SSO的Controller
@RequestMapping("/query/{ticket}")
public JSONPObject
findUserByTicket(@PathVariable String ticket
,
HttpServletResponse response
,
String callback
){
String userJSON
= jedisCluster
.get(ticket
);
if(StringUtils
.isEmpty(userJSON
)){
Cookie cookie
= new Cookie("JT_TICKET", "");
cookie
.setMaxAge(0);
cookie
.setDomain("jt.com");
cookie
.setPath("/");
response
.addCookie(cookie
);
return new JSONPObject(callback
,SysResult
.fail());
}
return new JSONPObject(callback
,SysResult
.success(userJSON
));
}
1.2 编辑Cookie工具API
package com
.jt
.util
;
import javax
.servlet
.http
.Cookie
;
import javax
.servlet
.http
.HttpServletRequest
;
import javax
.servlet
.http
.HttpServletResponse
;
public class CookieUtil {
public static void addCookie(HttpServletResponse response
,String cookieName
, String cookieValue
, int seconds
, String domain
){
Cookie cookie
= new Cookie(cookieName
,cookieValue
);
cookie
.setMaxAge(seconds
);
cookie
.setDomain(domain
);
cookie
.setPath("/");
response
.addCookie(cookie
);
}
public static String
getCookieValue(HttpServletRequest request
,String cookieName
){
Cookie
[] cookies
= request
.getCookies();
if(cookies
!=null
&& cookies
.length
>0){
for (Cookie cookie
: cookies
){
if(cookieName
.equals(cookie
.getName())){
return cookie
.getValue();
}
}
}
return null
;
}
public static void deleteCookie(HttpServletResponse response
,String cookieName
,String domain
){
addCookie(response
,cookieName
,"",0, domain
);
}
}
1.3 用户退出操作
1.3.1 业务说明
如果用户点击退出操作, 首先应该删除Redis中的数据 其次删除Cookie中的数据 之后重定向到系统首页.
1.3.2 URL分析
1.3.3 编辑UserController
@RequestMapping("logout")
public String
logout(HttpServletRequest request
,HttpServletResponse response
){
String ticket
= CookieUtil
.getCookieValue(request
,"JT_TICKET");
if(!StringUtils
.isEmpty(ticket
)){
jedisCluster
.del(ticket
);
CookieUtil
.deleteCookie(response
,"JT_TICKET","jt.com");
}
return "redirect:/";
}
2 实现商品详情展现
2.1 业务说明
说明: 当用户点击商品时,需要跳转到商品的展现页面中 页面名称item.jsp
2.2 重构JT-MANAGE
2.2.1创建接口
说明:在jt-common中创建接口
2.2.2 编辑Dubbo实现类
package com
.jt
.service
;
import com
.alibaba
.dubbo
.config
.annotation
.Service
;
import com
.jt
.mapper
.ItemDescMapper
;
import com
.jt
.mapper
.ItemMapper
;
import com
.jt
.pojo
.Item
;
import com
.jt
.pojo
.ItemDesc
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
@Service(timeout
= 3000)
public class DubboItemServiceImpl implements DubboItemService{
@Autowired
private ItemMapper itemMapper
;
@Autowired
private ItemDescMapper itemDescMapper
;
@Override
public Item
findItemById(Long itemId
) {
return itemMapper
.selectById(itemId
);
}
@Override
public ItemDesc
findItemDescById(Long itemId
) {
return itemDescMapper
.selectById(itemId
);
}
}
2.2.3 编辑YML配置文件
server
:
port
: 8091
servlet
:
context
-path
: /
spring
:
datasource
:
#引入druid数据源
#type
: com
.alibaba
.druid
.pool
.DruidDataSource
driver
-class-name
: com
.mysql
.cj
.jdbc
.Driver
url
: jdbc
:mysql
://127.0.0.1:3306/jtdb
?serverTimezone
=GMT
%2B8
&useUnicode
=true&characterEncoding
=utf8
&autoReconnect
=true&allowMultiQueries
=true
username
: root
password
: root
mvc
:
view
:
prefix
: /WEB
-INF
/views
/
suffix
: .jsp
#mybatis
-plush配置
mybatis
-plus
:
type
-aliases
-package: com
.jt
.pojo
mapper
-locations
: classpath
:/mybatis
/mappers
2.2.4 编辑ItemController
package com
.jt
.controller
;
import com
.alibaba
.dubbo
.config
.annotation
.Reference
;
import com
.jt
.pojo
.Item
;
import com
.jt
.pojo
.ItemDesc
;
import com
.jt
.service
.DubboItemService
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.ui
.Model
;
import org
.springframework
.web
.bind
.annotation
.PathVariable
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
@Controller
@RequestMapping("/items")
public class ItemController {
@Reference(timeout
= 3000)
private DubboItemService itemService
;
@RequestMapping("/{itemId}")
public String
findItemById(@PathVariable Long itemId
, Model model
){
Item item
= itemService
.findItemById(itemId
);
ItemDesc itemDesc
= itemService
.findItemDescById(itemId
);
model
.addAttribute("item",item
);
model
.addAttribute("itemDesc",itemDesc
);
return "item";
}
}
2.2.5 页面效果展现
3 购物车模块实现
3.1 创建服务提供者
3.1.1 创建项目JT-CART
3.1.2 添加继承/依赖/插件
<!--2.添加依赖信息
-->
<dependencies>
<!--依赖实质依赖的是jar包文件
-->
<dependency>
<groupId>com
.jt
</groupId
>
<artifactId>jt
-common
</artifactId
>
<version>1.0-SNAPSHOT
</version
>
</dependency
>
</dependencies
>
<!--3.添加插件
-->
<build>
<plugins>
<plugin>
<groupId>org
.springframework
.boot
</groupId
>
<artifactId>spring
-boot
-maven
-plugin
</artifactId
>
</plugin
>
</plugins
>
</build
>
3.1.3 编辑POJO对象
@TableName("tb_cart")
@Data
@Accessors(chain
= true)
public class Cart extends BasePojo{
@TableId(type
= IdType
.AUTO
)
private Long id
;
private Long uesrId
;
private Long itemId
;
private String itemTitle
;
private String itemImage
;
private Long itemPrice
;
private Integer num
;
}
3.1.4 编辑CartService接口
3.1.5 jt-cart代码结构
3.2 购物车列表页面展现
3.2.1 页面url分析
说明:当用户点击购物车按钮时,需要跳转到购物车展现页面. cart.jsp 页面取值: ${cartList} 要求: 只查询userId=7的购物车列表信息.之后进行页面展现
3.2.2 编辑CartController
package com
.jt
.controller
;
import com
.alibaba
.dubbo
.config
.annotation
.Reference
;
import com
.jt
.pojo
.Cart
;
import com
.jt
.service
.DubboCartService
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.ui
.Model
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import java
.util
.List
;
@Controller
@RequestMapping("/cart")
public class CartController {
@Reference(timeout
= 3000,check
= false)
private DubboCartService cartService
;
@RequestMapping("/show")
public String
findCartListByUserId(Model model
){
Long userId
= 7L
;
List
<Cart> cartList
= cartService
.findCartListByUserId(userId
);
model
.addAttribute("cartList",cartList
);
return "cart";
}
}
3.2.3 编辑CartService
package com
.jt
.service
;
import com
.alibaba
.dubbo
.config
.annotation
.Service
;
import com
.baomidou
.mybatisplus
.core
.conditions
.query
.QueryWrapper
;
import com
.jt
.mapper
.CartMapper
;
import com
.jt
.pojo
.Cart
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import java
.util
.List
;
@Service
public class DubboCartServiceImpl implements DubboCartService{
@Autowired
private CartMapper cartMapper
;
@Override
public List
<Cart> findCartListByUserId(Long userId
) {
QueryWrapper
<Cart> queryWrapper
= new QueryWrapper<>();
queryWrapper
.eq("user_id", userId
);
return cartMapper
.selectList(queryWrapper
);
}
}
3.2.4 页面效果展现
3.3 购物车数量的修改
3.3.1 业务说明
3.3.2 页面JS分析
3.3.3 编辑CartController
@RequestMapping("/update/num/{itemId}/{num}")
@ResponseBody
public void updateCartNum(Cart cart
){
Long userId
= 7L
;
cart
.setUserId(userId
);
cartService
.updateCartNum(cart
);
}
3.3.4 编辑CartService
@Override
public void updateCartNum(Cart cart
) {
Cart cartTemp
= new Cart();
cartTemp
.setNum(cart
.getNum());
UpdateWrapper
<Cart> updateWrapper
=
new UpdateWrapper<>(cart
.setNum(null
));
cartMapper
.update(cartTemp
,updateWrapper
);
}
3.3.5 编辑CartMapper
public interface CartMapper extends BaseMapper<Cart> {
@Update("update tb_cart set num = #{num},updated=now() where user_id=#{userId} and item_id=#{itemId}")
void updateCartNum(Cart cart
);
}
3.4 购物车新增
3.4.1 购物车新增业务
当用户点击购物车按钮时实现购物车入库操作. 如果用户重复加购则应该修改购物车商品的数量. 加购成功之后,应该重定向到购物车列表页面.
3.4.2 页面分析
3.4.3 页面表单提交
<form id
="cartForm" method
="post">
<input
class="text" id
="buy-num" name
="num" value
="1" onkeyup
="setAmount.modify('#buy-num');"/>
<input type
="hidden" class="text" name
="itemTitle" value
="${item.title }"/>
<input type
="hidden" class="text" name
="itemImage" value
="${item.images[0]}"/>
<input type
="hidden" class="text" name
="itemPrice" value
="${item.price}"/>
</form
>
3.4.4 页面JS解析
<a
class="btn-append " id
="InitCartUrl" onclick
="addCart();" clstag
="shangpin|keycount|product|initcarturl">加入购物车
<b></b
></a
>
function
addCart(){
var url
= "http://www.jt.com/cart/add/${item.id}.html";
document
.forms
[0].action
= url
;
document
.forms
[0].submit();
}
3.4.5 编辑CartController
@RequestMapping("/add/{itemId}")
public String
saveCart(Cart cart
){
Long userId
= 7L
;
cart
.setUserId(userId
);
cartService
.saveCart(cart
);
return "redirect:/cart/show.html";
}
3.4.6 编辑CartService
@Override
public void saveCart(Cart cart
) {
QueryWrapper
<Cart> queryWrapper
= new QueryWrapper<>();
queryWrapper
.eq("user_id", cart
.getUserId())
.eq("item_id", cart
.getItemId());
Cart cartDB
= cartMapper
.selectOne(queryWrapper
);
if(cartDB
== null
){
cartMapper
.insert(cart
);
}else {
int num
= cart
.getNum() + cartDB
.getNum();
cart
.setNum(num
);
cartMapper
.updateCartNum(cart
);
}
}
3.4.6 页面效果