【问题标题】:How to print the HasMap key and value data in separate text fields?如何在单独的文本字段中打印 HashMap 键和值数据?
【发布时间】:2016-06-10 13:25:27
【问题描述】:

我在 JSP 中使用 Struts2 标记。下面是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 名称列中。

【问题讨论】:

  • 我用“localhost:8080/Demostruts/selectAction.action”来初始化url,而不是直接调用jsp。我需要在下拉列表中生成银行 ID,并在另一个字段中生成相应的 empName。所以,我使用了hashmap。在struts html标签中有“list”属性..list键和列表值。但我不太确定如何在下面的字段中打印相应的 empName
  • 您将打印empName,但它尚未初始化。它不会被打印出来。
  • 你能解释一下吗..
  • 不清楚I want the bank ids to be in the bank id column and the key values to be in the emp name column。请澄清您的具体问题或添加其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。请参阅“如何提问”页面以获得澄清此问题的帮助。
  • 银行 ID 应显示在下拉列表中 @roman C 。并且相应的银行名称应自动填写在 empName 字段中。这是预期的输出。

标签: java jquery jsp struts2 struts-tags


【解决方案1】:

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";
    }
    ...
}

如果您想在使用 javascript 从下拉列表中动态选择时更新 empName

<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>

【讨论】:

  • 罗曼,感谢您的指正。我从来没有遇到过prepare方法和executeget或executepost,只是execute()。你能告诉我struts.xml 的样子吗?
  • /index.jsp/success.jsp
  • 以上是我当前的struts.xml
  • 不要在 cmets 中发布代码,而是编辑您的问题并将其添加到那里。
猜你喜欢
  • 1970-01-01
  • 2023-01-02
  • 1970-01-01
  • 2012-02-13
  • 2022-01-09
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多