67,515
社区成员
发帖
与我相关
我的任务
分享
@Override
protected void prepareModel() throws Exception
{
if (id != null)
{
entity = productManager.get(id) ;
productAttributeValue = new ArrayList<ProductAttributeValue>(entity.getProductAttributeValues()) ;
}
else
{
entity = new Product() ;
}
}
//input其实也很平常,没什么特别的。下面的input与问题似乎无关。
@Override
public String input() throws Exception
{
allTrades = tradeManager.getAll() ;
selectedTradeId = entity.getTradeId() ;
allCategories = categoryManager.getAll() ;
if (id != null)
{
selectedCategoryId = entity.getCategory().getId() ;
}
return INPUT ;
}
@Override
public String save() throws Exception
{
entity.setTrade(tradeManager.get(selectedTradeId)) ;
entity.setCategory(categoryManager.get(selectedCategoryId)) ;
if (id == null)
{
for (int i = 0; i < productAttributeValue.size(); i++)
{
//如果属于新建产品,则需要在productAttributeValue设置它的product
productAttributeValue.get(i).setProduct(entity) ;
}
}
//转换list为set
Set<ProductAttributeValue> productAttributeValues = new HashSet<ProductAttributeValue>(productAttributeValue) ;
entity.setProductAttributeValues(productAttributeValues) ;
entity.setCreateTime(new java.util.Date()) ;
productManager.save(entity) ;
addActionMessage("保存产品成功") ;
return RELOAD ;
}
productAttributeValue的get/set,略。(普通的get/set)
... ...
前台jsp中,根据产品的分类自动创建若干个input类型的产品属性让用户输入、提交。相关代码:
function initInput()
{
for(var i=0;i<attributeList.length;i++)
{
var id = parseInt(attributeList[i][0]);
var n = attributeList[i][1];
var t = attributeList[i][2];
var opt = document.createElement('input');
opt.setAttribute('name','productAttributeValue['+i+'].attributeValue');
var idOpt = document.createElement('input');
idOpt.setAttribute('type','hidden');
idOpt.setAttribute('name','productAttributeValue['+i+'].categoryAttribute.id');
idOpt.setAttribute('value',id);
var productIdOpt = document.createElement('input');
productIdOpt.setAttribute('type','hidden');
productIdOpt.setAttribute('name','productAttributeValue['+i+'].product.id');
productIdOpt.setAttribute('value',$("#id").val());
var inputDiv=document.createElement('div');
//取出每一个属性的分类并单独显示
if(i>0)
{
if( t!=attributeList[i-1][2])
{
inputDiv.innerHTML+="<br>";
inputDiv.innerHTML+="<font color=red>"+t+"</font>:<hr>";
}
}
else
{
inputDiv.innerHTML+="<br>";
inputDiv.innerHTML+="<font color=red>"+t+"</font>:<hr>";
}
inputDiv.innerHTML+=n+":";
inputDiv.appendChild(opt);
inputDiv.appendChild(idOpt);
inputDiv.appendChild(productIdOpt);
document.getElementById('attributeDiv').appendChild(inputDiv);
iMaxLength++;
}
}