将地图数据从servlet传递到jsp选择标记

将地图数据从servlet传递到jsp选择标记,jsp,servlets,jstl,Jsp,Servlets,Jstl,我看不出下面的代码有什么问题,它没有将任何数据从映射传递到jsp选择标记选项 这是我的模型课: @Entity @Table(name="customer") public class Customer implements Serializable { @Id @SequenceGenerator(name = "my_seq", sequenceName = "seq1", allocationSize = 1) @GeneratedValue(strategy = GenerationT

我看不出下面的代码有什么问题,它没有将任何数据从映射传递到jsp选择标记选项

这是我的模型课:

@Entity
@Table(name="customer")
public class Customer implements Serializable {

@Id
@SequenceGenerator(name = "my_seq", sequenceName = "seq1", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
public int id;
public String customerType;

@ElementCollection
@Column(name = "customerType")
public Map<String, String> customerTypes;


public Customer() {}


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}


  public void setCustomerTypes(Map<String, String> customerTypes) {

  this.customerTypes = customerTypes;

  }

  public Map<String, String> getCustomerTypes() {

      return customerTypes;
   }


public String getCustomerType() {
    return customerType;
}


public void setCustomerType(String customerType) {



    this.customerType = customerType;
}


}
@实体
@表(name=“客户”)
公共类Customer实现了可序列化{
@身份证
@SequenceGenerator(name=“my_seq”,sequenceName=“seq1”,allocationSize=1)
@GeneratedValue(策略=GenerationType.SEQUENCE,generator=“my_seq”)
公共int id;
公共字符串customerType;
@元素集合
@列(name=“customerType”)
公共地图客户类型;
公共客户(){}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共无效集合customerTypes(映射customerTypes){
this.customerTypes=customerTypes;
}
公共地图getCustomerTypes(){
返回客户类型;
}
公共字符串getCustomerType(){
返回客户类型;
}
public void setCustomerType(字符串customerType){
this.customerType=customerType;
}
}
Servlet,这里我在映射中添加选择选项,并将它们传递到jsp页面

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    showForm(request, response);

}
private void showForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    Map<String, String> map = new LinkedHashMap<String, String>();

       map.put("", " ");
       map.put("customerType.private", "Private");
       map.put("customerType.corporate", "Corporate");

    Customer customer = new Customer();

    customer.setCustomerTypes(map);
    request.setAttribute("customerType", customer);

    request.getRequestDispatcher("/Add.jsp").forward(request, response);    
}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
展示形式(请求、响应);
}
私有void showForm(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException
{
Map Map=newlinkedhashmap();
地图。放置(“,”);
map.put(“customerType.private”、“private”);
map.put(“customerType.corporate”、“corporate”);
客户=新客户();
customer.setCustomerTypes(地图);
setAttribute(“customerType”,customer);
request.getRequestDispatcher(“/Add.jsp”).forward(请求,响应);
}
以及jsp部分:

  <select id="customerTypeSelect" name="customerType" >

   <c:forEach var="entry" items="${customer.customerTypes}">

    <c:set var="selected" value="" scope="request"/>

      <c:if test="${entry.key == customer.customerType}">
      <c:set var="selected" value="selected=\"selected\"" scope="request"/>
    </c:if>
    <option value="${entry.key}" ${selected}>${entry.value}</option>
  </c:forEach>

 </select>

${entry.value}

尝试按以下方式更改代码

 request.setAttribute("customerType", customer);
您正在如下设置
customer
对象

 request.setAttribute("customerType", customer);
这里的键是
customerType
,您已经使用了
customer

1.)因此用户
customerType
作为jsp中的键,如下所示

<select id="customerTypeSelect" name="customerType" >

   <c:forEach var="entry" items="${customerType.customerTypes}">

    <c:set var="selected" value="" scope="request"/>

      <c:if test="${entry.key == customerType.customerType}">
      <c:set var="selected" value="selected=\"selected\"" scope="request"/>
    </c:if>
    <option value="${entry.key}" ${selected}>${entry.value}</option>
  </c:forEach>

 </select>

这里
items=“${customer.customerTypes}”
(在
中),什么是
customer.customerTypes
?它应该是模型类中地图的参考,绝对感谢!!非常感谢。我花了一个小时寻找这个愚蠢的错误,而你几分钟就发现了。再次感谢。@JuuriPeeter如果有效的话,别忘了投票/接受答案,我不会反对ankhur singhal。