使用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";
}