- 论坛徽章:
- 0
|
struts 1.x
public ActionForward jsonForTxry(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws ParseException{
LazyValidatorForm txryjcForm=(LazyValidatorForm)form;
int start=(txryjcForm.get("start")==null)?1:(Integer)txryjcForm.get("start");
int size=(txryjcForm.get("limit")==null)?10:(Integer)txryjcForm.get("limit");
String qdwdm=(txryjcForm.get("qdwdm")==null)?"":txryjcForm.get("qdwdm").toString();
String zdwdm=(txryjcForm.get("zdwdm")==null)?"":txryjcForm.get("qdwdm").toString();
String bbrq=(txryjcForm.get("bbrq")==null)?"":txryjcForm.get("qdwdm").toString();
String lx60=(txryjcForm.get("lx60")==null)?"":txryjcForm.get("qdwdm").toString();
String lx55=(txryjcForm.get("lx55")==null)?"":txryjcForm.get("qdwdm").toString();
String lx50=(txryjcForm.get("lx50")==null)?"":txryjcForm.get("qdwdm").toString();
String bdq=(txryjcForm.get("bdq")==null)?"":txryjcForm.get("qdwdm").toString();
String qxbm=UserInfo.getInstance().getQxbm();
Map Object,Object> map=new HashMap Object,Object>();
map.put("qdwdm", qdwdm);
map.put("zdwdm", zdwdm);
map.put("bbrq",new java.sql.Date(new SimpleDateFormat("yyyy/mm/dd").parse(bbrq).getTime()));
map.put("lx60", lx60);
map.put("lx55", lx55);
map.put("lx50", lx50);
map.put("bdq", bdq);
map.put("qxdm", qxbm);
Page queryPage=txryjc.pagedQuery(GrViewTxryjc.class, map, start / size + 1, size);
JSONObject json=new JSONObject();
try{
json.put("Total", queryPage.getTotalCount());
json.put("Limit", size);
JSONArray list=new JSONArray();
List rows=(List)queryPage.getResult();
for(int i=0;i rows.size();i++){
GrViewTxryjc grViewTxryjc=(GrViewTxryjc)rows.get(i);
Map convert=new HashMap();
map.put("jclb", grViewTxryjc.getJclb());
map.put("dwdm", grViewTxryjc.getDwdm());
map.put("dwmc", grViewTxryjc.getDwmc());
map.put("xm", grViewTxryjc.getXm());
map.put("xb", grViewTxryjc.getXb());
map.put("bzhm", grViewTxryjc.getBzhm());
map.put("csrq", grViewTxryjc.getCsrq());
list.put(map);
json.put("txryjc", list);
}
}
catch(JSONException e){
e.printStackTrace();
System.out.println("错误:"+e.getMessage());
}
this.renderText(response, json.toString());
return null;
}
//在父类的类中
protected void renderText(HttpServletResponse response, String text) {
render(response,text,"text/plain;charset=UTF-8");
}
protected void render(HttpServletResponse response, String text,String contentType){
try {
response.setContentType(contentType);
response.getWriter().write(text);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
这个是手动转的JSON 是struts1.X的写法 2.0也可以这么转,用struts2.0的JSON转换也可以(有时候好像有问题)
要用上面的写法要去下个 JSON的JAR文件 网上有~~
struts 2.x
服务器端struts主要实现对数据的交换工作,客户端跟struts提交数据或者索要数据。
ext2对JSON数据的处理支持很好,而且相比XML,JSON具有更好的处理性能。
我前段是用struts2来做的,下面是一个action的例子:
package action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.googlecode.jsonplugin.annotations.JSON;
import common.CacheManager;
import domain.ListRange;
import domain.User;
import UserService;
import ErrorMessage;
public class UserAction extends BaseActionSupport{
private UserService userService;
private int userid;
private String name;
private String loginid;
private String usertype;
public String list(){
listrange=new ListRange();
List l=cacheManager.getUsersList();
if (l==null||l.size()==0){
listrange.setTotalSize(0);
return SUCCESS;
}
listrange.setTotalSize(l.size());
List l2=null;
if (limit=0)
l2=l;
else{
l2=new ArrayList();
for (int i=start;istart+limit&&il.size();i++){
l2.add(l.get(i));
}
}
listrange.setData(l2.toArray());
return SUCCESS;
}
public String save(){
int rtn=0;
if (userid==0){
rtn=userService.addUser(loginid, name, usertype);
} else {
User user=new User();
user.setUserid(userid);
user.setName(name);
user.setUsertype(usertype);
user.setLoginid(loginid);
rtn=userService.updateUser(user);
}
setReturn(rtn);
return SUCCESS;
}
@JSON(serialize=false)
public String getLoginid() {
return loginid;
}
public void setLoginid(String loginid) {
this.loginid = loginid;
}
@JSON(serialize=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JSON(serialize=false)
public String getUsertype() {
return usertype;
}
public void setUsertype(String usertype) {
this.usertype = usertype;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
@JSON(serialize=true)
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
@JSON(name="list")
public ListRange getListrange() {
return listrange;
}
@JSON
public boolean isFailure() {
return failure;
}
@JSON
public boolean isSuccess() {
return success;
}
@JSON
public String getInfo() {
return info;
}
}
上面的例子显示了取用户列表(分页)和保存用户的功能。
用到了com.googlecode.jsonplugin(开源JSON插件),在struts的配置文件设置:
action name="user" class="userAction">
result type="json"/>
/action>
指出返回的类型为json
这样,struts action返回给客户端的就是一个JSON数据包
ext2处理这种JSON数据包支持很好。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/55983/showart_2033652.html |
|