JSP按照顺序展示下拉框 选择框

来源:互联网 发布:孤岛惊魂3优化补丁 编辑:程序博客网 时间:2024/06/18 14:53
关于JSP页面上展示下拉框字典的做法
比如下拉选国家 下拉选性别等,通常的做法是在后台设置一个全局静态变量,初始化加载到内存中,
例如下面两种
一种是静态写死
public static final Map<String,String> selectIndexMap = new LinkedMap();
//或者 = new  HashMap<String,String>(); 区别就是前者有序,可以按照你想要的顺序展示下拉框

static{
selectIndexMap.put("1", "1");
selectIndexMap.put("2", "2");;
}
第二种是数据库可维护,有数据库表存在,这种可以在登陆的时候 查询一下,如果数据有改动,就要重登陆,或者在数据改动的代码保存后


,手动写上重新查询一次数据库
public static final Map<String,String> countryTypeMap = new  LinkedMap();
加载的函数 
public void QueryCountry() {
//下面是加载国家的map
countryTypeMap.clear();
List<ConstantVo> countryList = queryGlobalConstantDaoImpl.queryCountry();
if(countryList != null && countryList.size()>0){
for(ConstantVo vo : countryList){
countryTypeMap.put(vo.getSelect_code(), vo.getSelect_name());
}
}
}
一般我们数据库都是utf-8设置的,那尽管我们是有序的map,我们前台展示的时候 怎么按照国家拼音的数据展示呢
就要在查询的语句上下手了
order by convert(country_name using gbk) asc; //mysql 按照拼音排序,这样前台展示出来才是按照数据的,用户选择的时候才能方便的找到自己想要的选项。

JSP的展示就很简单了,struts的标签
<p>
<label>允许国家类型:</label>
<s:select id="country_code" name="versionUpdateVo.country_codes" 


list="%{@com.smartshown.common.GlobalConstant@countryTypeMap}" ---引用我们定义的静态map
  listKey="key"   listValue="value" multiple="true" /> --是否多选 当然 还有emptyOption headerKey headervalue等可以设置      
<br />
</p>
0 0
原创粉丝点击