前端分析:
下面概要的分析一下前端的具体实现:
首先,当此界面(item_param_add.jsp)加载成功后会调用我们再common.js中定义的initItemCat方法,其传递了一个json形式的方法,fun为名称,后面具体的为方法的定义,方法主要是判断一个类目的规格参数模板是否已经添加过,如果添加了就提示,若没添加就调用增加分组。那上述的方法具体是怎么执行的呢?
从代码里可以了解到,我们会将子节点中的内容查找name = cid的符合项,作为节点的id,假如data也就是上面我们传入的内容存在并且时,就会回调item_param_add页面中的响应的方法,如果没有的话,则会去创建,添加选择栏目。
功能分析及实现
可以从以上的代码片段中发现,需要请求的url为:/item/param/query/itemcatid/{itemCatId}参数为:itemCatId,返回值为自定义的NewstylesResult消息响应类。
Dao层:
从tb_item_param表中根据商品分类id查询内容。分析业务逻辑可以发现,这个过程为单表操作。因此,可以实现前面章节使用逆向工程生成的代码。
Service层:
功能:接收商品分类id。调用mapper查询tb_item_param表,返回结果NewstylesResult。
定义的接口为:
import com.newstyles.common.pojo.NewstylesResult;
public interface ItemParamService {
NewstylesResult getItemParamByCid(long id);
}
对应的实现类为:
@Service
public class ItemParamServiceImpl implements ItemParamService {
@Autowired
private TbItemParamMapper itemParamMapper;
@Override
public NewstylesResult getItemParamByCid(long cid){
TbItemParamExample example = new TbItemParamExample();
Criteria criteria = example.createCriteria();
criteria.andItemCatIdEqualTo(cid);
List<TbItemParam> list = itemParamMapper.selectByExampleWithBLOBs(example);
//判断是否有结果
if(list != null && list.size() > 0){
return NewstylesResult.ok(list.get(0));
}
return NewstylesResult.ok();
}
}
Controller层:
@Controller
@RequestMapping("/item/param")
public class ItemParamController {
private ItemParamService itemParamService;
//RESTful风格的请求参数
@RequestMapping("/query/itemcatid/{itemCatId}")
@ResponseBody
public NewstylesResult getItemParamByCid(@PathVariable long itemCatId){
NewstylesResult result = itemParamService.getItemParamByCid(itemCatId);
return result;
}
}
实现结果如下:
可以看到,能够将对应的选择的类目显示到对应位置。并且能够完整的建立规格参数的模板。
提交规格参数模板
首先把页面中所有文本框中的内容转换成json数据。把json字符串提交给后台。保存到规格参数表中。首先查看jsp中的,有关于提交按钮的代码:
可以看到下一行代码中有一个json.stringify方法,这个方法将json格式的数据转换为一个string类型的数据,并且看到,请求的参数名为paramData,返回值可以使用NewstylesResult消息类来设计,这样分析之后,我们可以一步步实现了:
Dao层:
保存规格参数模板,向tb_item_param表添加一条记录。分析可以发现,这个业务逻辑是基于单表查询的,所以可以使用逆向工程生成的代码。
Service层:
功能:接收TbItemParam对象。 把对象调用mapper插入到tb_item_param表中。返回NewstylesResult。
在上面定义的接口中添加接口:
NewstylesResult insertItemParam(TbItemParam itemParam);
对应的实现类中,需要实现上述接口为:
@Override
public NewstylesResult insertItemParam(TbItemParam itemParam){
//补全pojo类
itemParam.setCreated(new Date());
itemParam.setUpdated(new Date());
//插入到规格参数表中
itemParamMapper.insert(itemParam);
return NewstylesResult.ok();
}
Controller层:
在上面定义的Controller层中添加一个对应的控制: @RequestMapping("/save/{cid}")
@ResponseBody
public NewstylesResult isnertItemParam(@PathVariable long cid,String paramData){
TbItemParam itemParam = new TbItemParam();
itemParam.setItemCatId(cid);
itemParam.setParamData(paramData);
NewstylesResult result = itemParamService.insertItemParam(itemParam);
return result;
}
这样就可以实现将对应的规格参数添加到对应的模板的表中。