用c标签,el表达式动态取数据库中的值并显示在页面上,动态下拉列表的实现,html:select

首先jsp头部要引个东西,上次因为没引,找了好半天的错误

<%@ taglib uri="/tags/c" prefix="c" %>

 

还有有

<%@ page language="java" pageEncoding="gbk" import="java.util.List,struts.form.KK_BlacklistVehicle_UserEnrolForm"%>

 

jsp里写的

<%
      List listCode=bean.KK_BlacklistVehicle_UserEnrolSQLBean.getInstance().diffListCode();//从后台把值取出并放在list里
    request.setAttribute("listCode", listCode);
   %>
   
   <%
    //这个是测试这个list在后台有没有取到值
    //for(int i = 0; i < listCode.size(); i++) {
      //KK_BlacklistVehicle_UserEnrolForm bean = (KK_BlacklistVehicle_UserEnrolForm)listCode.get(i);
      //System.out.println("页面上输出的:======="+bean.getKk_BlacklistVehicle_UserEnrol_DW());
   //} 
   %> 
   
   
 <c:if test="${empty listCode}">//这个判断listCode的值有没有传到页面上来,没有的话会在页面上输出not get list value
  <%out.print("not get list value"); %>
 </c:if>

<html:select property="kk_BlacklistVehicle_UserEnrol_DW" >
    <html:option value="">--请选择单位--</html:option>
     <c:forEach var="listCode" items="${listCode}">//这句是遍历这个listCode,遍历一次取一次值,赋给 var="listCode"
    <html:option value="${listCode.kk_BlacklistVehicle_UserEnrol_DW}">${listCode.kk_BlacklistVehicle_UserEnrol_DW}</html:option>//显示出来遍历后的值
     </c:forEach>
   </html:select>


KK_BlacklistVehicle_UserEnrolSQLBean

public class KK_BlacklistVehicle_UserEnrolSQLBean
{
 // 声明一个数据库操作对象
 PreparedStatement pstmt = null ;
 // 声明一个结果集对象
 ResultSet rs   = null ;
 // DataBaseConnection为具体的数据库连接及关闭操作类,连接数据库
 DataBaseConnection dbc  = new DataBaseConnection() ;
 //数据库对象
 Statement sta   = null ;
  
 private static KK_BlacklistVehicle_UserEnrolSQLBean instance;
 
 public synchronized static KK_BlacklistVehicle_UserEnrolSQLBean getInstance()
 {
  if(instance==null)
  {
   instance=new KK_BlacklistVehicle_UserEnrolSQLBean();
  }
  return instance;
 }
 
 public  List diffListCode( )
 {
   DataBaseConnection dbc=new DataBaseConnection();
   Connection conn=dbc.getConnection();
   PreparedStatement pst = null;
  List<KK_BlacklistVehicle_UserEnrolForm> list=new ArrayList<KK_BlacklistVehicle_UserEnrolForm>();
  
   String sql="select distinct dw from kk.KKYH ";
   try {
    pst = conn.prepareStatement(sql);
    ResultSet rs=pst.executeQuery();
    while(rs.next())
    {
     KK_BlacklistVehicle_UserEnrolForm userEnrolForm=new KK_BlacklistVehicle_UserEnrolForm();
     userEnrolForm.setKk_BlacklistVehicle_UserEnrol_DW(rs.getString(1));
     list.add(userEnrolForm);
    }
   }catch (SQLException e) {
    e.printStackTrace();
   }finally{
    try {
     if(pst!=null)
     {
      pst.close();
     }if(conn!=null)
     {
      conn.close();
     }
    } catch (Exception e) {
    }
   }
  return list;
 }
}

 
  

 

黑色头发  http://heisetoufa.iteye.com

你可能感兴趣的:(html,C++,c,bean,C#)