第十七篇:商品详情工程

前言:
我们前面实现了利用ActiveMQ来发送消息和接收消息。现在我们看看如何实现商品详情的查看

1.商品详情分析

我们可以看到在京东搜索手机的时候,点击商品的链接进入到商品详情页面,访问的地址是变了。因此我们推出这是专门用来展示商品详情的工程。


image.png
image.png

2.工程搭建

从上面的分析可以看出我们需要搭建一个详情工程,我们可以参考taotao-seacrch-web的工程进行配置。具体步骤不再赘述;这里贴出一些配置文件内容。
pom.xml


    4.0.0
    
        com.lan
        common-parent
        1.0-SNAPSHOT
        ../common-parent/pom.xml
    
    com.lan
    taotao-item-web
    0.0.1-SNAPSHOT
    war

    
        
        
            com.lan
            common-utils
            1.0-SNAPSHOT
        
        
            com.lan
            taotao-manager-interface
            1.0-SNAPSHOT
        
        
        
            org.springframework
            spring-context
        
        
            org.springframework
            spring-beans
        
        
            org.springframework
            spring-webmvc
        
        
            org.springframework
            spring-jdbc
        
        
            org.springframework
            spring-aspects
        
        
            org.springframework
            spring-jms
        
        
            org.springframework
            spring-context-support
        
        
            junit
            junit
        
        
        
            jstl
            jstl
        
        
            javax.servlet
            servlet-api
            provided
        
        
            javax.servlet
            jsp-api
            provided
        
        
        
            com.alibaba
            dubbo
            
                
                    spring
                    org.springframework
                
                
                    netty
                    org.jboss.netty
                
            
        
        
            org.apache.zookeeper
            zookeeper
        
        
            com.github.sgroschupf
            zkclient
        
        
            commons-fileupload
            commons-fileupload
        
    
    
        
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                
                    /
                    8086
                
            
        
    

springmvc.xml


image.png



    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
    
      

web.xml



  taotao-item-web
  
    index.html
  
  
  
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
  
  
    CharacterEncodingFilter
    /*
  
  
  
    taotao-item-web
    org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      classpath:spring/springmvc.xml
    
    1
  
  
    taotao-item-web
    
    *.html
  


然后把静态化资源加进去


image.png

接下来修改taotao-search-web工程中的search.jsp文件


image.png

这样我们的工程就搭建好了

3.商品详情展示代码实现

我们可以观察到在item.jsp中,要展示的数据都是可以从tbitem中获取的。但是图片列表是没有的,我们可以特殊处理一下。,因为tbItem实体类中图片的字段存储的是以","分隔的图片地址的字符串,因此要将字符串转换成数组才行,而tbItem实体类没有images属性,这就需要我们再新建一个pojo类,该类要继承自tbItem,只是处理一下图片即可。商品描述是在实体类tbItemDesc当中。

image.png

image.png

功能分析:
请求的url:/item/{itemId}
参数:商品id
返回值:String 逻辑视图
业务逻辑:
1、从url中取参数,商品id
2、根据商品id查询商品信息(tb_item)得到一个TbItem对象,缺少images属性,可以创建一个pojo继承TbItem,添加一个getImages方法。在taotao-item-web工程中。

public class Item extends TbItem {

    public String[] getImages() {
        String image2 = this.getImage();
        if (image2 != null && !"".equals(image2)) {
            String[] strings = image2.split(",");
            return strings;
        }
        return null;
    }
    
    public Item() {
        
    }
    
    public Item(TbItem tbItem) {
        this.setBarcode(tbItem.getBarcode());
        this.setCid(tbItem.getCid());
        this.setCreated(tbItem.getCreated());
        this.setId(tbItem.getId());
        this.setImage(tbItem.getImage());
        this.setNum(tbItem.getNum());
        this.setPrice(tbItem.getPrice());
        this.setSellPoint(tbItem.getSellPoint());
        this.setStatus(tbItem.getStatus());
        this.setTitle(tbItem.getTitle());
        this.setUpdated(tbItem.getUpdated());
    }
    
}

Dao层
查询tb_item, tb_item_desc两个表,都是单表查询。可以使用逆向工程。
Service层
在taotao-manager-service中添加
1、根据商品id查询商品信息
参数:商品id
返回值:TbItem
2、根据商品id查询商品描述
参数:商品id
返回值:TbItemDesc

image.png

由于这是在之前的服务增加的代码,因此不用再发布服务了
表现层
1.接收服务:
image.png

Controller:
请求的url:/item/{itemId}
参数:商品id
返回值:String 逻辑视图

@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;
    @RequestMapping("/item/{itemId}")
    public String showItem(@PathVariable Long itemId, Model model) {
        //获得商品的基本信息
        TbItem tbItem = itemService.getItemById(itemId);
        //获得商品的基本描述
        TbItemDesc tbItemDesc = itemService.getDescById(itemId);
        Item item = new Item(tbItem);
        model.addAttribute("item",item);
        model.addAttribute("itemDesc",tbItemDesc);
        //返回逻辑视图
        return "item";
    }
}

启动tomcat。
测试结果:

没加缓存前.png

你可能感兴趣的:(第十七篇:商品详情工程)