从数据库取数据,如何实现下拉框效果。
我有一张表s_model,
字段: MODEL_NAME MODEL_DESCRIBE TYPE_ID REMARK
数据类型: VARCHAR2 VARCHAR2 VARCHAR2 VARCHAR2
在web页面中如何显示成下拉框效果,显示值为model_describe字段值的值,请教了,谢谢各位
我有一张表s_model,
字段: MODEL_NAME MODEL_DESCRIBE TYPE_ID REMARK
数据类型: VARCHAR2 VARCHAR2 VARCHAR2 VARCHAR2
在web页面中如何显示成下拉框效果,显示值为model_describe字段值的值,请教了,谢谢各位
<html:select property="model">
<html:options property="models" />
</html:select>FormBean:
private String model;
private String[] models; //models为集合类型也可以
//get set方法Action:
调用业务接口(数据层ibatis完成数据查询操作,业务层调数据层),获得model_describe数据,然后设置(FormBean的Set方法)到models中,转到页面就OK了!
应该可以我以前是这样做的
public List list() throws SQLException {
// TODO Auto-generated method stub
List list = null;
try {
this.startTransaction();
PunishmentModel pm=new PunishmentModel();
this.getSqlMap().executeQueryForObject("getPunishmentByModel",pm);
Iterator it=null;
while(it.hasNext())
{
System.out.println(pm.getModelName());
System.out.println(pm.getModelDescribe());
}
list = this.getSqlMap().executeQueryForList(
"getPunishmentByModel",null);
for(int i=0;i<=list.size();i++)
{
System.out.println(list.get(i));
}
this.commit();
} catch (Exception e) {
// TODO: Auto-generated catch block
e.printStackTrace();
this.rollback();
}
return list;
}action 写法:
/查找出model表信息
public String onListPunishmentModel(ActionEvent e)throws SQLException{
//PunishmentAmtAction devSample1=new PunishmentAmtAction();
//PunishmentModelForm devSampleModelForm=(PunishmentModelForm)e.getFormBean();
//BeanUtils.populate(devSampleModelForm,devSample1);
PunishmentModelDao pmd=new PunishmentModelDao();
List dataList=pmd.list();
e.getRequest().setAttribute("dataList",dataList);
return IConstants.FORWARD_SUCCESS_KEY;
jsp写法:
<%@ page contentType="text/html; charset=GBK"%>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib uri="displaytag" prefix="display" %>
<%@ page import="java.util.*"%>
<%@ page import="com.peak.riskscan.punishment.model.PunishmentModel"%>
<%
List list=(List)request.getAttribute("dataList");
PunishmentModel pm=null;
// String action ="addPunishmentAmt.do";
%><script language=javascript src="../../page/javascript/openWin.js"></script>
<script language=javascript src="/RiskScan/page/javascript/popcalendar.js"></script>
<link rel="stylesheet" href="<%=request.getContextPath()%>/page/css/screen.css" type="text/css"><p class="title" align="center">添加处罚金</p>
<form method="post" name="QueryForm" action="<%=request.getContextPath()%>/punishment/addPunishmentAmt.do" >
<table class='simpleform' width="100%">
<tr>
<td>复核差错与风险预警名:</td>
<td><input type="text" name="modelName"></td>
</tr>
<tr>
<td>处罚金额:</td>
<td><input type="text" name="punishmentAmt"></td>
<td>
<select id="modelName">
<%
for(int i=0,j=list.size();i<j;i++)
{
pm=(PunishmentModel)list.get(i);
%>
<option value="<%=pm.getModelName()%>"><%=pm.getModelDescribe()%></option>
<%} %>
</select>
运行jsp文件,显示空指针异常,为什么??
先确认下你的list的size是否为0,如果list里有值的话,继续去看你的传值方式。