请教关于extjs与struts2互相传递数据问题!
各位好。我的项目前台用的是extjs,后台是struts2+Spring+mybatis。我在界面上有个查询按钮,一点查询会将条件传递给后台,后台根据条件从数据库中找到相应的数据,并返回给前台。
下边是我的form
- JScript code
function LoadForm(baseUrl) { var fpProductSearch = new Ext.FormPanel({ renderTo: 'searchDiv', id:"fpProductSearch", labelWidth: 75, frame:true, title: '产品搜索', bodyStyle:'padding:5px 5px 0', width: 350, defaults: {width: 230}, defaultType: 'textfield', items: [{ fieldLabel: '产品名称', emptyText:'B-5538(YAS023400)', name: 'product.productname' }], buttons: [{ id:"btnSearch", text: '查找', handler: function(){ var baseParams = fpProductSearch.form.getValues(); fpProduct.getForm().reset(); Ext.Ajax.request({ url : baseUrl + '/productAction!SearchProduct', method:"post", params: baseParams, success: function (response,options) { obj = Ext.decode(response.responseText); if(obj.success) { fpProduct.getForm().setValues(obj.message); }else { Ext.Msg.alert('错误', obj.message); } }, failure: function(response,options) { Ext.Msg.alert('错误', response.statusText); } }); } },{ id:"btnSReset", text: '清空', handler: function(){ fpProductSearch.getForm().reset(); } }] });var fpProduct = new Ext.FormPanel({ renderTo: 'resultDiv', labelAlign: 'top', frame:true, title: '产品信息编辑', bodyStyle:'padding:5px 5px 0', width: 960, items: [{ layout:'column', items:[{ columnWidth:.25, layout: 'form', items: [{ xtype:'textfield', fieldLabel: '产品名称', name: 'productname', anchor:'95%' }, { xtype:'textfield', fieldLabel: '创建人', name: 'creator', anchor:'95%' } , { xtype:'textfield', fieldLabel: '客户', name: 'customeruid', anchor:'95%' } , { xtype:'textfield', fieldLabel: '拼板数量', name: 'panelnum', anchor:'95%' } , { xtype:'textfield', fieldLabel: '备注', name: 'description', anchor:'95%' }] },{ columnWidth:.25, layout: 'form', items: [{ xtype:'textfield', fieldLabel: '物料号', name: 'productpartnumber', anchor:'95%' },{ xtype:'textfield', fieldLabel: '创建时间', name: 'createDateStr', anchor:'95%' }, { xtype:'textfield', fieldLabel: '位号图', name: 'drawuid', anchor:'95%' }, { xtype:'textfield', fieldLabel: 'B面点数', name: 'bottomplaneqty', anchor:'95%' }, { xtype:'combo', fieldLabel: '状态', store:productStatusStore, name: 'productstatus', hiddenName:'productstatus', mode : 'local', valueField: 'accountType', displayField: 'accountTypeName', triggerAction: "all", editable:false, anchor:'95%' }] } ,{ columnWidth:.25, layout: 'form', items: [{ xtype:'textfield', fieldLabel: '版本', name: 'productversion', anchor:'95%' },{ xtype:'textfield', fieldLabel: '最后修改人', name: 'modifier', anchor:'95%' }, { xtype:'textfield', fieldLabel: '工艺', name: 'proroutinguid', anchor:'95%' }, { xtype:'textfield', fieldLabel: 'T面点数', name: 'topplaneqty', anchor:'95%' }] } ,{ columnWidth:.25, layout: 'form', items: [{ xtype:'textfield', fieldLabel: '产品型号', name: 'productcode', anchor:'95%' },{ xtype:'textfield', fieldLabel: '最后修改时间', name: 'updateDateStr', anchor:'95%' }, { xtype:'textfield', fieldLabel: 'BOM', name: 'bomuid', anchor:'95%' }, { xtype:'textfield', fieldLabel: '变更号', name: 'ecn', anchor:'95%' }] }] }], buttons: [{ text: '提交', handler: function(){ var pros=fpProduct.form.getValues(); Ext.Ajax.request({ url : baseUrl + '/productAction!UpdateProduct', params: pros, success: function (response, option) { obj = Ext.decode(response.responseText); fpProduct.getForm().setValues(obj); }}) } },{ text: '清空', handler: function(){ fpProduct.getForm().reset(); } }] }); }
我绑定的是候,用的是name: 'productname',这个字段,现在的问题,后台返回的数据,我的前台能显示出来,但是当我点击提交按钮的进候,后台接收不到前台发过去的数据?我用抓包工单看了一下,发现这样提交的过去的数据是不含类名的,也就是productname='abc',我看了别人的代码,将extjs的字段写这样name: 'product.productname',后台就可以收到数据了,但是前台因为加了一个类名,后台发送的过来的数据就显示不出来了。我前台是这样赋值的:fpProduct.getForm().setValues(obj.message); 请问有谁知道这种情况怎么处理啊?谢谢了。
下边是后台action
- Java code
package dmes.action;import java.util.HashMap;import java.util.Map;import javax.annotation.Resource;import net.sf.json.JSONObject;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import dmes.po.Product;import dmes.service.ProductService;/** * @author James.Lu * @date:2012-5-23 下午09:30:57 * @version : 0.1 * */public class ProductAction extends BaseAction { /** * */ private static final long serialVersionUID = -3370426361991359641L; private Product product = null; @Resource private ProductService productService =null; public ProductAction() { dataMap = new HashMap<String, Object>(); } public String SearchProduct() { dataMap.clear(); if(product==null) { dataMap.put("message","产品名称不能为空!"); dataMap.put("success", false); } Product result = productService.SearchProduct(product); if(result!=null) { dataMap.put("message",result); dataMap.put("success", true); } else { dataMap.put("message","没有找到相关产品信息!"); dataMap.put("success", false); } return SUCCESS; } public String UpdateProduct() { dataMap.clear(); Map objMap = getRequest().getParameterMap(); JSONObject json = JSONObject.fromObject(objMap); if(product==null) { dataMap.put("message","产品名称不能为空!"); dataMap.put("success", false); } Product result = productService.SearchProduct(product); if(result!=null) { dataMap.put("message",result); dataMap.put("success", true); } else { dataMap.put("message","没有找到相关产品信息!"); dataMap.put("success", false); } return SUCCESS; } /** * @param product the product to set */ public void setProduct(Product product) { this.product = product; } /** * @return the product */ public Product getProduct() { return product; } /** * @param productService the productService to set */ public void setProductService(ProductService productService) { this.productService = productService; } /** * @return the productService */ public ProductService getProductService() { return productService; }}
[解决办法]
你在FIREFOX中的firebug中看看提交的参数是什么,和ACTION中的参数是不是一致
[解决办法]
你可以把值这样赋给带类名的参数
var formData = Ext.util.JSON.decode(form.responseText);
fpanel.getCmpByName("questionReportDetail.questionAnalysis").setValue(this.questionAnalysis);
fpanel.getCmpByName("questionReportDetail.modifyAdvice").setValue(this.modifyAdvice);