下拉框

1.VO属性设为MAP:
private HashMap cityHm = null;
/**
* 设置城市信息
* @param id 城市ID
* @param desc 城市显示名
*/
public void setCityHm(String id,String desc)
{
if(cityHm == null){
cityHm = new HashMap();
}
cityHm.put(id,desc);
}

public void setCityHm(HashMap cityHm)
{
this.cityHm=cityHm;
}
/**
* 获取城市HASH年信息
* @return HashMap 城市信息的HASHMAP
*/
public HashMap getCityHm()
{
return cityHm;
}


2.ACTIONFORM属性设为List:
public List getCityList()
{
return cityList;
}

public void setCityList(List cityList)
{
this.cityList = cityList;
}

3.Service层中查数据赋值到MAP中:
... ...
String citySql="select CITYID,CITYNAME,PROVINCEID from city c where c.PROVINCEID = ?";
String[] cityParam={provinceId}; //通过省ID查相关城市
HashMap cityMap=this.parseParam(citySql,cityParam);
if(cityMap==null){
cityMap=new HashMap();
}
if(typeId==ActionConstants.ACT_ID_UPDATE){
cityMap.put("-1", "请选择");
}else if(typeId==ActionConstants.ACT_ID_ADD){
cityMap.put("-1", "请选择");
}else if(typeId==ActionConstants.ACT_ID_QUERY){
cityMap.put("-1", "全部");
}
vo.setCityHm(cityMap);
retrun vo;
... ...


4.Action 中赋值: getList调用下面方法

MiscPrefixInitVO initVO = (MiscPrefixInitVO)result.getretVO();
prefixNumForm.setCityList(getList(initVO.getCityHm()));
return mapping.findForward("query");

/**
* 把hashMap内容排序后放到List
*
* @param map HashMap
* @param select int
* @return ArrayList
*/
private List getList(HashMap map)
{
ArrayList list = new ArrayList();
if(map == null){
return list;
}
LabelValueBean labelValue = null;

//排序
Set set = map.keySet();
Object[] obj = set.toArray();
int[] iobj = new int[obj.length];
List keylist = new ArrayList();
for(int i = 0; i < obj.length; ++i){
iobj[i] = Integer.parseInt(obj[i] + "");
keylist.add(new Integer(iobj[i]));
}
Collections.sort(keylist);
//~排序结束

Iterator itor = keylist.iterator();
String key = null;
while(itor.hasNext()){
key = itor.next() + "";
labelValue = new LabelValueBean(map.get(key) + "", key);
list.add(labelValue);
}
return list;
}

5.JSP页面显示:
<html:select property="city">
<html:optionsCollection name="prefixNumForm" property="cityList"/>
</html:select>


直接传SQL语句合查询条件的参数查数据:-----------------------------------------------------------------------
返回的是map

private HashMap parseParam(String sql,Object[] param)throws FrameException{
ResultSet rs = null ;
PreparedStatement stm = null ;
Connection conn = null ;
HashMap map=new HashMap();
try
{
conn=JdbcConnSource.getConnection();
}
catch (Exception ex)
{
FrameException fe =new FrameException(OAMErrorCodeConst._EC_WTC_DB_ERROR,ex);
throw fe;
}
try
{
stm = conn.prepareStatement ( sql ) ;
if ( param != null )
for ( int i = 0 ; i < param.length ; i++ )
stm.setObject ( i + 1 , param[i]) ;
rs = stm.executeQuery () ;
while(rs.next())
{
map.put(rs.getString(1),rs.getString(2));
}
}
catch ( SQLException e )
{
FrameException fe =new FrameException(OAMErrorCodeConst._EC_WTC_DB_ERROR,e);
throw fe;
}
catch ( Exception e )
{
FrameException fe =new FrameException(OAMErrorCodeConst._EC_WTC_DB_ERROR,e);
throw fe;
}
finally
{
JdbcConnSource.close(rs, stm, conn);
}
return map;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值