从Servlet填充选择列表

时间:2014-03-25 01:19:17

标签: jsp servlets

我想从我添加到名为States的模型类的数组列表中填充选择列表。使用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;
    }
}

1 个答案:

答案 0 :(得分: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>");
 }
%>