我在jsp页面上检索数组值时遇到问题,下拉选项

时间:2014-11-14 06:08:31

标签: java jsp model-view-controller foreach

我必须从不同的不同表中获取值并且值不止一个并且所有值在不同的不同下拉框中我必须在框的基础上显示,我已经做到这一点我不确定我的方式是正确的还是不仅仅是获得价值,但最终发生了什么,当我试图选择它即将来临但是像这样[saurabh,prakash,karan]并且在一行中有数组符号但我想要saurabh并且在下一行prakash所以这是我的问题,这是我的servlet类,我在其中调用empDetail方法

 @WebServlet(name = "EmployeeMapping", urlPatterns = {"/EmployeeMapping"})
 public class EmployeeMapping extends HttpServlet {
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
  HttpSession session = request.getSession(true);     
  Employee emp1 = (Employee)session.getAttribute("emp1");
  EmployeeBean eb = new EmployeeBean();
  List<Employee> leave1 = eb.empDetail(emp1); 
  if(emp1.isValid())
  {
    request.setAttribute("leave1",leave1);
    response.sendRedirect("AssignProject.jsp");
  }}
   @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  processRequest(request, response);
   }
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  processRequest(request, response);
     }
  @Override
  public String getServletInfo() {
  return "Short description";}}

这是我的方法

  public List<Employee> empDetail(Employee emp1) {
  List<Employee> leave1 = new ArrayList<Employee>();
  List<String> myList1 = new ArrayList<String>();
  List<String> myList2 = new ArrayList<String>();
  List<String> myList3 = new ArrayList<String>();
  List<String> myList4 = new ArrayList<String>();
  try{
  con = ConnectionManager.getConnection();
  stmt1 = con.createStatement();
  String Query11 = "SELECT projectname FROM addproject";
  System.out.println("Query1 is" +Query11);
  rs1 = stmt1.executeQuery(Query11);
  while(rs1.next())
  {
  String z =(rs1.getString("projectname"));
  System.out.println("...11." +z);
  myList1.add(z);
  }
   emp1.setPrjname(myList1);
   stmt2 = con.createStatement();
  String Query12 = "select empName from empinfo where access_type ='teamleader'";
  System.out.println("Query1 is" +Query12);
  rs2 = stmt2.executeQuery(Query12);
  while(rs2.next())
  {
String p = rs2.getString("empName");
System.out.println("...11." +p);
myList2.add(p);
  }
  emp1.setTlname(myList2);
  stmt3 = con.createStatement();
  String Query13 = "select empName from empinfo where access_type ='manager'";
  System.out.println("Query1 is" +Query13);
  rs3 = stmt3.executeQuery(Query13);
  while(rs3.next())
  {
  String q = rs3.getString("empName");
  System.out.println("...11." +q);
  myList3.add(q);
  }     
  emp1.setManname(myList3);
  stmt4 = con.createStatement();
   String Query14 = "select empName from empinfo where access_type ='employee'";
   System.out.println("Query1 is" +Query14);
   rs4 = stmt4.executeQuery(Query14);
   while(rs4.next())
  {
  String r = rs4.getString("empName");
  System.out.println("...11." +r);
  myList4.add(r);
  }
   emp1.setEmpname(myList4);
   leave1.add(emp1);
   emp1.setValid(true);
  }
  catch (SQLException  ex) {
  } finally {
      try {
          if (pstmt != null) {
              pstmt.close();
          }
          if (con != null) {
              con.close();
          }
      } catch (SQLException ex) {
          Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    return leave1;
    };

这是我的jsp页面,我在其中获得了像[saurabh,prakash,karan]这样的值,但如果我给$ {emp1.prjname}这个$ {emp1.prjname [0]}的内容,那么我将获得saurabh所以喜欢聪明,在这里我面临着困难,因此请任何人帮助我...

 <%if(session.getAttribute("emp1")==null)
 {
 String message ="please login first";
 request.setAttribute("message", message);
  request.getRequestDispatcher("index.jsp").forward(request, response);
 }
 %>
   <title>RESOURCE MANAGEMENT SYSTEM</title>
 <head
 </head><body>
 <%
 Employee emp1 = (Employee)session.getAttribute("emp1");
 session.setAttribute("emp1",emp1);
 %>
 <tr>
        <td >select project name </td>
         <td>   <select name="pname">    
      <option value="${emp1.prjnamename}">${emp1.prjnamename}</option></select></td>
      </tr>
         <tr>
      <td >manager </td>
         <td>   <select name="manager">
      <option value="${emp1.manname}">${emp1.manname}</option></select></td>
            </tr>
         <tr>
               <td >team leader</td>
         <td>   <select name="teamleader">
      <option value="emp1.Tlname()">emp1.Tlname()</option></select></td>
            </tr>
    <tr>
                <td> <section class="container">
    <div>
    <select id="leftValues" size="5" multiple>
   <option value="${emp1.empname}">${emp1.empname}</option>                  
  </select>
  </div>
  </td>
  <td>
  <div>
    <input type="button" id="btnLeft" value="&lt;&lt;" />
    <input type="button" id="btnRight" value="&gt;&gt;" />
  </div>
 </td>
  <td>
 <div>
    <select name="nameofemps" id="rightValues" size="4" multiple="multiple">
        <option>
        </option>
    </select>
    <div>
        <input type="text" id="txtRight" />
    </div>
  </div>
 </section>
 </td>
   </tr>            
        </table><BR>
        </fieldset>
  </body>
   </html>

2 个答案:

答案 0 :(得分:1)

您需要使用forEach属性

迭代request循环
  <select id="leftValues" size="5" multiple>
  <c:forEach var='temp' items='${leave1}' >
    <option value="${temp.empname}">${temp.empname}</option>    
  </c:forEach>              
  </select>

此外,您在此处创建了一个新请求,

  if(emp1.isValid())
  {
    request.setAttribute("leave1",leave1);
    response.sendRedirect("AssignProject.jsp");
  }}

相同的请求应该是forwarded到jsp。请改用RequestDispatcher

我假设您正在尝试从请求中打印leave1,如果您的观点不同,则在我的回答下方发表评论

答案 1 :(得分:0)

我找到了我的问题的解决方案

          <tr>
         <td >select project name </td>
          td>   <select name="pname">
         <c:forEach var="temp" items="${emp1.prjname}">
         <option value="${temp}">${temp}</option>                  
       </c:forEach></select></td></tr>

及其工作