亲爱的部长大人又提新需求了,希望我们能做出动态加载单选按钮的弹出框,方便后期维护。如下图所示,添加一个菜品的时候,需要定义辣的梯度:不辣,微辣,麻辣,如果后期需要添加什么变态辣啊,超辣之类的,还要去改动页面上的多处代码,后期维护起来会烦人啦。所以对这个辣做活,进行抽象和封装,给编程人员减负啊,丫的,这才是真正的懒人~
首先,我查阅了一些资料。大致解决方案如下:
第一种,特别复古特别原始的方法,类似Servlet的原理,在controller里面拼接弹出框,然后return回前台,因为要做整个页面,小主比较懒,就给pass了。.net的可以参考一下王雅瑾师姐的博客 投诉举报项目中动态加载单选框、复选框,写的特好。
第二种,就是通过Ajax在前台拼接表单,然后返回,可以参考这个博客ajax动态加载下拉框、单选框、复选框,感觉还是很复杂,也pass掉了。
第三种,采用SpringMVC的标签,form标签,能实现加载出多个单选框,但是样式不一致,也给pass掉了。
核心代码如下:
Action:
<span style="font-size:14px;"> /**
* 添加辣度梯度弹出框 王美 2015年12月22日16:49:24
* @return
*/
@RequestMapping(value="/addDishInfo.do",method=RequestMethod.GET)
public String testAudio(Map<String, Object> map){
Spcity spcity = new Spcity();
spcity.setFavoriteBall("1");//设置默认辣度
Map<Integer, String> ballMap = new HashMap<Integer, String>();
ballMap.put(1, "不辣");
ballMap.put(0, "微辣");
ballMap.put(2, "麻辣");
map.put("spcity", spcity);
map.put("ballMap", ballMap);
return "ssnc/ncgl/caipin-add";
}
</span>
SpringMVC:
<span style="font-size:14px;"><form:form action="ssnc/ncgl/addDishInfo.do" method="post" commandName="spcity">
<div class="f_label">辣度:</div>
<form:radiobuttons name="spicy" path="favoriteBall" items="${ballMap}" delimiter=" "/>
</form:form></span>
实体:
<span style="font-size:14px;"> package com.snsoft.ssnc.ncgl.action;
/**
*菜品-- 辣度实体 王美 2015年12月24日09:31:25
* @author wm
*
*/
public class Spcity {
private String id;
private String FavoriteBall;
public String getId(){
return this.id;
}
public void setId(String id){
this.id = id;
}
public String getFavoriteBall() {
return FavoriteBall;
}
public void setFavoriteBall(String favoriteBall) {
FavoriteBall = favoriteBall;
}
}</span>
第四种,(*@ο@*) 哇~终于等到你~还好我没放弃~采用JSTL表达式,完美实现,之前网上商城的项目有用过哦,现在是温习的时候啦。
编辑JSP:
<span style="font-size:14px;"><div class="formitem">
<div class="f_label">辣度:</div>
<div class="f_item">
<c:forEach items="${list}" var="m" >
<input type="radio" id="spicy${m.key }" name="spicy" value="${m.key }" <c:if test="${dishInfo.spicy ==m.key}">checked="checked"</c:if>/><label for="ld${m.key }" class="label">${m.val }</label>
</c:forEach>
</div>
</span>
编辑action:
<span style="font-size:14px;"> /**
* 打开菜品编辑页面 王美 2015年12月20日
* @param modelMap 用于保存待显示数据
* @param roleuuid 菜品ID
* @return
*/
@RequestMapping(value="/editDishInfo.do", method=RequestMethod.GET)
@SuppressWarnings("all")
public String edit(ModelMap modelMap,ModelMap map, String dishuuid){
spicy spcity = new spicy();
List<Object> list = new ArrayList<Object>();
Map<String, String> map2 = new HashMap<String, String>();
map2.put("key", "1");
map2.put("val", "不辣");
list.add(map2);
map2 = new HashMap<String, String>();
map2.put("key", "0");
map2.put("val", "微辣");
list.add(map2);
map2 = new HashMap<String, String>();
map2.put("key", "2");
map2.put("val", "麻辣");
list.add(map2);
map.put("list", list);
map.put("spcity", spcity);
//获取菜品的持久化对象
SnDishInfo dishInfo = dishInfoService.get(dishuuid);
//把使用的数据实体放入ModelMap中
modelMap.put("dishInfo", dishInfo);
//返回caipin-edit页面
return "ssnc/ncgl/caipin-edit";
}
</span>
<span style="font-size:14px;"> <div class="formitem">
<div class="f_label">辣度:</div>
<div class="f_item">
<c:forEach items="${list}" var="m">
<c:if test="${m.key==1 }" >
<input type="radio" id="spicy${m.key }" name="spicy" value="${m.key }" checked="checked" /><label for="ld${m.key }" class="label">${m.val }</label>
</c:if>
<c:if test="${m.key!=1 }">
<input type="radio" id="spicy${m.key }" name="spicy" value="${m.key }" /><label for="ld${m.key }" class="label">${m.val }</label>
</c:if>
</c:forEach>
</div>
</span>
添加Action:
<span style="font-size:14px;"> /**
* 添加菜品弹出框
* @return 添加菜品jsp
*/
@RequestMapping(value="/addDishInfo.do",method=RequestMethod.GET)
public String addDishInfo(ModelMap map){
spicy spcity = new spicy();
List<Object> list = new ArrayList<Object>();
Map<String, String> map2 = new HashMap<String, String>();
map2.put("key", "1");
map2.put("val", "不辣");
list.add(map2);
map2 = new HashMap<String, String>();
map2.put("key", "0");
map2.put("val", "微辣");
list.add(map2);
map2 = new HashMap<String, String>();
map2.put("key", "2");
map2.put("val", "麻辣");
list.add(map2);
map.put("list", list);
map.put("spcity", spcity);
return "ssnc/ncgl/caipin-add";
}</span>
实体:
<span style="font-size:14px;"> package com.snsoft.ssnc.ncgl.entity;
/**
*菜品-- 辣度实体 王美 2015年12月24日09:31:25
* @author wm
*
*/
public class SnSpicy {
private String id;
private String Favorite;
public String getId(){
return this.id;
}
public void setId(String id){
this.id = id;
}
public String getFavorite() {
return Favorite;
}
public void setFavoriteBall(String favorite) {
Favorite = favorite;
}
}
</span>
第五种,写一个静态的辣度类:
package com.snsoft.framework.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 辣度类
* @author wm
*
*/
public class Spicy {
public static final List<Map<String, String>> SPICY_LIST= new ArrayList<Map<String, String>>();
static{
Map<String, String> map2 = new HashMap<String, String>();
map2.put("key", "1");
map2.put("val", "不辣");
SPICY_LIST.add(map2);
map2 = new HashMap<String, String>();
map2.put("key", "0");
map2.put("val", "微辣");
SPICY_LIST.add(map2);
map2 = new HashMap<String, String>();
map2.put("key", "2");
map2.put("val", "麻辣");
SPICY_LIST.add(map2);
/* map2 = new HashMap<String, String>();
map2.put("key", "3");
map2.put("val", "变态辣");
SPICY_LIST.add(map2);*/
}
}
添加菜品弹出框:
/**
* 添加菜品弹出框
* @return 添加菜品jsp
*/
@RequestMapping(value="/addDishInfo.do",method=RequestMethod.GET)
public String addDishInfo(ModelMap map){
Spicy spicy=new Spicy(); //实例化辣度类
map.put("list", spicy.SPICY_LIST);//spicy.SPICY_LIST是辣度的梯度-静态属性
return "ssnc/caipin/caipin-add";
}
编辑菜品弹出框:
/**
* 打开菜品编辑页面 王美 2015年12月20日
* @param modelMap 用于保存待显示数据
* @param roleuuid 菜品ID
* @return
*/
@RequestMapping(value="/editDishInfo.do", method=RequestMethod.GET)
@SuppressWarnings("all")
public String edit(ModelMap modelMap,ModelMap map, String dishuuid){
Spicy spicy=new Spicy(); //实例化辣度类
map.put("list", spicy.SPICY_LIST);//spicy.SPICY_LIST是辣度的梯度-静态属性
//获取菜品的持久化对象
SnDishInfo dishInfo = dishInfoService.get(dishuuid);
if(dishInfo==null){
modelMap.put("errormessage", "参数异常");
return "framework/error/error";
}
//把使用的数据实体放入ModelMap中
modelMap.put("dishInfo", dishInfo);
//返回caipin-edit页面
return "ssnc/caipin/caipin-edit";
}
辣度添加jsp:
<div class="formitem">
<div class="f_label">辣度:</div>
<div class="f_item">
<c:forEach items="${list}" var="m">
<c:if test="${m.key==1 }" >
<input type="radio" id="spicy${m.key }" name="spicy" value="${m.key }" checked="checked" /><label for="ld${m.key }" class="label">${m.val }</label>
</c:if>
<c:if test="${m.key!=1 }">
<input type="radio" id="spicy${m.key }" name="spicy" value="${m.key }" /><label for="ld${m.key }" class="label">${m.val }</label>
</c:if>
</c:forEach>
</div>
辣度修改jsp:
<div class="formitem">
<div class="f_label">辣度:</div>
<div class="f_item">
<c:forEach items="${list}" var="m" >
<input type="radio" id="spicy${m.key }" name="spicy" value="${m.key }" <c:if test="${dishInfo.spicy ==m.key}">checked="checked"</c:if>/><label for="ld${m.key }" class="label">${m.val }</label>
</c:forEach>
</div>
用祖蓝和金星奶奶的两个字,就是完美~
最后,点点滴滴的积累,发现自己正在从小菜到大菜的转型中,happy everyday~~