【发布时间】: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<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() 应该返回我在地图中传递的双非对象!!)
编辑 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