extjs+mvc给Ext表单添加数据,然后以实体的形式返回到后台

2025-05-12 21:41:12
推荐回答(1个)
回答(1):

js代码:
Ext.define('app.site.projectWindow', {
extend: 'Ext.window.Window',
border: 0,
height:380,
width: 440,
layout: {
type: 'border'
},
title: '新建监测项目',
constrain: true,
modal: true,
initComponent: function() {
var me = this;
var row = me.row;
var required = '*';

Ext.applyIf(me, {
items: [
{
xtype: 'form',
region: 'center',
frame: true,
layout: {
type: 'column'
},
defaults: {
margin:'2 0 0 0',
columnWidth:.9
},
bodyPadding: 10,
title: '',
items: [
{
xtype: 'hiddenfield',
fieldLabel: '监测因子id',
name:'project.id',
value:row==''?'':row.get('id')
},
{
xtype: 'textfield',
fieldLabel: '名称',
name:'project.name',
afterLabelTextTpl : required,
allowBlank : false,
value:row==''?'':row.get('name')

},
{
xtype: 'textfield',
fieldLabel: '编码',
name:'project.code',
value:row==''?'':row.get('code')
},
{
xtype: 'textfield',
fieldLabel: '单位',
afterLabelTextTpl : required,
allowBlank : false,
name:'project.unit',
value:row==''?'':row.get('unit')
},
{
xtype: 'numberfield',
fieldLabel: '最大值',
afterLabelTextTpl : required,
allowBlank : false,
name:'project.maxNum',
value:row==''?'':row.get('maxNum')
},
{
xtype: 'numberfield',
fieldLabel: '最小值',
afterLabelTextTpl : required,
allowBlank : false,
name:'project.minNum',
value:row==''?'':row.get('minNum')
},
{
xtype: 'numberfield',
fieldLabel: '报警值',
afterLabelTextTpl : required,
allowBlank : false,
name:'project.alarmNum',
value:row==''?'':row.get('alarmNum')
},
{
xtype: 'numberfield',
fieldLabel: '小数位',
name:'project.decimal',
value:row==''?'':row.get('decimal')
},
{
xtype: 'textfield',
fieldLabel: '国标No',
name:'project.guobiaoNo',//对象.属性
value:row==''?'':row.get('guobiaoNo')
},
{
xtype: 'textfield',
fieldLabel: '数据Mk',
name:'project.shujuMk',,//对象.属性 value:row==''?'':row.get('shujuMk')
},
{
xtype: 'textareafield',
fieldLabel: '备注',
name:'project.mask',
value:row==''?'':row.get('mask')
}
]
}
],
buttonAlign:'center',
buttons:[{
width : 100,
text : '提交',
iconCls : 'icon-ok',
handler : function(t,e) {
t.setDisabled(true);
var form = me.down('form').getForm();
var url = 'project!savePro.action';
if(row!='')url = "project!update.action";
if (form.isValid()) {
Ext.Ajax.request({
url : url,
params : form.getValues(),
success : function(response) {
var text = Ext.JSON.decode(response.responseText);
if (text.success) {
me.grid.getStore().load();
me.close();
} else {
t.setDisabled(false);
Ext.Msg.alert('提示', '操作失败');
}
}
});
} else {
t.setDisabled(false);
Ext.MessageBox.alert('提示', '请填写正确的信息');
}
}
},{
width : 100,
text : '重置',
iconCls : 'icon-reset',
handler : function(t,e) {
me.down('form').getForm().reset();
}
},
{
width : 100,
text : '退出',
iconCls : 'icon-cancel',
handler : function(t,e) {
me.close();
}
}]
});
me.callParent(arguments);
}
});
java代码:
package com.szusst.action.site;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.szusst.entity.TBasMoniProject;
import com.szusst.service.BaseService;
public class ProjectAction implements Action{
private BaseService baseService;
private int limit;// 行数
private int start;// 开始行数
private int total;// 总数
private String key;
private List list = new ArrayList(0);
private Boolean success;
private TBasMoniProject project = new TBasMoniProject();//这个是需要set方法和get方法--关键
private String proIds;

public String execute() {
int []num = {0};
list = baseService.queryProjects(limit, start, key, num);
total = num[0];
success = true;
return SUCCESS;
}
public String savePro() {
success = baseService.savePro(project);
return SUCCESS;
}
public String delete() {
long num = baseService.deletePro(proIds);
if(num>0){
success = true;
}
return SUCCESS;
}
public String update() {
success = baseService.updatePro(project);
return SUCCESS;
}
public int getTotal() {
return total;
}
public List getList() {
return list;
}

public Boolean getSuccess() {
return success;
}
public void setBaseService(BaseService baseService) {
this.baseService = baseService;
}
public void setLimit(int limit) {
this.limit = limit;
}
public void setStart(int start) {
this.start = start;
}
public void setKey(String key) {
this.key = key;
}
@JSON(serialize=false)
public TBasMoniProject getProject() {
return project;
}
public void setProject(TBasMoniProject project) {
this.project = project;
}
public void setProIds(String proIds) {
this.proIds = proIds;
}

}
实体类:
package com.szusst.entity;
import java.util.HashSet;
import java.util.Set;

/**
* TBasMoniProject entity. @author MyEclipse Persistence Tools
*/
public class TBasMoniProject{
// Fields
private Integer id;
private TOthSiteType siteType;
private String name;
private String code;
private String unit;
private Integer decimal;
private String maxNum;
private String minNum;
private String alarmNum;
private String mask;
private String guobiaoNo;
private String shujuMk;
private Set equips = new HashSet(0);
public TBasMoniProject() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public Integer getDecimal() {
return decimal;
}
public void setDecimal(Integer decimal) {
this.decimal = decimal;
}
public TOthSiteType getSiteType() {
return siteType;
}
public void setSiteType(TOthSiteType siteType) {
this.siteType = siteType;
}
public String getMaxNum() {
return maxNum;
}
public void setMaxNum(String maxNum) {
this.maxNum = maxNum;
}
public String getMinNum() {
return minNum;
}
public void setMinNum(String minNum) {
this.minNum = minNum;
}
public String getAlarmNum() {
return alarmNum;
}
public void setAlarmNum(String alarmNum) {
this.alarmNum = alarmNum;
}
public String getMask() {
return mask;
}
public void setMask(String mask) {
this.mask = mask;
}
public String getGuobiaoNo() {
return guobiaoNo;
}
public void setGuobiaoNo(String guobiaoNo) {
this.guobiaoNo = guobiaoNo;
}
public String getShujuMk() {
return shujuMk;
}
public void setShujuMk(String shujuMk) {
this.shujuMk = shujuMk;
}
public Set getEquips() {
return equips;
}
public void setEquips(Set equips) {
this.equips = equips;
}
}
配置文件:



然后就是配置好structs.....基本上就ok 。。。。