如何在单独的文本字段中打印HasMap键和值数据?

问题描述 投票:0回答:1

我在 JSP 中使用 Struts 2 标签。下面是

Action
类。

package com.action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class EmpLeavePlan extends ActionSupport{
private String bankId;
private String empName;
private String monthYear;
private String leaves;
private Map<String,String> bankIds;
public String display() {
    return "SUCESS";
}
public String getDefaultSearchEngine() {
    return "yahoo.com";
}
public String execute() throws Exception
{
    bankIds = new HashMap<String,String>();
    bankIds.put("1541742","h6");
    bankIds.put("1541742","eft");
    bankIds.put("1394842","dfd");
    bankIds.put("1541742","dfee");
        return "SUCCESS";}
public String getBankId() {
    return bankId;
}
public Map<String, String> getBankIds() {
    return bankIds;
}
public void setBankIds(Map<String, String> bankIds) {
    this.bankIds = bankIds;
}
public void setBankId(String bankId) {
    this.bankId = bankId;
}
public String getEmpName() {
    return empName;
}
public void setEmpName(String empName) {
    this.empName = empName;
}
public String getMonthYear() {
    return monthYear;
}
public void setMonthYear(String monthYear) {
    this.monthYear = monthYear;
}
public String getLeaves() {
    return leaves;
}
public void setLeaves(String leaves) {
    this.leaves = leaves;
}}

JSP:

  <%@ taglib uri="/struts-tags" prefix="s" %>
 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
      var currentDate = new Date();
      $("#datepicker").datepicker( {
          changeMonth: true,
          changeYear: true,
          showButtonPanel: true,
          dateFormat: 'MM yy'
          /* onClose: function(dateText, inst) { 
              $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
          } */
          
      });
      $("#datepicker").datepicker("setDate", currentDate);
      
      
  });
  
   </script>
  
  <style>
.ui-datepicker-calendar {
    display: none;
   
    }
    .ui-datepicker-year{
  
    width: 70px !important;
    height: 25px;
     }
    .ui-datepicker-month{
     width: 60px !important;
    height: 25px;
    }
    .ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all{
    width: 213px !important;
    }
</style>
<h1> Leave Plan Management!!! </h1>
<body style="background-color:lightgrey;">
<s:form action="bankgen">
<s:textfield name="monthYear" label="Month/Year" id="datepicker"></s:textfield>
<%-- <s:select label="Select Bank ID" 
        headerKey="-1" headerValue="Select bank id"  list="bankIds" 
        name="bankIdd" id="bankChange" /> --%>
<s:select label="Select Bank ID" 
        headerKey="-1" headerValue="Select bank id"  list="bankIds"  
        name="bankId" id="bankChange" />    
        
<s:textfield name="empName" label="Name" id="empName"></s:textfield>
<s:textarea name="leaves" label="Leaves :" id="leaves"></s:textarea>
<s:submit value="save"></s:submit>

</s:form>
</body>

但是,我没有得到输出。在下拉列表中,仅反映值而不是键,即

bank id
。我希望银行 ID 位于
bank id
列中,键值位于
emp
名称列中。

java jquery jsp struts2 struts-tags
1个回答
0
投票

bankIds
和对应的
empName
填充在操作方法中

public String execute() throws Exception {
    bankIds = new HashMap<String,String>();
    bankIds.put("1541742","h6");
    bankIds.put("1541742","eft");
    bankIds.put("1394842","dfd");
    bankIds.put("1541742","dfee");

    //before returning to JSP you should set empName
    empName = bankIds.get(bankId);

    return "SUCCESS";
}

你没有初始化

bankId
所以第一次它会是空的。提交表单后,将填充
bankId
并初始化相应的
empName
。这两个值都将在 Struts 标签中预先选择。由于您没有使用
value
属性,因此将使用
name
属性评估该值。

您可以使用两种操作方法:一种用于使用

GET
http 方法呈现 JSP,另一种用于使用
POST
http 方法提交表单。当
bankId
发布时,
bankIds
地图应该已经填充。所以你应该使用另一种方法
prepare()
来填充地图
bankIds
。要在执行操作并返回结果 JSP 之前自动调用,操作类应该实现
Preparable
接口。

public class EmpLeavePlan extends ActionSupport implements Preparable {
    ...
    public void prepare() {
        bankIds = new HashMap<String,String>();
        bankIds.put("1541742","h6");
        bankIds.put("1541742","eft");
        bankIds.put("1394842","dfd");
        bankIds.put("1541742","dfee");
    }

    public String executeGet() throws Exception {

        //before returning to JSP you should set empName
        empName = bankIds.get(bankId);

        return "SUCCESS";
    }

    public String executePost() throws Exception {

        //before returning to JSP you should set empName
        empName = bankIds.get(bankId);

        return "SUCCESS";
    }
    ...
}

如果您想更新

empName
当您使用 javascript 从下拉列表中动态选择时

<s:select label="Select Bank ID" 
        headerKey="-1" headerValue="Select bank id"  list="bankIds"  
        name="bankId" id="bankChange" />    

<s:textfield name="empName" label="Name" id="empName" />
<script>
$(function(){
  $("#bankChange").change(function(){
    $("#empName").val($(this).val());
  });
});
</script>

© www.soinside.com 2019 - 2024. All rights reserved.