[英]Struts2 : map which contain String and double values return null for double values
我想知道如何将包含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)
,因此我将这些参数从action传递到map<String,object>
到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()
应该返回我在地图中传递的double而不是对象!!)
编辑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
值的属性 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.