81,092
社区成员
发帖
与我相关
我的任务
分享
package com.myboxcs.bean.product;
``````此处省略``````
@Entity
public class ProductInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = -1136568171160218438L;
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>();
@OneToMany(cascade={CascadeType.REMOVE,CascadeType.PERSIST},mappedBy="product")
public Set<ProductStyle> getStyles() {
return styles;
}
public void setStyles(Set<ProductStyle> styles) {
this.styles = styles;
}
public void removestyles(ProductStyle styles){
if(this.styles.contains(styles)){
this.styles.remove(styles);
styles.setProduct(null);
}
}
public void addstyles(ProductStyle styles){
if(!this.styles.contains(styles)){
this.styles.add(styles);
styles.setProduct(this);
}
}
package com.myboxcs.bean.product;
``````此处省略``````
@Entity
public class ProductStyle implements Serializable{
/**
*
*/
private static final long serialVersionUID = -772326910196394263L;
private Integer id;
private String name;//样式名称
private String imagename;//图片
private Boolean visible=true;//是否可见
private ProductInfo product;
public ProductStyle() {}
public ProductStyle(String name, String imagename) {
this.name = name;
this.imagename = imagename;
}
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
@JoinColumn(name="productid")
public ProductInfo getProduct() {
return product;
}
public void setProduct(ProductInfo product) {
this.product = product;
}
@Id @GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(length=80)
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;
}
@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;
final ProductStyle other = (ProductStyle) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
package junit.test;
``````此处省略``````
public class ProductInfoServiceTest {
public static ApplicationContext cxt;
public static ProductInfoService productInfoService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
cxt = new ClassPathXmlApplicationContext("beans.xml");
productInfoService= (ProductInfoService)cxt.getBean("productInfoServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testSave() {
ProductInfo product =new ProductInfo();
product.setName("含笑半步颠");
product.setBaseprice(100f);
product.setBrand(new Brand("063e5af7-30de-41a7-ad95-dbe8c32bc2e7"));
product.setCode("XW001");
product.setDescription("居家旅行必备良药");
product.setMarketprice(200f);
product.setSellprice(150f);
product.setModel("TNT");
product.setSexrequest(Sex.NONE);
product.addstyles(new ProductStyle("蓝瓶","xxxx.gif"));
product.setType(new productType(32));
product.setWeight(50);
productInfoService.save(product);
}
@Test
public void testupdate(){
ProductInfo product = productInfoService.find(ProductInfo.class, 2);
product.setName("最春哥");
product.setBaseprice(100f);
product.setSellprice(200f);
product.setMarketprice(150f);
product.setBrand(new Brand("063e5af7-30de-41a7-ad95-dbe8c32bc2e7"));
product.setDescription("cc");
product.setBuyexplain("ffff");
product.setCode("ddd");
product.setModel("1010");
product.setSexrequest(Sex.NONE);
product.setWeight(150);
product.setType(new productType(32));
productInfoService.update(product);
}
}
/**
*
产品修改
*/
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductForm formbean=(ProductForm) form;
ProductInfo product = productInfoService.find(ProductInfo.class, formbean.getProductid());
/*product.setName(formbean.getName());
product.setBaseprice(formbean.getBaseprice());
product.setSellprice(formbean.getSellprice());
product.setMarketprice(formbean.getMarketprice());
if(formbean.getBrandid()!=null && "".equals(formbean.getBrandid().trim())){
product.setBrand(new Brand(formbean.getBrandid()));
}
product.setDescription(formbean.getDescription());
product.setBuyexplain(formbean.getBuyexplain());
product.setCode(formbean.getCode());
product.setModel(formbean.getModel());
product.setSexrequest(Sex.valueOf(formbean.getSex()));
product.setWeight(formbean.getWeight());
product.setType(new productType(32));
productInfoService.update(product);*/
System.out.println(product); 我直接打印product 发现是为空的所以product.set 时候就开始报错
request.setAttribute("massage", "修改成功");
return mapping.findForward("massage");
}
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/page/share/taglib.jsp" %>
<html>
<head>
<title>修改产品</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/myboxcs/css/vip.css" type="text/css">
<SCRIPT language=JavaScript src="/myboxcs/js/FoshanRen.js"></SCRIPT>
<script type="text/javascript" src="/myboxcs/js/jscripts/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
language : "zh_cn",
mode : "textareas",
theme : "advanced",
//width : "500",
plugins : "table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,zoom,flash,searchreplace,print,contextmenu",
theme_advanced_buttons1_add_before : "save,separator",
theme_advanced_buttons1_add : "fontselect,fontsizeselect",
theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor",
theme_advanced_buttons2_add_before: "cut,copy,paste,separator,search,replace,separator",
theme_advanced_buttons3_add_before : "tablecontrols,separator",
theme_advanced_buttons3_add : "emotions,iespell,flash,advhr,separator,print",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_path_location : "bottom",
plugin_insertdate_dateFormat : "%Y-%m-%d",
plugin_insertdate_timeFormat : "%H:%M:%S",
extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
external_link_list_url : "example_data/example_link_list.js",
external_image_list_url : "example_data/example_image_list.js",
flash_external_list_url : "example_data/example_flash_list.js"
});
function Formfield(name, label){
this.name=name;
this.label=label;
}
function verifyForm(objForm){
tinyMCE.triggerSave();//手动把iframe的值赋给textarea表单元素
var list = new Array(new Formfield("name", "产品名称"),new Formfield("typeid", "产品类型"),
new Formfield("baseprice", "产品底价"),new Formfield("marketprice", "产品市场价")
,new Formfield("sellprice", "产品销售价"),new Formfield("description", "产品描述"));
for(var i=0;i<list.length;i++){
var objfield = eval("objForm."+ list[i].name);
if(trim(objfield.value)==""){
alert(list[i].label+ "不能为空");
if(objfield.type!="hidden" && objfield.focus())
objfield.focus();
return false;
}
}
var brandid=objForm.brandid.value;
if(brandid=="0")
{
alert("请选择品牌");
return false;
}
return true;
}
function SureSubmit(objForm){
if (verifyForm(objForm)) objForm.submit();
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<html:form action="/control/product/manage" enctype="multipart/form-data" method="post">
<input type="hidden" name="method" value="edit">
<input type="hidden" name="typeid">
<input type="hidden" name="productid">
<table width="98%" border="0" cellspacing="1" cellpadding="3" align="center">
<tr bgcolor="6f8ac4">
<td colspan="2" ><font color="#FFFFFF">添加产品:</font></td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">产品名称 :</div></td>
<td width="75%"><html:text property="name" size="50" maxlength="40"/><font color="#FF0000">*</font></td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">产品类别<font color="#FF0000">*</font> :</div></td>
<td width="75%"> <input type="text" name="v_type_name" disabled="true" size="30" value="${typename}"/>
<input type="button" name="select" value="选择..." onClick="javaScript:winOpen('<html:rewrite action="/control/product/manage"/>?method=selectUI','列表',600,400)">(<a href="<html:rewrite action='/control/product/type/manage'/>?method=addUI">添加产品类别</a>)
</td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">底(采购)价 :</div></td>
<td width="75%"> <html:text property="baseprice" size="10" maxlength="10" onkeypress="javascript:InputLongNumberCheck()"/>元 <font color="#FF0000">*</font></td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">市场价 :</div></td>
<td width="75%"> <html:text property="marketprice" size="10" maxlength="10" onkeypress="javascript:InputLongNumberCheck()"/>元 <font color="#FF0000">*</font></td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">销售价 :</div></td>
<td width="75%"> <html:text property="sellprice" size="10" maxlength="10" onkeypress="javascript:InputLongNumberCheck()"/>元 <font color="#FF0000">*</font></td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">货号 :</div></td>
<td width="75%"> <html:text property="code" size="20" maxlength="30"/>(注:供货商提供的便于产品查找的编号)</td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">品牌 :</div></td>
<td width="75%">
<html:select property="brandid">
<html:optionsCollection name="brands" label="name" value="code"/>
</html:select>(<a href="<html:rewrite action='/control/brand/manage'/>?method=addUI")>添加品牌</a>)</td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">适用性别 :</div></td>
<td width="75%">
<html:select property="sex">
<html:option value="NONE">男女不限</html:option>
<html:option value="MAN">男士</html:option>
<html:option value="WOMEN">女士</html:option>
</html:select>
</td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">型号 :</div></td>
<td width="75%"> <html:text property="model" size="35" maxlength="30"/></td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">重量 :</div></td>
<td width="75%"> <html:text property="weight" size="10" maxlength="10" onkeypress="javascript:InputLongNumberCheck()"/>克</td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%"> <div align="right">购买说明 :</div></td>
<td width="75%"> <html:text property="buyexplain" size="35" maxlength="30"/></td>
</tr>
<tr bgcolor="f5f5f5">
<td width="25%" valign="top"> <div align="right">产品简介<font color="#FF0000">*</font> :</div></td>
<td width="75%"><html:textarea property="description" cols="80" rows="23"></html:textarea></td>
</tr>
<tr bgcolor="f5f5f5">
<td colspan="2"> <div align="center">
<input type="button" name="edit" value=" 确 认 " class="frm_btn" onClick="javascript:SureSubmit(this.form)">
<input type="button" name="Button" value=" 返 回 " class="frm_btn" onclick="javascript:history.back()">
</div></td>
</tr>
</table>
</html:form>
<br>
</body>
</html>
package com.myboxcs.web.action.product;
此处省略
@Controller("/control/product/manage")
public class ProductManageAction extends DispatchAction {
@Resource (name="productInfoServiceBean")
private ProductInfoService productInfoService;
@Resource(name="brandServiceBean")
private BrandService brandService;
@Resource (name="productTypeServiceBean")
private ProductTypeService productTypeService;
/**
*
选择类别
此处省略
/**
*
修改界面
*/
public ActionForward editUI(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductForm formbean=(ProductForm) form;
ProductInfo product=productInfoService.find(ProductInfo.class, formbean.getProductid());
formbean.setBaseprice(product.getBaseprice());
if(product.getBrand()!=null) formbean.setBrandid(product.getBrand().getCode());
formbean.setBuyexplain(product.getBuyexplain());
formbean.setCode(product.getCode());
formbean.setDescription(product.getDescription());
formbean.setMarketprice(product.getMarketprice());
formbean.setModel(product.getModel());
formbean.setName(product.getName());
formbean.setSex(product.getSexrequest().toString());
formbean.setTypeid(product.getType().getTypeid());
formbean.setWeight(product.getWeight());
formbean.setSellprice(product.getSellprice());
request.setAttribute("brands", brandService.getScrollData(Brand.class).getResultlist());
request.setAttribute("typename", product.getType().getGoodsname());
return mapping.findForward("edit");
}
/**
*
添加界面
*/
public ActionForward addUI(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductForm formbean=(ProductForm) form;
request.setAttribute("brands", brandService.getScrollData(Brand.class).getResultlist());
return mapping.findForward("add");
}
/**
*
添加产品
*/
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductForm formbean=(ProductForm) form;
ProductInfo product = new ProductInfo();
product.setName(formbean.getName());
product.setBaseprice(formbean.getBaseprice());
product.setSellprice(formbean.getSellprice());
product.setMarketprice(formbean.getMarketprice());
if(formbean.getBrandid()!=null && "".equals(formbean.getBrandid().trim())){
product.setBrand(new Brand(formbean.getBrandid()));
}
product.setDescription(formbean.getDescription());
product.setBuyexplain(formbean.getBuyexplain());
product.setCode(formbean.getCode());
product.setModel(formbean.getModel());
product.setSexrequest(Sex.valueOf(formbean.getSex()));
product.setWeight(formbean.getWeight());
product.setType(new productType(formbean.getTypeid()));
if(formbean.getImagefile()!=null && formbean.getImagefile().getFileSize()>0){
String ext=formbean.getImagefile().getFileName().substring(formbean.getImagefile().getFileName().lastIndexOf("."));
String imagename=UUID.randomUUID().toString()+ext;
product.addstyles(new ProductStyle(formbean.getStylename(),imagename));
productInfoService.save(product);
String logopathdir="/images/product/"+ formbean.getTypeid()+"/"+product.getId()+"/prototype";
String logorealpathdir = request.getSession().getServletContext().getRealPath(logopathdir);
File logosavedir= new File(logorealpathdir);
if(!logosavedir.exists()) logosavedir.mkdirs();
FileOutputStream fileoutstream = new FileOutputStream(new File(logorealpathdir,imagename));
fileoutstream.write(formbean.getImagefile().getFileData());
fileoutstream.close();
//String logopath ="/myboxcs"+logopathdir+"/"+imagename;
}
request.setAttribute("massage", "添加成功");
return mapping.findForward("massage");
}
/**
*
产品修改
*/
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ProductForm formbean=(ProductForm) form;
ProductInfo product = productInfoService.find(ProductInfo.class, formbean.getProductid());
product.setName(formbean.getName());
product.setBaseprice(formbean.getBaseprice());
product.setSellprice(formbean.getSellprice());
product.setMarketprice(formbean.getMarketprice());
if(formbean.getBrandid()!=null && "".equals(formbean.getBrandid().trim())){
product.setBrand(new Brand(formbean.getBrandid()));
}
product.setDescription(formbean.getDescription());
product.setBuyexplain(formbean.getBuyexplain());
product.setCode(formbean.getCode());
product.setModel(formbean.getModel());
product.setSexrequest(Sex.valueOf(formbean.getSex()));
product.setWeight(formbean.getWeight());
product.setType(new productType(formbean.getTypeid()));
productInfoService.update(product);
request.setAttribute("massage", "修改成功");
return mapping.findForward("massage");
}
}