【j2ee spring】39、巴巴运动网的产品信息

来源:互联网 发布:阿里云官方论坛 编辑:程序博客网 时间:2024/05/18 06:27

巴巴运动网的产品信息

1、项目图解

这里写图片描述

这里写图片描述

这里写图片描述

2、我们开始做我们的相应的功能模块
页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8803985

产品显示的接口实现

/** * 功能:这个是产品业务的接口 * 文件:ProductInfoService.java * 时间:2015年5月22日16:48:25 * 作者:cutter_point */package com.cutter_point.service.product;import com.cutter_point.service.base.DAO;public interface ProductInfoService extends DAO{    //这里面定义ProductInfoService专有的方法}/** * 功能:这是产品的服务类 * 文件:ProductInfoServiceBean.java * 时间:2015年5月27日10:17:45 * 作者:cutter_point */package com.cutter_point.service.product.impl;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import com.cutter_point.service.base.DaoSupport;import com.cutter_point.service.product.ProductInfoService;@Service    //相当于在spring里面定义一个bean,这是注解的方式<context:component-scan base-package="com.cutter_point" />@Transactional      //在方法执行的时候开启事务public class ProductInfoServiceBean extends DaoSupport implements ProductInfoService{}

显示产品的实体

/** * 功能:性别类别 * 文件:Sex.java * 时间:2015年5月26日19:25:01 * 作者:cutter_point */package com.cutter_point.bean.product;public enum Sex{    NONE     {        @Override        public String getName()        {            return "男女不限";        }    },    MAN     {        @Override        public String getName()        {            return "男";        }    },    WOMEN     {        @Override        public String getName()        {            return "女";        }    };    public abstract String getName();}/** * 功能:这是产品样式的实体 * 文件:ProductStyle.java * 时间:2015年5月27日09:18:23 * 作者:cutter_point */package com.cutter_point.bean.product;import java.io.Serializable;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;@Entitypublic class ProductStyle implements Serializable{    private static final long serialVersionUID = -6670329064347582109L;    //id号    private Integer id;    //样式名字    private String name;    //样式照片的名字    private String imagename;    //是否可见    private Boolean visible;    //许多的样式都对应着一种产品    private ProductInfo product;    public ProductStyle()    {    }    public ProductStyle(String name, String imagename)    {        this.name = name;        this.imagename = imagename;    }    @Id @GeneratedValue     //设定为主键,且自增长    public Integer getId()    {        return id;    }    public void setId(Integer id)    {        this.id = id;    }    @Column(length = 30, nullable = false)    public String getName()    {        return name;    }    public void setName(String name)    {        this.name = name;    }    @Column(length = 40, nullable = false)    public String getImagename()    {        return imagename;    }    public void setImagename(String imagename)    {        this.imagename = imagename;    }    @Column(nullable = false)    public Boolean getVisible()    {        return visible;    }    public void setVisible(Boolean visible)    {        this.visible = visible;    }    //级联更新,并且必须选择    @ManyToOne(cascade = CascadeType.REFRESH, optional = false)    //设定外键    @JoinColumn(name = "productid")    public ProductInfo getProduct()    {        return product;    }    public void setProduct(ProductInfo product)    {        this.product = product;    }    @Override    public int hashCode()    {        final int prime = 31;        int result = 1;        result = prime * result + ((id == null) ? 0 : id.hashCode());        return result;    }    @Override    public boolean equals(Object obj)    {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        ProductStyle other = (ProductStyle) obj;        if (id == null)        {            if (other.id != null)                return false;        } else if (!id.equals(other.id))            return false;        return true;    }}/** * 功能:产品信息的实体类 * 文件:ProductInfo.java * 时间:2015年5月22日16:33:49 * 作者:cutter_point */package com.cutter_point.bean.product;import java.io.Serializable;import java.util.Date;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.EnumType;import javax.persistence.Enumerated;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.Lob;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Temporal;import javax.persistence.TemporalType;@Entitypublic class ProductInfo implements Serializable{    private static final long serialVersionUID = -9060015497593350640L;    //产品的id号    private Integer id;     //货号    private String code;    //产品名    private String name;    //品牌    private Brand brand;        //型号    private String model;       //底价,也就是采购价    private Float baseprice;        //市场价    private Float marketprice;      //销售价    private Float sellprice;        //产品重量    private Integer weight;         //产品简介    private String description;     //购买说明    private String buyexplain;      //是否可见    private Boolean visible = true;     //产品型号    private ProductType type;       //上架日期    private Date createdate = new Date();           //人气指数    private Integer clickcount = 1;         //销售量    private Integer sellcount = 0;          //是否推荐    private Boolean commend = false;        //性别要求    private Sex sexrequest = Sex.NONE;    //一种产品会有多个样式    private Set<ProductStyle> styles = new HashSet<ProductStyle>();    @Column(length = 30)    public String getCode()    {        return code;    }    public void setCode(String code)    {        this.code = code;    }    @Column(length = 50, nullable = false)    public String getName()    {        return name;    }    public void setName(String name)    {        this.name = name;    }    //一个品牌有多个产品    @ManyToOne(cascade = CascadeType.REFRESH)   //级联更新    //设定一个外键    @JoinColumn(name = "brandid")    public Brand getBrand()    {        return brand;    }    public void setBrand(Brand brand)    {        this.brand = brand;    }    @Column(length = 20)    public String getModel()    {        return model;    }    public void setModel(String model)    {        this.model = model;    }    @Column(nullable = false)    public Float getBaseprice()    {        return baseprice;    }    public void setBaseprice(Float baseprice)    {        this.baseprice = baseprice;    }    //市场价    @Column(nullable = false)    public Float getMarketprice()    {        return marketprice;    }    public void setMarketprice(Float marketprice)    {        this.marketprice = marketprice;    }    //销售价    @Column(nullable = false)    public Float getSellprice()    {        return sellprice;    }    public void setSellprice(Float sellprice)    {        this.sellprice = sellprice;    }    //重量,可以为空    public Integer getWeight()    {        return weight;    }    public void setWeight(Integer weight)    {        this.weight = weight;    }    //lob表示可以存放大点的数据    @Lob @Column(nullable = false)    public String getDescription()    {        return description;    }    public void setDescription(String description)    {        this.description = description;    }    @Column(length = 30)    public String getBuyexplain()    {        return buyexplain;    }    public void setBuyexplain(String buyexplain)    {        this.buyexplain = buyexplain;    }    @Column(nullable = false)    public Boolean getVisible()    {        return visible;    }    public void setVisible(Boolean visible)    {        this.visible = visible;    }    @ManyToOne(cascade = CascadeType.REFRESH, optional = false) //级联更新,并且这个产品类型必须存在    @JoinColumn(name = "typeid")    //设置外键    public ProductType getType()    {        return type;    }    public void setType(ProductType type)    {        this.type = type;    }    @Temporal(TemporalType.DATE)    //时间    public Date getCreatedate()    {        return createdate;    }    public void setCreatedate(Date createdate)    {        this.createdate = createdate;    }    @Column(nullable = false)    public Integer getClickcount()    {        return clickcount;    }    public void setClickcount(Integer clickcount)    {        this.clickcount = clickcount;    }    @Column(nullable = false)    public Integer getSellcount()    {        return sellcount;    }    public void setSellcount(Integer sellcount)    {        this.sellcount = sellcount;    }    @Column(nullable = false)    public Boolean getCommend()    {        return commend;    }    public void setCommend(Boolean commend)    {        this.commend = commend;    }    //枚举类型,EnumType.ORDINAL表示在数据库中存放相应的数字,EnumType.STRING表示在数据库中存放相应的字符串    @Enumerated(EnumType.STRING) @Column(length = 5, nullable = false)    public Sex getSexrequest()    {        return sexrequest;    }    public void setSexrequest(Sex sexrequest)    {        this.sexrequest = sexrequest;    }    @Id @GeneratedValue    public Integer getId()    {        return id;    }    public void setId(Integer id)    {        this.id = id;    }    //产品和样式之间的关系是一对多,这里定义级联删除,每个样式对应相应的产品    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "product")    public Set<ProductStyle> getStyles()    {        return styles;    }    public void setStyles(Set<ProductStyle> styles)    {        this.styles = styles;    }    //添加一个样式    public void addProductStyle(ProductStyle style)    {        if(!this.styles.contains(style))        {            this.styles.add(style); //添加一个样式            //同时让这个样式对应这种产品            style.setProduct(this);        }    }    //删除一个样式    public void removeProductStyle(ProductStyle style)    {        if(this.styles.contains(style)) //如果包含了这个样式        {            this.styles.remove(style);  //添加一个样式            //同时让这个样式对应这种产品            style.setProduct(null);        }    }}

产品的显示界面productlist.jsp

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" %><%@  taglib uri="/struts-tags" prefix="s"%><html><head><title>产品列表</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><link rel="stylesheet" href="../css/vip.css" type="text/css"><script type="text/javascript">    //到指定的分页页面    function topage(page)    {        document.getElementById("page").value = page;        var obj = document.getElementsByName("form1").item(0);        obj.submit();    }    function actionEvent(methodname){        var form = document.forms[0];        if(validateIsSelect(form.all, form.productids)){            form.action='<html:rewrite action="/control/product/manage"/>';            form.method.value=methodname;            form.submit();        }else{            alert("请选择要操作的记录");        }    }    function allselect()    {        var selectall = document.getElementsByName("all").item(0);  //获取得到全选按钮        var selecteveryone = document.getElementsByName("productids");        //判断全选按钮是否被选中        //如果被选中了,那么我们用循环进行和全选复选框同步为一样的        for(var i = 0; i < selecteveryone.length; ++i)        {            //吧选项一个一个地进行同步一样的            selecteveryone[i].checked = selectall.checked;        }    }    /*     * 判断是否选择了记录     */    function validateIsSelect(allobj,items){        var state = allobj.checked;        if(items.length){            for(var i=0;i<items.length;i++){                if(items[i].checked) return true;            }        }else{            if(items.checked) return true;        }        return false;    }//--></script><SCRIPT type="text/javascript" src="../js/FoshanRen.js"></SCRIPT></head><body bgcolor="#FFFFFF" text="#000000" marginwidth="0" marginheight="0"><s:form action="productlist" method="post"><s:hidden name="page"/>  <table width="98%" border="0" cellspacing="1" cellpadding="2" align="center">    <tr ><td colspan="10"  bgcolor="6f8ac4" align="right">   </td></tr>    <tr>      <td width="5%" bgcolor="6f8ac4"><div align="center"><font color="#FFFFFF">产品ID</font></div></td>      <td width="10%" bgcolor="6f8ac4"> <div align="center"><font color="#FFFFFF">货号</font></div></td>      <td width="5%" nowrap bgcolor="6f8ac4"> <div align="center"><font color="#FFFFFF">修改</font></div></td>      <td width="30%" bgcolor="6f8ac4"> <div align="center"><font color="#FFFFFF">产品名称</font></div></td>      <td width="10%" nowrap bgcolor="6f8ac4"> <div align="center"><font color="#FFFFFF">所属分类</font></div></td>      <td width="5%" bgcolor="6f8ac4"><div align="center"><font color="#FFFFFF">底价</font></div></td>      <td width="5%" bgcolor="6f8ac4"><div align="center"><font color="#FFFFFF">销售价</font></div></td>      <td width="10%" bgcolor="6f8ac4"><div align="center"><font color="#FFFFFF">在售</font></div></td>      <td width="10%" bgcolor="6f8ac4"><div align="center"><font color="#FFFFFF">推荐</font></div></td>      <td width="10%" bgcolor="6f8ac4"><div align="center"><font color="#FFFFFF">产品图片</font></div></td>    </tr><!---------------------------LOOP START------------------------------><s:iterator value="#request.pageView.records" var="entry">    <tr>      <td bgcolor="f5f5f5">         <div align="center">            <INPUT TYPE="checkbox" NAME="productids" value="<s:property value="#entry.id" />">            <s:property value="#entry.id" />        </div>      </td>      <td bgcolor="f5f5f5">             <div align="center">                <s:property value="#entry.code" />            </div>      </td>      <td bgcolor="f5f5f5">             <div align="center">                <a href="##">                    <img src="../images/edit.gif" width="15" height="16" border="0" />                </a>            </div>      </td>      <td bgcolor="f5f5f5">             <div align="center">                <s:property value="#entry.name" />            </div>      </td>      <td bgcolor="f5f5f5">             <div align="center">                <s:property value="#entry.type.name" />            </div>      </td>      <td bgcolor="f5f5f5">             <div align="center">                <s:property value="#entry.baseprice" />            </div>      </td>      <td bgcolor="f5f5f5">             <div align="center">                <s:property value="#entry.sellprice" />            </div>      </td>      <td bgcolor="f5f5f5" align="center">            <s:if test="#entry.visible">在售</s:if>            <s:if test="!#entry.visible">停售</s:if>      </td>      <td bgcolor="f5f5f5" align="center">            <s:if test="#entry.commend">推荐</s:if>            <s:if test="!#entry.commend">--</s:if>      </td>       <td bgcolor="f5f5f5">             <div align="center">                <a href="##">                    产品图片管理                </a>            </div>       </td>    </tr></s:iterator>    <!----------------------LOOP END------------------------------->    <tr>       <td bgcolor="f5f5f5" colspan="10" align="center"><table width="100%" border="0" cellspacing="1" cellpadding="3">          <tr>             <td width="8%"><INPUT TYPE="checkbox" NAME="all" &amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;s:if test="??">disabled="disabled"</s:if>             onclick="javascript:allselect()">全选</td>              <td width="85%">              <input type="button" class="frm_btn" onClick="javascript:window.location.href='<s:url action="/control/product/manage"/>?method=addUI'" value="添加产品"> &nbsp;&nbsp;              <input name="query" type="button" class="frm_btn" id="query" onClick="javascript:window.location.href='<s:url action="/control/product/manage"/>?method=queryUI'" value=" 查 询 "> &nbsp;&nbsp;              <input name="visible" type="button"              <s:if test="??">disabled="disabled"</s:if>               class="frm_btn" onClick="javascript:actionEvent('visible')" value=" 上 架 "> &nbsp;&nbsp;              <input name="disable" type="button" class="frm_btn"               <s:if test="??">disabled="disabled"</s:if>              onClick="javascript:actionEvent('disable')" value=" 下 架 "> &nbsp;&nbsp;              <input name="commend" type="button" class="frm_btn"               <s:if test="??">disabled="disabled"</s:if>              onClick="javascript:actionEvent('commend')" value=" 推 荐 "> &nbsp;&nbsp;              <input name="uncommend" type="button" class="frm_btn"                 <s:if test="??">disabled="disabled"</s:if>                onClick="javascript:actionEvent('uncommend')" value=" 不推荐 " /> &nbsp;&nbsp;            </td>          </tr>        </table></td>    </tr>  </table></s:form></body></html>

我们的页面控制器action

/** * 功能:这个是实现产品类和web层的交互 * 时间:2015年5月27日10:19:42 * 文件:ProductAction.java * 作者:cutter_point */package com.cutter_point.web.action.product;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;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.service.product.ProductInfoService;import com.opensymphony.xwork2.ActionSupport;@Controller@Scope("prototype")public class ProductAction extends ActionSupport implements ServletRequestAware{    private static final long serialVersionUID = -1041016333626692241L;    @Resource    private ProductInfoService productInfoService;  //业务注入    //request    private HttpServletRequest request;    private int page;    @Override    public String execute() throws Exception    {        PageView<ProductInfo> pageview = new PageView<ProductInfo>(12, this.getPage());        int firstindex = (pageview.getCurrentpage() - 1) * pageview.getMaxresult(); //得到从哪个开始索引的值        LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();        orderby.put("id", "asc");        StringBuilder hsql = new StringBuilder("o.visible = ?");        List<Object> params = new ArrayList<Object>();  //这个用来存放需要的排序方式        params.add(true);        QueryResult<ProductInfo> qr = productInfoService.getScrollData(ProductInfo.class, firstindex, pageview.getMaxresult(), hsql.toString(),                 params.toArray(), orderby);        pageview.setQueryResult(qr);        request.setAttribute("pageView", pageview);        return "list";    }    @Override    public void setServletRequest(HttpServletRequest arg0)    {        this.request = arg0;    }    public ProductInfoService getProductInfoService()    {        return productInfoService;    }    public void setProductInfoService(ProductInfoService productInfoService)    {        this.productInfoService = productInfoService;    }    public Integer getPage()    {        return page < 1 ? 1 : page;    }    public void setPage(Integer page)    {        this.page = page;    }}

4、struts2的配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <include file="struts-default.xml" />    <constant name="struts.ObjectFactory" value="spring" /> <!--    表示这里面的action由spring进行创建 -->    <constant name="struts.devMode" value="true" />    <!--解决乱码    -->    <constant name="struts.i18n.encoding" value="UTF-8" />    <package name="control" namespace="/control" extends="struts-default">        <global-results>            <result name="message">/page/share/message.jsp</result>        </global-results>        <action name="center-*"><!-- 直接跳转,不需要经过class的验证,默认返回success -->            <result name="success">/page/controlcenter/{1}.jsp</result>        </action>        <!-- 产品类别展示 -->        <action name="producttypelist" class="productTypeAction" method="execute">            <result name="list">/page/product/producttypelist.jsp</result>        </action>        <!-- 产品类别管理 -->        <action name="*-producttypemanage" class="productTypeManageAction" method="{1}UI">            <result name="{1}">/page/product/{1}_productType.jsp</result>        </action>        <action name="producttypemanage-*" class="productTypeManageAction" method="{1}">            <result name="{1}">/page/product/{1}_productType.jsp</result>        </action>        <!-- 品牌类别展示 -->        <action name="brandlist" class="brandAction" method="execute">            <result name="list">/page/product/brandlist.jsp</result>        </action>        <!-- 品牌类别管理 -->        <action name="*-brandmanage" class="brandManageAction" method="{1}UI">            <result name="{1}">/page/product/{1}_brand.jsp</result>        </action>        <action name="brandmanage-*" class="brandManageAction" method="{1}">            <result name="{1}">/page/product/{1}_brand.jsp</result>        </action>        <!-- 文件展示 -->        <action name="uploadfilelist" class="uploadFileAction" method="execute">            <result name="list">/page/uploadfile/uploadfilelist.jsp</result>        </action>        <!-- 文件上传管理 -->        <action name="*-filemanage" class="uploadfileManageAction" method="{1}">            <result name="{1}">/page/uploadfile/{1}.jsp</result>            <result name="fileuploadfinish">/page/uploadfile/fileuploadfinish.jsp</result>        </action>        <!-- 产品分页展示 -->        <action name="productlist" class="productAction" method="execute">            <result name="list">/page/product/productlist.jsp</result>        </action>    </package></struts>

5、接下来我们测试一下页面的效果

我们访问这个网站

http://localhost:8080/ babaSport_1400_productinfo/control/center-main

这里写图片描述

6、总结

这里只是做了一个显示的界面,后面的功能我们接着加

0 0
原创粉丝点击