JSP下拉列表转换为JSLT下拉列表

时间:2014-07-08 06:30:40

标签: jsp servlets jstl

如何将JSP下拉列表转换为JSLT下拉列表以供选择?

NewEventResource servlet

 private String EVENT_ID_LIST = "eventAdd";
 private String JOB_ID_LIST = "jobAdd";    
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    List<EventResource> eventAdd;
    List<EventResource> jobAdd;
    EventResourceDAO evdao = new EventResourceDAO();
    try {
        /* TODO output your page here. You may use following sample code. */
        eventAdd = evdao.eventNameOption();// data list from database
        jobAdd = evdao.optionJobName();//data list from database
        String destination = "/new-event-resource.jsp";
        request.setAttribute(EVENT_ID_LIST, eventAdd);
        request.setAttribute(JOB_ID_LIST, jobAdd);

        RequestDispatcher rd = request.getRequestDispatcher(destination);
        rd.forward(request, response);

    }`

Mydatabase类

    public List<EventResource> eventNameOption(){    
    List<EventResource> list = new ArrayList<EventResource>();
    PreparedStatement psmt = null;
    try {            
        conn = ConnectionManager.getConnection();
        psmt = conn.prepareStatement("SELECT event_id FROM event_details");
        rs = psmt.executeQuery();            
        while(rs.next()){
            EventResource erv = new EventResource();
            erv.setEventId(rs.getString(1));
            list.add(erv);
        }            
    } catch(SQLException ex) {
        Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if( conn != null ) {
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            conn = null;
        } 
        if( psmt != null ) {
            try {
                psmt.close();
            } catch (SQLException ex) {
                Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            psmt = null;
        }
        if( rs != null ) {
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            rs = null;
        }
    }
    return list;
}

public List<EventResource> optionJobName(){       

    List<EventResource> ls = new ArrayList<EventResource>();
    PreparedStatement psmt = null;

    try {

        conn = ConnectionManager.getConnection();
        psmt = conn.prepareStatement("SELECT job_id FROM humen_resources");
        rs = psmt.executeQuery();

        while(rs.next()){
            EventResource lss = new EventResource();
            lss.setJobId(rs.getString(1));
            ls.add(lss);
        }

    } catch (SQLException ex) {
        Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if( conn != null ) {
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            conn = null;
        } 
        if( psmt != null ) {
            try {
                psmt.close();
            } catch (SQLException ex) {
                Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            psmt = null;
        }
        if( rs != null ) {
            try {
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(EventResourceDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            rs = null;
        }
    }

    return ls;
}

我的数据库类

public class EventResource {        
    private String eventId;
    private String jobId;
    private String date;
    private String time;
    private String teamSize;    
    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getTeamSize() {
        return teamSize;
    }

    public void setTeamSize(String teamSize) {
        this.teamSize = teamSize;
    }

}

Jsp文件

<pre><form action="AddEventResources" method="post" ></pre>


               <pre> <label></pre>
                  <pre>  <span>Event Name :</span></pre>

                 <pre>   <select name="ename"  /> </pre>

                    <%

                        if (request.getAttribute("eventAdd") != null) {
                            List<EventResource> eventAdd = (List<EventResource>) request.getAttribute("eventAdd");
                    %>
                    <%
                        if (eventAdd != null) {

                            for (EventResource event : eventAdd) {
                    %>

                    <option value="<%= event.getEventId() %>"> <%= event.getEventId() %> </option>
                    <% }
                            }

                        }%>
                  <pre>  </select></pre>

                 <pre> </label></pre>

             <pre>   <label></pre>
                <pre>    <span>Job Name :</span></pre>
                 <pre>   <select name="jname"></pre>
                    <%

                        if (request.getAttribute("jobAdd") != null) {
                            List<EventResource> jobAdd = (List<EventResource>) request.getAttribute("jobAdd");
                    %>
                    <%
                        if (jobAdd != null) {
                            for (EventResource job : jobAdd) {
                    %>
                    <option value="<%= job.getJobId() %>"> <%= job.getJobId()%> </option>
                    <% }
                            }

                        }%>
                 <pre>   </select></pre>
              <pre>  </label></pre>
<pre></form></pre>

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

使用jstl非常简单,只需迭代select标签内的列表

首先,您需要为jstl supprt添加此行,

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

然后用jstl标签替换你的scriptlet

<select name="ename">
<c:if test="${empty EVENT_ID_LIST}">
 <c:forEach var="tempVariable" items="${EVENT_ID_LIST}">
   <option>${tempVariable.getJobId}</option>
 </c:forEach>
</c:if>

如果您遇到任何问题,请告诉我。 希望这会有所帮助!!