您的位置:首页 > 编程语言 > Java开发

【j2ee spring】44、巴巴运动网前台产品显示

2015-07-15 15:58 344 查看
【j2ee spring】44、巴巴运动网前台产品显示

项目结构









项目代码

界面显示

<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>${producttype.name} 巴巴运动网</title>    
    <link href="css/global/header01.css" rel="stylesheet" type="text/css" />
    <link href="css/product/list.css" rel="stylesheet" type="text/css" />   
    <link href="css/global/topsell.css" rel="stylesheet" type="text/css" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="Keywords" content="${producttype.name}" />
    <META name="description" content="${producttype.note}" />
<SCRIPT type="text/javascript" src="js/xmlhttp.js"></SCRIPT>
<SCRIPT type="text/javascript" >
    function getTopSell(typeid){
        var salespromotion = document.getElementById('salespromotion');     
        if(salespromotion && typeid!=""){
            salespromotion.innerHTML= "数据正在加载...";
            send_request(function(value){salespromotion.innerHTML=value},
                     "<html:rewrite action="/product/switch"/>?method=topsell&typeid="+ typeid, true);
        }
    }
    function getViewHistory(){
        var viewHistoryUI = document.getElementById('viewHistory');     
        if(viewHistoryUI){
            viewHistoryUI.innerHTML= "数据正在加载...";
            send_request(function(value){viewHistoryUI.innerHTML=value},
                     "<html:rewrite action="/product/switch"/>?method=getViewHistory", true);
        }
    }
    function pageInit(){
        getTopSell("${producttype.typeid}");
        getViewHistory();
    }
</SCRIPT>
</head>

<body class="ProducTypeHome2" onload="javascript:pageInit()">
    <jsp:include page="/page/share/Head.jsp"/>
    <s:set name="out" value='' />  <!-- 这个是定义了一个变量 -->
    <s:iterator value="#request.types" var="type"> 
        <s:set name="out" var="out" value="#out+'>>'+#type.name+'</a>'" />
    </s:iterator>
    <div id="position">您现在的位置: <a href="/" name="linkHome">巴巴运动网</a> 
        <s:property value="#out" escapeHtml="false" />(<s:property value="#request.pageView.totalrecords" />个)
    </div>

    <!--页面左侧分类浏览部分-->
    <div class="browse_left">
         <div class="browse">
            <div class="browse_t">${producttype.name}</div>
                <h2><span class="gray">浏览下级分类</span></h2>
                <ul>
                    <s:iterator value="#request.producttype.childtypes" var="childtype">
                        <li class='bj_blue'> 
                            <a href="<s:url action="frontProductlist"/>?typeid=<s:property value="#childtype.typeid" />"><s:property value="#childtype.name" /></a>
                        </li>
                    </s:iterator>
                </ul>
         </div>
<DIV id="sy_biankuang">
        <DIV class="lanmu_font">最畅销${producttype.name}</DIV>
        <DIV style="PADDING-LEFT: 10px; COLOR: #333333" id="salespromotion">

        </DIV>
</DIV>
         <br/>
         <div class="browse">
              <div class="browse_t">您浏览过的商品</div>
              <ul id="viewHistory"></ul>
         </div>
    </div>
    <!--页面右侧分类列表部分开始-->
    <div class="browse_right">
         <div class="select_reorder">
              <div class="reorder_l">请选择排序方式: 
              <s:if test="%{'selldesc' == pf.sort}"><strong><em>销量多到少</em></strong></s:if>
              <s:if test="%{'selldesc' != pf.sort}">
                <a title='按销量降序' href="<s:url action="frontProductlist" />?sort=selldesc&typeid=<s:property value="#parameters.typeid" />">销量多到少</a>
              </s:if>
              | 
              <s:if test="%{'sellpricedesc' == pf.sort}"><strong><em>价格高到低</em></strong></s:if>
              <s:if test="%{'sellpricedesc' != pf.sort}">
                <a title='按销量降序' href="<s:url action="frontProductlist" />?sort=sellpricedesc&typeid=<s:property value="#parameters.typeid" />">价格高到低</a>
              </s:if>
              | 
              <s:if test="%{'sellpriceasc' == pf.sort}"><strong><em>价格低到低</em></strong></s:if>
              <s:if test="%{'sellpriceasc' != pf.sort}">
                <a title='价格低到高' href="<s:url action="frontProductlist" />?sort=sellpriceasc&typeid=<s:property value="#parameters.typeid" />">价格低到高</a>
              </s:if>
              | 
              <s:if test="%{pf.sort == ''}"><strong><em>最近上架时间</em></strong></s:if>
              <s:if test="%{pf.sort != ''}">
                <a title='价格低到高' href="<s:url action="frontProductlist" />?sort=&typeid=<s:property value="#parameters.typeid" />">最近上架时间</a>
              </s:if> 
         </div>

              <div class="reorder_r">显示方式:
                  <s:if test="##">
                    <strong><em>图文版</em></strong>
                  </s:if>
                  <s:if test="##">
                    <a href="<html:rewrite action="/product/list/display"/>?sort=${param.sort}&typeid=${param.typeid}&sex=${param.sex }&brandid=${param.brandid}&style=imagetext">图文版</a>
                  </s:if> |
                  <s:if test="##">
                    <a href="<html:rewrite action="/product/list/display"/>?sort=${param.sort}&typeid=${param.typeid}&sex=${param.sex }&brandid=${param.brandid}&style=image">图片版</a>
                  </s:if>
                  <s:if test="##">
                    <strong><em>图片版</em></strong>
                  </s:if>
              </div>
            <div class="emptybox"></div>
             <div class="brand">
                <div class="FindByHint">按<strong>品牌</strong>选择:</div>
                <ul class="CategoryListTableLevel1">
                    <s:iterator value="#request.brands" var="brand"> 
                        <li>
                            <a href="<s:url action="frontProductlist" />?sort=<s:property value="#request.sort" />
                                                &brandid=<s:property value="#brand.code" />&typeid=<s:property value="#parameters.typeid" />">
                                                <s:property value="#brand.name" />
                            </a>
                        </li>
                    </s:iterator>
                </ul>
             </div>
             <div class="SubCategoryBox">
                <div class="FindByHint">按<strong>男女款</strong>选择:</div>
                <ul class="CategoryListTableLevel1">
                <li>
                    <a  href="<s:url action="frontProductlist"/>?sort=sellpriceasc&
                                typeid=<s:property value="#parameters.typeid" />&
                                sex=MAN&brandid=<s:property value="#brand.code" />">男款</a>
                </li>
                <li>
                    <a  href="<s:url action="frontProductlist"/>?sort=sellpriceasc&
                                typeid=<s:property value="#parameters.typeid" />&
                                sex=WOMEN&brandid=<s:property value="#brand.code" />">女款</a>
                </li>
                <li>
                    <a  href="<s:url action="frontProductlist"/>?sort=sellpriceasc&typeid=<s:property value="#parameters.typeid" />&
                                sex=NONE&brandid=<s:property value="#brand.code" />">男女均可</a>
                </li>
                <li>
                    <a class="red" href="<s:url action="frontProductlist"/>?sort=sellpriceasc&typeid=<s:property value="#parameters.typeid" />">全部</a>
                </li>
                </ul>
             </div>
        </div>
         <div id="divNaviTop" class="number"> 
              <div class="number_l">以下<span class='number_white'><s:property value="#request.pageView.totalrecords" /></span>条结果按<span class="number_white">
                <s:if test="%{'selldesc' == #request.sort}">
                    销量多到少
                </s:if>
                <s:elseif test="%{'sellpricedesc' == #request.sort}">
                    价格高到低
                </s:elseif>
                <s:elseif test="%{'sellpriceasc' == #request.sort}">
                    价格低到高
                </s:elseif>
                <s:else>
                    最近上架时间
                </s:else>
              </span>排列,显示方式是<span class="number_white"><s:if test="##">图文版</s:if><s:if test="##">图片版</s:if></span> 每页显示<span class="number_white">${pageView.maxresult}</span>条</div>
              <div class="turnpage">
                <div><em>第${pageView.currentpage}页</em></div>
              </div>
         </div>

    <div class='goods_pic'>
<!---------------------------LOOP START------------------------------>
<s:iterator value="#request.pageView.records" var="entry">
    <div class="detail">   
        <div class="goods" style="cursor:hand;background:url(<s:iterator value="#entry.styles" var="pic"><s:property value="#pic.image140FullPath" /> </s:iterator>) center center no-repeat">
            <a href="/html/product/<s:property value="#entry.type.typeid" />/<s:property value="#entry.id" />.shtml" target="_blank">
                <img src="../../../images/global/product_blank.gif" alt="<s:property value="#entry.name" />" width="140" height="168"  border="0"/>
             </a>
         </div>
        <h2>
            <a href="/html/product/<s:property value="#entry.type.typeid" />/<s:property value="#entry.id" />.shtml" target="_blank" title="<s:property value="#entry.name" />}"><s:property value="#entry.name" /></a>
        </h2>
        <div class="save_number">
            <s>¥<s:property value="#entry.marketprice" /></s> 
            <strong><em>¥<s:property value="#entry.sellprice" /></em></strong> 节省:<s:property value="#entry.savedPrice" />
        </div>
        <div class="an_img" align="center">
            <a href="/html/product/<s:property value="#entry.type.typeid" />/<s:property value="#entry.id" />.shtml">
                <img src='images/sale.gif' width='84' height='24' border='0' class='a_1' />
            </a>
        </div>
     </div>
</s:iterator>
<!----------------------LOOP END------------------------------->
        <div class='emptybox'></div>
    </div>
         <div id="divNaviBottom" class="page_number">
         <div class="turnpage turnpage_bottom"> 
        <s:iterator begin="%{#request.pageView.pageindex.startpage}" end="%{#request.pageView.pageindex.endpage}" var="index">
            <s:if test="#request.pageView.currentpage == #index"><div class='red'>第${index}页 </div></s:if>
            <s:else>
                <div class="page">
                    <a href="<s:url action="frontProductlist" />?page=${index }&typeid=<s:property value="#parameters.typeid" />&sort=<s:property value="#parameters.sort" />" onclick="topage(${index })" class="a03">第${index}页 </a>
                </div>
            </s:else>
        </s:iterator>
        <div>  </div>
        跳转到第
        <select name="selectPage" class="kuang" onchange="javascript:topage(this.value)">
            <s:iterator begin="1" end="#request.pageView.totalpage" var="index"> 
                <option value="${index }" &amp;amp;amp;amp;lt;s:if test="%{#request.pageView.currentpage == #index}" >selected="selected"</s:if> >
                    <s:property value="#index" />
                </option>
            </s:iterator>
        </Select> 
        页
        <input type="hidden" id="typeid" value="<s:property value="#parameters.typeid" />" />
        <input type="hidden" id="sort" value="<s:property value="#parameters.sort" />" />
        <SCRIPT type="text/javascript">

            function topage(pagenum)
            {
                var typeid=document.getElementById("typeid").value;
                var sort=document.getElementById("sort").value;
                window.location.href="<s:url action='frontProductlist' />?sort="+ sort +"&page="+ pagenum +"&typeid="+ typeid;
            }
        </SCRIPT>
         </div>
         </div>
    </div>
    <jsp:include page="/page/share/Foot.jsp" />
</body>
</html>


后台代码展示

/**
 * 功能:这个是前台产品展示的控制器
 * 时间:2015年6月3日09:46:50
 * 文件:FrontProductAction.java
 * 作者:cutter_point
 */
package com.cutter_point.web.action.product;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cutter_point.bean.PageView;
import com.cutter_point.bean.QueryResult;
import com.cutter_point.bean.product.ProductInfo;
import com.cutter_point.bean.product.ProductStyle;
import com.cutter_point.bean.product.ProductType;
import com.cutter_point.bean.product.Sex;
import com.cutter_point.service.product.ProductInfoService;
import com.cutter_point.service.product.ProductTypeService;
import com.cutter_point.web.formbean.product.FrontProductForm;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Scope("prototype")     //这个表示每次访问的时候都会有一个新的对象创建
public class FrontProductAction extends ActionSupport implements ServletRequestAware, ModelDriven<FrontProductForm>
{
    private static final long serialVersionUID = -568904658537583015L;
    @Resource
    private ProductInfoService productInfoService;  //业务注入
    @Resource
    private ProductTypeService productTypeService;  //业务注入
    private HttpServletRequest request;
    private FrontProductForm pf;    //一个表单bean

    @Override
    public String execute() throws Exception 
    {
        PageView<ProductInfo> pageview = new PageView<ProductInfo>(2, pf.getPage());
        pageview.setPagecode(20);   //这个是为了设置页码数
        int firstindex = (pageview.getCurrentpage() - 1) * pageview.getMaxresult(); //得到从哪个开始索引的值
        //按什么排序的选择
        LinkedHashMap<String, String> orderby = this.buildOrder(pf.getSort());  //获取到相应的排序规则
        //执行的SQL语句
        StringBuilder sql = new StringBuilder(" o.visible=?");  //用来组装的SQL语句
        List<Object> params = new ArrayList<Object>();      //用来给?赋值的参数列表
        params.add(true);   //只有显示为产品上架的才会被显示

        //判断是不是有typeid
        if(pf.getTypeid() != null)
        {
            sql.append(" and o.typeid=?");
            params.add(pf.getTypeid()); //查询出相应的类别id号作为条件
        }

        //这里先判断一下品牌的id是不是为空
        if(pf.getBrandid() != null && !"".equals(pf.getBrandid().trim()))
        {
            //这里组装一条语句
            sql.append(" and o.brandid=?");
            params.add(pf.getBrandid());    //查询出相应的品牌id号作为条件
        }

        //判断选择的性别要求
        if(pf.getSex() != null)
        {
            //获取性别属性
            String sex = pf.getSex().trim();
            if("NONE".equalsIgnoreCase(sex) || "MAN".equalsIgnoreCase(sex) || "WOMEN".equalsIgnoreCase(sex))
            {
                sql.append(" and o.sexrequest=?");
                params.add(Sex.valueOf(sex).toString());    //根据String获取相应的值
            }
        }

        //获取所有的子类,包括子类的子类
        List<Integer> typeids = new ArrayList<Integer>();
        //首先把顶级要查询的父类放进去
        //判断是不是有typeid
        if(pf.getTypeid() != null)
        {
            typeids.add(pf.getTypeid());
            this.getTypeids(typeids, new Integer[]{pf.getTypeid()});
            //吧查询出来的所有类型号查询出来
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < typeids.size(); ++i)
            {
                sb.append("?,");
            }
            //去掉逗号
            sb.deleteCharAt(sb.length() - 1);
            sql.append("and o.typeid in (" + sb.toString() + ")");
            params.addAll(typeids);  //传入参数
        }

        QueryResult<ProductInfo> qr = null;
        //根据相应的条件取得相应的数据
        qr = productInfoService.getScrollData(ProductInfo.class, firstindex, pageview.getMaxresult(), sql.toString(), params.toArray(), orderby);
        pageview.setQueryResult(qr);

        //显示相应产品的样式的时候,我们要显示一个
        for(ProductInfo product : pageview.getRecords())
        {
            Set<ProductStyle> styles = new HashSet<ProductStyle>();
            //然后把取出来的产品的样式一个一个地取出来
            for(ProductStyle style : product.getStyles())
            {
                //取出一个可以上架的样式就跳出循环
                if(style.getVisible())
                {
                    styles.add(style); //给要显示的样式添加一个对象
                    break;
                }
            }
            product.setStyles(styles); //每个产品的样式给设置出来
        }

        request.setAttribute("sort", pf.getSort());
        request.setAttribute("pageView", pageview);
        if(pf.getTypeid() != null)
        {
            Integer[] ids = new Integer[typeids.size()];    //吧类型转换为数组
            for(int i = 0; i < typeids.size(); ++i)
            {
                ids[i] = typeids.get(i);
            }
            request.setAttribute("brands", productInfoService.getBrandsByProductTypeid(ids));   //查询出相应的品牌
            ProductType type = productTypeService.find(ProductType.class, pf.getTypeid());  //查找到当前的类别
            //页面循环显示导航路径
            List<ProductType> types = new ArrayList<ProductType>();
            types.add(type);    //吧这个类别添加到这个路径中
            ProductType parenttype = type.getParent();  //获取父类类型
            //吧所有的父类,父类的父类的父类。。。全部放到list作为路径
            while(parenttype  != null)
            {
                types.add(parenttype);
                parenttype = parenttype.getParent();
            }
            request.setAttribute("producttype", type);  //查询出相应的当前类别
            request.setAttribute("types", types);   //查询出相应的序列
        }
        return "list";
    }

    /**
     * 查询出所有的子类id(子类的子类全部获取)
     * @param outtypeids    这个是查询出来的所有的有关id号
     * @param typeids   父类id
     * @return
     */
    public void getTypeids(List<Integer> outtypeids, Integer[] typeids)
    {
        //首先查出父类id的所有子类id
        List<Integer> subtypeids = productTypeService.getSubTypeid(typeids);
//      //吧自身也包含进去
//      List<Integer> typeidss = Arrays.asList(typeids);
//      outtypeids.addAll(typeidss);    //吧自己也加进去
        //只要查出来的子类id号不为空说明子类可能还有子类,一直把最后一层之类查询不出来为止
        if(subtypeids != null && subtypeids.size() > 0)
        {
            //吧查询出来的子类放出到参数中存放
            outtypeids.addAll(subtypeids);
            //然后把查询出来的子类当做父类进行查询
            Integer[] ids = new Integer[subtypeids.size()];
            for(int i = 0; i < subtypeids.size(); ++i)
            {
                ids[i] = Integer.valueOf(subtypeids.get(i).toString());
            }
            getTypeids(outtypeids, ids);    //吧List转化为数组
        }
    }

    /**
     * 组装排序规则
     * @param orderfiled
     * @return
     */
    private LinkedHashMap<String, String> buildOrder(String orderfiled)
    {
        LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
        //判断字符串前面的 是不是以字符串orderfiled结尾.
        if("selldesc".equals(orderfiled))
        {
            orderby.put("sellcount", "desc");   //这个是以销量来排序
        }
        else if("sellpricedesc".equals(orderfiled)) //这个是以销售价格的高低来排序
        {
            orderby.put("sellprice", "desc");
        }
        else if("sellpriceasc".equals(orderfiled)) //这个是以价格的低高来排序
        {
            orderby.put("sellprice", "asc");
        }
        else    //以产品的上架时间来判断
        {
            orderby.put("createdate", "desc");
        }
        return orderby; //返回相应的排序列表
    }

    public ProductInfoService getProductInfoService() {
        return productInfoService;
    }

    public void setProductInfoService(ProductInfoService productInfoService) {
        this.productInfoService = productInfoService;
    }

    public FrontProductForm getPf() {
        return pf;
    }

    public void setPf(FrontProductForm pf) {
        this.pf = pf;
    }

    @Override
    public FrontProductForm getModel() 
    {
        //从页面获取表单值
        if(pf == null)
            pf = new FrontProductForm();

        return pf;
    }

    @Override
    public void setServletRequest(HttpServletRequest arg0) 
    {
        this.request = arg0;
    }

}


前台页面显示

这个页面都是我们最后做好的页面,这里可能有一点点和上面的代码对不上,但是基本一样了



总结

这个功能的实现代码并不复杂,代码量也不多,但是最麻烦的时候我们的页面代码很难调试,也许是我对CSS和HTML更重要的一点我觉得是我对struts2的标签库的不熟悉
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: