这是页面的extjs
//显示用户信息
Ext.QuickTips.init();
var cm = new Ext.grid.ColumnModel([
{header: '用户账号', dataIndex: 'id'},
{header: '用户姓名', dataIndex: 'username'},
{header: '用户密码', dataIndex: 'password'},
{header: '年龄', dataIndex: 'age'},
{header: '生日', dataIndex: 'birthday'},
{header: '地址', dataIndex: 'address'},
{header: 'resume', dataIndex: 'resume'}
]);
var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url:'listUsersAction'}),
reader: new Ext.data.JsonReader({
totalProperty: 'totalCount',
id:'id',
root:'list'
}, [
{id:'id',name: 'id',mapping:'id'},
{name: 'name',mapping:'name'},
{name: 'password',mapping:'password'},
{name: 'age',mapping:'age'},
{name: 'birthday',mapping:'birthday'},
{name: 'address',mapping:'address'},
{name: 'resume',mapping:'resume'}
])
});
var grid = new Ext.grid.GridPanel({
renderTo:document.body,
autoHeight: true,
store: store,
width:800,
height:400,
loadMask:true,
autoShow:true,
columnLines:true,
header:true,
frame:true,
cm:cm,
bbar: new Ext.PagingToolbar({
pageSize: 5,
store: store,
displayInfo: true,
displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条',
emptyMsg: "没有记录"
})
});
store.load({params:{start:0,limit:20}});
grid.render(); 后台action package ssh.cn.com.action.users;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import ssh.cn.com.service.UsersService;@SuppressWarnings("serial")public class ListUsersAction extends ActionSupport {
private UsersService service; public UsersService getService() {
return service;
}
public void setService(UsersService service) {
this.service = service;
} @SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
Map request = (Map) ActionContext.getContext().get("request"); request.put("list", service.findAll());
return SUCCESS;
}
}
//显示用户信息
Ext.QuickTips.init();
var cm = new Ext.grid.ColumnModel([
{header: '用户账号', dataIndex: 'id'},
{header: '用户姓名', dataIndex: 'username'},
{header: '用户密码', dataIndex: 'password'},
{header: '年龄', dataIndex: 'age'},
{header: '生日', dataIndex: 'birthday'},
{header: '地址', dataIndex: 'address'},
{header: 'resume', dataIndex: 'resume'}
]);
var store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url:'listUsersAction'}),
reader: new Ext.data.JsonReader({
totalProperty: 'totalCount',
id:'id',
root:'list'
}, [
{id:'id',name: 'id',mapping:'id'},
{name: 'name',mapping:'name'},
{name: 'password',mapping:'password'},
{name: 'age',mapping:'age'},
{name: 'birthday',mapping:'birthday'},
{name: 'address',mapping:'address'},
{name: 'resume',mapping:'resume'}
])
});
var grid = new Ext.grid.GridPanel({
renderTo:document.body,
autoHeight: true,
store: store,
width:800,
height:400,
loadMask:true,
autoShow:true,
columnLines:true,
header:true,
frame:true,
cm:cm,
bbar: new Ext.PagingToolbar({
pageSize: 5,
store: store,
displayInfo: true,
displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条',
emptyMsg: "没有记录"
})
});
store.load({params:{start:0,limit:20}});
grid.render(); 后台action package ssh.cn.com.action.users;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import ssh.cn.com.service.UsersService;@SuppressWarnings("serial")public class ListUsersAction extends ActionSupport {
private UsersService service; public UsersService getService() {
return service;
}
public void setService(UsersService service) {
this.service = service;
} @SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
Map request = (Map) ActionContext.getContext().get("request"); request.put("list", service.findAll());
return SUCCESS;
}
}
struts里面没有用过,在springmvc中用过,
记得当时不是用request的,用的是response.getWriter.write(json数据);
还有记得关闭流,否则也不会显示的…
还有你这样写难道是使用的return type="json"?
你的这种写法是错的,extjs不可能接收到的。
一种方式 ,用struts2的return type="json",不推荐,
第二种,用response.getwriter返回,不过都要是JSON数据格式。LZ连etxjs要接收什么数据都还不知道就要用ext。好好看看API先。
返回xml字符串会否?改用xmlReader
response.getOutputStream().print(new String(htmldata.getBytes("UTF-8"), "ISO8859-1"));
import java.io.PrintWriter;
import java.io.Serializable;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport implements Serializable {
private static final long serialVersionUID = 1L; public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
protected transient final Log log = LogFactory.getLog(getClass());
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
} public void outJsonString(String str) {
this.getResponse().setContentType("text/html;charset=UTF-8");
outString(str);
} public void outJson(Object obj) {
outJsonString(JSONObject.fromObject(obj).toString());
} public void outJsonArray(Object array) {
JSONArray json = JSONArray.fromObject(array);
outJsonString(json.toString());
}
//去掉JSON中不需要的字段
public void outJsonArray(Object array,String[] str) {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.setExcludes(str);
JSONArray json = JSONArray.fromObject(array);
System.out.println(json.toString());
outJsonString(json.toString());
}
public void outString(String str) {
try {
PrintWriter out = getResponse().getWriter();
out.write(str);
} catch (IOException e) {
}
} public void outXMLString(String xmlStr) {
getResponse().setContentType("application/xml;charset=UTF-8");
outString(xmlStr);
}}