【问题标题】:Struts2 : map which contain String and double values return null for double valuesStruts2:包含字符串和双精度值的映射对于双精度值返回 null
【发布时间】:2014-03-24 15:12:04
【问题描述】:

我想知道如何将包含 String 和 double 值的 HashMap 从 Action 传递到 javascript 代码?

当我编辑一些具有双值的字段时,我在这些字段上有一个空值,我该如何解决?

行动:

 public String query() {
    Service = new ServiceImpl();
    v = new Vehicule();
    v= Service.getByImmat(im);
    map.put("kilo", v.getKilo()); //double value
    /*SimpleDateFormat formatter5=new SimpleDateFormat("yyyy-MM-dd");
    String formats1 = formatter5.format(ss1);*/
    map.put("datepicker", formats1);
    map.put("etat", v.getEtat());//string value
    map.put("capacite_bagages", v.getCapacite_bagage()); //double value
    return "success";
}

阿贾克斯:

$.ajax({
    type: 'POST', 
    url  : "<s:url action='query'/>",
    dataType : 'json',
    data: { imm : selectedValue},
    success: function(result) {
        $.each(result, function(key,value){ 
            $("#"+key).val(value); 
        });
    });

编辑:

我想更新一些值 (string,Date and double) ,因此我将这些参数从操作传递到 map&lt;String,object&gt; 到 jsp 页面。

public String query() {
        Service = new ServiceImpl();
        v = new Vehicule();
        v= Service.getByImmat(im);
        map.put("kilo", v.getKilo()); //double value
        SimpleDateFormat formatter5=new SimpleDateFormat("yyyy-MM-dd");
        String formats1 = formatter5.format(v.getDate());
        map.put("datepicker", formats1);
        map.put("etat", v.getEtat());//string value
        map.put("capacite_bagages", v.getCapacite_bagage()); //double value
        return "success";
    }

我在我的jsp中显示它,我在我的jsp页面中编辑它,但是当我想保存结果时,双值取一个NAN! :

public String modifiervehicule () {

        Service = new ServiceImpl();
        v= new Vehicule();
        v = vehiculeService.getVehiculeByImmat(immatricul);
        v.setKilo((Double)getKilo());
        v.setDate((Date)getDate());
        v.setEtat(getEtat());
        v.setCapacite_bagage((Double)getCapacite_bagages());

        v = Service.update(v);
        return "success";

    }

千斤域(双精度)编辑后有空值,但如果我用整数值编辑它就不会有空值,

我认为因为public double getkilo() 应该返回我在地图中传递的双非对象!!)

编辑 2:

jsp:

<script type="text/javascript">
$(document).ready(function() 
    {

      $('#imm').change(function()
        {


          var selectedValue = $('#imm').val();
            if ($.trim(selectedValue).length > 0) 
             {
                $.ajax(
                {
                    type: 'POST', 
                    url  : "<s:url action='query'/>",
                    dataType : 'json',
                    data: { imm : selectedValue},
                    success: function(result){                                                                                     
                        $.each(result, function(key,value)
                        {                    


                                $("#"+key).val(value);

                                                            } );                                
    });
</script>

<s:form  cssStyle="border:0;" validate="true" action="modifiervehicule" namespace="/" >
<s:select  id="imm" label="immatriculation" list="immatriculs" name="immatricul"></s:select>
<s:textfield id="kilo"  name="kil" label="kilometrage" size="15" ></s:textfield>
<s:textfield  id="etat" name="etat" label="etat" size="15" ></s:textfield>
 <s:textfield  id="datepicker" name="date" label="date"  size="15" ></s:textfield> 
<s:textfield id="capacite_bagages" name="capacite_bagages" label="capacité de bagages" size="15"></s:textfield>

<s:submit  style="height:30px; width:125px" name="Valider" value="Valider"></s:submit>
</s:form>

Struts.xml

<action name="query" class="action.Gestion" method="query">
    <result name="success" type="json" >
        <param name="root">map</param>

    </result>

</action>

【问题讨论】:

  • 您的代码缺少一些部分(Service = 等)。什么是地图?双变量有哪个值?页面上打印了什么?
  • 私人服务服务; // 使用休眠私有 Map 连接到数据库的服务 map= new HashMap();

标签: java javascript jquery ajax struts2


【解决方案1】:

使map 映射对象而不是字符串。例如

Map<String, Object> map= new HashMap<>();

现在您必须将对象放入其中,如果值为null,它将保留您在success处理程序返回JSON结果后可以获得的值。也可以看看 how to exclude properties with null values from the JSON result

【讨论】:

  • 我还有一个空值!
  • 我想更新一些值(字符串、整数和双精度),因此我将这些参数从操作传递到 map 到 jsp 页面。我在我的 jsp 中显示它,我对其进行了编辑,但是当我想保存结果时,所有双精度值都采用 NAN !!
  • 我不明白你在说什么,你能更新一下你的问题吗?
  • 我不知道动作是如何映射到 JSP 的,所以发布 struts.xml。还要用缩进格式化代码。
  • 我发布了 struts.xml
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
  • 2022-10-01
  • 2017-02-23
  • 1970-01-01
相关资源
最近更新 更多