jsp中使用select,并实现通过request中的参数进行默认选中

        使用Action获得数据,并用select(下拉框)进行数据显示,而且需要根据request中的参数进行判断并默认选中。jsp中需要用到struts2的标签。

需要先引入struts2的标签库:

<%@ taglib prefix="s" uri="/struts-tags"%>

一下是select组件:

<div class="title">
 <table width="100%" height="31px" bgcolor="#2F589C">
  <tr height="31px">
   <td width="50%" height="31px" align="right"><h1>物资到货监控(导地线)</h1></td>
   <td width="50%" height="31px" align="left" bgcolor="#2F589C">
    <select id="batch" name="batch">
     <option value="请选择">请选择</option>
     <s:iterator value="#request.batchList" status="entry" id="item">
      <option value="#item.get('PLAN_NAME')" <s:if test="#request.batchName==#item.get('PLAN_NAME')">selected="true"</s:if>>  
       <s:property value="#item.get('PLAN_NAME')" />
      </option>
     </s:iterator>
    </select>
   </td>
  </tr>
 </table>
</div>

最重要的是<s:if test="#request.batchName==#item.get('PLAN_NAME')">selected="true"</s:if>这句,可以通过判断让select的selected属性是否生效。

附上Action中的代码:

public String list() {
		HttpServletRequest req = getHttpRequest();
		HttpSession session = req.getSession();

		SupplyService service = getSupplyService();

		this.batchName = "";

		this.projectId = (String) session.getAttribute(ContentUtil.DIRECT_ID);
		this.iProjectId = (String) session.getAttribute(ContentUtil.PROJECT_ID);
		this.cProjectId = (String) session
				.getAttribute(ContentUtil.SUB_PROJECT_ID);

		String temp = req.getParameter("bn");
		String bn;
		if (temp != null) {
			try {
				bn = new String((temp).getBytes("ISO-8859-1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				bn = "";
				e.printStackTrace();
			}
		} else {
			bn = "";
		}

		if ("请选择".equals(bn))
			this.batchName = "";
		else
			this.batchName = bn;
		req.setAttribute("batchName", batchName);

		this.ddxList = service.getSupplyDDX(projectId, iProjectId, cProjectId,
				batchName);

		List<Map<String, Object>> batchList = new ArrayList<Map<String, Object>>();
		batchList = service.getSupBatch();

		req.setAttribute("batchList", batchList);

		/**
		 * for(Map map : ddxList){ Set<String> keySet = map.entrySet();
		 * Iterator<String> iter = keySet.iterator(); while(iter.hasNext()){
		 * String key = iter.next(); String value = (String) map.get(key);
		 * System.out.println(value); } }
		 */

		return "list";
	}



 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值