包含String和double值的Struts2:map对于double值返回null

Struts2 : map which contain String and double values return null for double values

本文关键字:double 对于 返回 null map Struts2 String 值的 包含      更新时间:2023-09-26

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

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

操作:

 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:

$.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),因此我将这些参数从action传递到jsp页面的map<String,object>中。

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

kilo字段(double(在编辑后有一个null值,但如果我用Integer值编辑它,它就不会有null值,

我认为是因为public double getkilo()应该返回一个double-not对象,而不是我在映射中传递的对象!!(

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

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

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

现在您必须将对象放入其中,如果值为null,它将保留success处理程序返回JSON结果后可以获得的值。另请参阅如何从JSON结果中排除具有null值的属性。