2014-03-25 76 views
0

我想從我添加到名爲States的模型類的數組列表中填充選擇列表。將Servlet用作控制器我需要在表單的選擇列表中填充狀態。從Servlet填充選擇列表

這可能嗎?

下面是我有這麼遠

形式的代碼:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
    <%@ page import="java.util.ArrayList" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Customer Management</title> 
</head> 
<body> 
    <form action="/customerManagment" method="post"> 
     First Name:<br> 
     <input type="text" name="firstName"/><br> 
     Last Name:<br> 
     <input type="text" name="lastName"/><br> 
     Email:<br> 
     <input type="text" name="email"/><br> 
     Phone:<br> 
     <input type="text" name="phone"/><br> 
     Phone Type:<br> 

     Street Address:<br> 
     <input type="text" name="streetAddress"/><br> 
     Apartment Number:<br> 
     <input type="text" name="apartmentNumber"/><br> 
     City:<br> 
     <input type="text" name="city"/><br> 
     State:<br> 
     <select> 
      <option><% 
      ArrayList<edu.witc.Assignment03.model.States> states = (java.util.ArrayList)request.getAttribute("states"); 
       for (edu.witc.Assignment03.model.States state : states) { 
        state.getStates(); 
       }%></option> 
     </select><br> 

     <input type="submit" value="submit"> 
     </form> 

的Servlet:

import java.util.List;  
import javax.servlet.*; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
//import javax.servlet.annotation.WebServlet; 
//import javax.servlet.http.HttpServlet; 
//import javax.servlet.http.HttpServletRequest; 
//import javax.servlet.http.HttpServletResponse; 




import edu.witc.Assignment03.model.Customer; 
import edu.witc.Assignment03.model.Phone; 
import edu.witc.Assignment03.model.States; 

/* 
* Not thread-safe. For illustration purpose only 
*/ 
@WebServlet(name = "CustomerServlet", urlPatterns = { 
     "/customerManagement"}) 
public class CustomerServlet extends HttpServlet { 
    private static final long serialVersionUID = -20L; 

    private List<edu.witc.Assignment03.model.States> states = new ArrayList<States>(); 
    private List<edu.witc.Assignment03.model.Customer> customers = new ArrayList<Customer>(); 

    public void init() throws ServletException { 
     States state = new States(); 
     states.add(state); 

    } 

private void addCustomer(HttpServletResponse response, HttpServletRequest request)//redirect to index 
      throws IOException, ServletException { 
    String url = "/customerManagement.jsp"; 
     request.setAttribute("customers", customers); 
     request.getRequestDispatcher(url).forward(request,response); 
    } 

    private void editCustomer(HttpServletResponse response, HttpServletRequest request)//redirect to index 
      throws IOException, ServletException { 
     String url = "/customerManagement.jsp"; 
     request.setAttribute("customers", customers); 
    request.getRequestDispatcher(url).forward(request,response); 
    } 

    private void sendCustomerList(HttpServletResponse response, HttpServletRequest request)//redirect to index 
      throws IOException, ServletException { 
     String url = "/index.jsp"; 
     request.setAttribute("customers", customers); 
     request.getRequestDispatcher(url).forward(request,response);           
    } 

    private Customer getCustomer(int customerId) { 
     for (Customer customer : customers) { 
      if (customer.getCustomerId() == customerId) { 
       return customer; 
      } 
     } 
     return null; 
    } 

    private void sendEditCustomerForm(HttpServletRequest request, 
      HttpServletResponse response) throws IOException, ServletException { 

     String url = "/customerManagement.jsp"; 
     request.setAttribute("customers", customers); 
     request.getRequestDispatcher(url).forward(request,response); 
    } 


    public void doGet(HttpServletRequest request, 
      HttpServletResponse response) 
      throws ServletException, IOException { 
     String uri = request.getRequestURI(); 
     if (uri.endsWith("/customer")) { 
      sendCustomerList(response, request); 
     } else if (uri.endsWith("/editCustomer")) { 
      sendEditCustomerForm(request, response); 
     }   
    } 

    public void doPost(HttpServletRequest request, 
      HttpServletResponse response) 
      throws ServletException, IOException { 
     // update customer 
     int customerId = 0; 
     try { 
      customerId = 
        Integer.parseInt(request.getParameter("id")); 
     } catch (NumberFormatException e) { 
     } 
     Customer customer = getCustomer(customerId); 
     if (customer != null) { 
      customer.setFirstName(request.getParameter("firstName")); 
      customer.setLastName(request.getParameter("lastName")); 
      customer.setEmail(request.getParameter("email")); 
      customer.setPhone(request.getParameter("phone")); 
      customer.setAddress(request.getParameter("address")); 
      customer.setCity(request.getParameter("city")); 
      customer.setState(request.getParameter("states")); 
      customer.setZip(request.getParameter("zip")); 
     } 
     addCustomer(response, request); 
    } 
} 

型號:

package edu.witc.Assignment03.model; 

import java.util.ArrayList; 
import java.util.List; 

public class States { 

    private List<String> state = new ArrayList<>();{ 

    state.add("Alabama"); 
    state.add("Alaska"); 
    state.add("Arizona"); 
    state.add("Arkansas"); 
    state.add("California"); 
    state.add("Colorado"); 
    state.add("Connecticut"); 
    state.add("Delaware"); 
    state.add("Florida"); 
    state.add("Georgia"); 
    state.add("Hawaii"); 
    state.add("Idaho"); 
    state.add("Illinois"); 
    state.add("Indiana"); 
    state.add("Iowa"); 
    state.add("Kansas"); 
    state.add("Kentucky"); 
    state.add("Louisiana"); 
    state.add("Maine"); 
    state.add("Maryland"); 
    state.add("Massachusetts"); 
    state.add("Michigan"); 
    state.add("Minnesota"); 
    state.add("Mississippi"); 
    state.add("Missouri"); 
    state.add("Montana"); 
    state.add("Nebraska"); 
    state.add("Nevada"); 
    state.add("New Hampshire"); 
    state.add("New Jersey"); 
    state.add("New Mexico"); 
    state.add("New York"); 
    state.add("North Carolina"); 
    state.add("North Dakota"); 
    state.add("Ohio"); 
    state.add("Oklahoma"); 
    state.add("Oregon"); 
    state.add("Pennsylvania"); 
    state.add("Rhode Island"); 
    state.add("South Carolina"); 
    state.add("South Dakota"); 
    state.add("Tennessee"); 
    state.add("Texas"); 
    state.add("Utah"); 
    state.add("Vermont"); 
    state.add("Virginia"); 
    state.add("Washington"); 
    state.add("West Virginia"); 
    state.add("Wisconsin"); 
    state.add("Wyoming"); 
    } 

    public List<String> getStates(){ 
     return this.state; 
    } 
} 
+0

你想知道如何寫一個'for'循環? –

+0

不是。我試圖讓選擇列表填充在所有表單上的選項列表中。之後,我只需要選擇應該很容易的項目。 – user3457789

+0

你有什麼錯誤? – Rembo

回答

1

在你的servlet:

request.setAttribute("states", state.getStates()); 

在你的JSP,基本不變:

<option> 
<% 
ArrayList<edu.witc.Assignment03.model.States> states = (java.util.ArrayList)request.getAttribute("states"); 
for (edu.witc.Assignment03.model.States state : states) 
{ 
state.getStates(); 
} 
%> 
</option> 

到:

<% 
ArrayList<String> states = (java.util.ArrayList)request.getAttribute("states"); 
for (String state : states) 
{ 
out.print("<option value='"+state+"'>"+state+"</option>"); 
} 
%> 
+0

由於某些原因,當我將你所說的放在servlet中時,它會一直拋出一個錯誤 – user3457789

+0

因爲我沒有註釋。我剛剛使用您的實際代碼更新了它。 –

+0

小心類型。 States.getStates()返回的ArrayList屬於不屬於狀態的字符串。 –