【问题标题】:JSP Drop Down list convert to JSLT Drop down listJSP 下拉列表转换为 JSLT 下拉列表
【发布时间】:2014-07-08 06:30:40
【问题描述】:

如何将 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);

    }`

我的数据库类

    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>

非常感谢任何帮助

【问题讨论】:

    标签: jsp servlets jstl


    【解决方案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>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多