e3mall项目:前台商品详情展示
准备工作:新建子工程 e3-item-web,并将静态资源导入,包结构如下:
一、,相关配置文件以及代码(e3-item-web)
(1)pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>e3-parent</artifactId>
<groupId>cn.e3mall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>e3-item-web</artifactId>
<packaging>war</packaging>
<name>e3-item-web</name>
<dependencies>
<dependency>
<groupId>cn.e3mall</groupId>
<artifactId>e3-manager-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.e3mall</groupId>
<artifactId>e3-manager-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- JSP相关 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8086</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
(2)springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--读取resource配置文件-->
<context:property-placeholder location="classpath:other/*.properties"/>
<!--开启注解支持-->
<context:component-scan base-package="cn.e3mall.item.controller" />
<!--配置SpringMVC注解支持-->
<mvc:annotation-driven />
<!--配置SpringMVC视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 引用dubbo服务 -->
<dubbo:application name="e3-search-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.25.130:2181"/>
<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />
</beans>
(3)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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>e3-item-web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!--配置SpringMVC前段控制器-->
<servlet>
<servlet-name>e3-item-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>e3-item-web</servlet-name>
<!--伪静态化-->
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<!--POST提交乱码解决-->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(4)ItemInfoController
package cn.e3mall.item.controller;
import cn.e3mall.common.entity.E3Result;
import cn.e3mall.entity.TbItem;
import cn.e3mall.entity.TbItemDesc;
import cn.e3mall.item.entity.Item;
import cn.e3mall.service.ItemService;
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;
/**
* 商品详情展示 web层
* Author: xushuai
* Date: 2018/5/27
* Time: 13:16
* Description:
*/
@Controller
public class ItemInfoController {
@Autowired
private ItemService itemService;
@RequestMapping("/item/{itemId}")
public String loadItemInfo(@PathVariable Long itemId, Model model){
//调用服务查询商品信息
E3Result e3Result1 = itemService.load(itemId);
TbItem item = (TbItem) e3Result1.getData();
//查询商品描述
E3Result e3Result2 = itemService.findDescByItemid(itemId);
TbItemDesc desc = (TbItemDesc) e3Result2.getData();
//保存数据到页面
model.addAttribute("item",new Item(item));
model.addAttribute("itemDesc",desc);
return "item";
}
}
(5)还需要封装一个 实体类,继承自TbItem(页面需要)
package cn.e3mall.item.entity;
import cn.e3mall.entity.TbItem;
public class Item extends TbItem {
public Item(TbItem tbItem) {
this.setId(tbItem.getId());
this.setTitle(tbItem.getTitle());
this.setSellPoint(tbItem.getSellPoint());
this.setPrice(tbItem.getPrice());
this.setNum(tbItem.getNum());
this.setBarcode(tbItem.getBarcode());
this.setImage(tbItem.getImage());
this.setCid(tbItem.getCid());
this.setStatus(tbItem.getStatus());
this.setCreated(tbItem.getCreated());
this.setUpdated(tbItem.getUpdated());
}
public String[] getImages() {
String image2 = this.getImage();
if (image2 != null && !"".equals(image2)) {
String[] strings = image2.split(",");
return strings;
}
return null;
}
}
二、在服务提供端,应用缓存(e3-manager-service工程的ItemServiceImpl)
(1)添加redis依赖(e3-manager-service的pom文件中新增)
<!--redis客户端-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
(2)相关代码。(其中包括,查询缓存、添加缓存以及同步缓存)
针对三个功能,我封装成了三个方法
/**
* 将数据添加到缓存中
* @auther: xushuai
* @date: 2018/5/27 14:28
*/
private void saveCache(String key,Object data) {
try {
//将查询到数据添加到缓存
jedisClient.set(key, JsonUtils.objectToJson(data));
//设置缓存过期时间
jedisClient.expire(key,timeout);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 查询缓存
* 如果查询到,返回查询到的结果
* @auther: xushuai
* @date: 2018/5/27 14:20
*/
private E3Result searchCache(String key, Class clazz){
try {
//根据key值查询
String s = jedisClient.get(key);
//校验查询结果是否不为空
if(StringUtils.isNotBlank(s)){
return E3Result.ok(JsonUtils.jsonToPojo(s,clazz));
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 同步缓存(删除对应key的缓存即可)
* @auther: xushuai
* @date: 2018/5/27 14:54
*/
private void syncCache(Long itemId) {
try {
//生成对应的key
String key = prefix + ":" + itemId + ":";
//BASE_KEY
String baseKey = key + "BASE";
//DESC_KEY
String descKey = key + "DESC";
//删除这两个key值对应的缓存,即设置过期时间为0
jedisClient.expire(baseKey,0);
jedisClient.expire(descKey,0);
}catch (Exception e){
e.printStackTrace();
}
}
在需要的地方记性调用:
注意:在项目中注入相关类和数据,如下:
(3)resource.properties新增配置
## redis单机版连接信息
redis.host=192.168.25.130
redis.port=6379
## redis中的key前缀
redis.prefix=ITEM_INFO
## redis 商品缓存过期时间 (单位:秒)
redis.time=3600
(4)新建配置文件:applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!--配置单机版redis-->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="${redis.host}"/>
<constructor-arg name="port" value="${redis.port}"/>
</bean>
<bean id="jedisClientPool" class="cn.e3mall.common.redis.JedisClientPool">
<property name="jedisPool" ref="jedisPool" />
</bean>
<!--配置集群版redis-->
<!--
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host1}"/>
<constructor-arg name="port" value="${redis.port1}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host2}"/>
<constructor-arg name="port" value="${redis.port2}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.host3}"/>
<constructor-arg name="port" value="${redis.port3}"/>
</bean>
</set>
</constructor-arg>
</bean>
<bean id="jedisClientCluster" class="cn.e3mall.common.redis.JedisClientCluster">
<property name="jedisCluster" ref="jedisCluster"/>
</bean>
-->
</beans>