日期:2014-05-16  浏览次数:20466 次

struts2如何返回json和处理json
以下是小弟的一点研究成果,拿来和大家分享,我用了3到4中struts2操作json找了一种我认识最合理和最简单的,最下面有引用网上的资料,因为都一样就没有必要赘述了,上面都是我自己的实例,如有错误请大家指正,若有疑问请联系我 QQ 376434729。

需要的jar包是struts2.1.8,json插件就是2.1.8的struts2-json-plugin-2.1.8.1,因为json的第三方插件有很多,例如json-lib-2.4-jdk15和json虽然说可以实现json的处理,但是相对来说可能是有点麻烦,而S2自带的插件为简单一点。

实例:(分页查询和添加)

实体类(domain)
public classFieldmanage implements java.io.Serializable {
 
    // Fields
 
    private Integer fieldManageId;
    private String fieldManageName;
    private String fieldManageDesc;
    private String isuse;
    getter&&setter省略。
}


数据访问Dao
public classFieldManageDao extendsHibernateEntityDao<Fieldmanage> {
   
public booleanoperaFieldmanage(Fieldmanage fieldmanage){
        try {
            this.saveOrUpdate(fieldmanage);
            return true;
        } catch(Exception e) {
            e.printStackTrace();
        }
        return false;
    }

 
publicList<Fieldmanage> getFieldmanageList(String name,intstart,int end){
String hql="from Fieldmanage where fieldManageName like '%"+name+"%'";
List<Fieldmanage> list=null;
try {
Query q = this.getSession().createQuery(hql);
            q.setFirstResult(start);
            q.setMaxResults(end);
            list = q.list();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return list;
}

   
    public intgetFieldmanageCount(String name){
String hql="select count(*) from Fieldmanage where fieldManageName like '%"+name+"%'";
        intcount=0;
        try {
    Query q =this.getSession().createQuery(hql);
count = Integer.parseInt(q.uniqueResult().toString());
        } catch(Exception e) {
            e.printStackTrace();
        }
        returncount;
    }


业务逻辑层Service
public classFieldManageService {
    //spring注入
    privateFieldManageDao fieldManageDao;
public voidsetFieldManageDao(FieldManageDao fieldManageDao) {
        this.fieldManageDao = fieldManageDao;
    }
   
   
public booleanoperaFieldmanage(Fieldmanage fieldmanage){
return fieldManageDao. operaFieldmanage (fieldmanage);
 
    }

      
publicList<Fieldmanage> getFieldmanageList (String name,intstart,int end){
    List<Fieldmanage> list=null;
    try {
list = fieldManageDao.getFieldmanageList(name, start, end);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return list;
}
   
public intgetFieldmanageCount(String name){
return fieldManageDao.getFieldmanageCount(name);
}

}

Struts控制层
public classFieldManageAction extends ActionSupport {
    privateFieldManageService fieldManageService;
    privateFieldmanage fieldmanage;
   
    privateList<Fieldmanage> data;
    private int totalCount;
    privateString success;
    //分页
    private int start;
    private int limit;
    均有getter和setter方法,但是service注入进来的方法最好没有get
   
   
   
    publicString operaFieldmanage(){
            try {
    boolean sign = fieldManageService.operaFieldmanage(fieldmanage);
                if(sign){
                    this.setSuccess("true");
                }else{
                    this.setSuccess("false");
                }
            } catch(Exception e) {
                this.setSuccess("false");
                e.printStackTrace();
            }
            return "operaFieldmanage";
    }
   
    publicString fieldManageList(){
        String name="";
        try {
if(fieldmanage对象和名称是否为null){
        name=fieldmanage.getFieldManageName();
    }
            intlimitNo=15;
            if(limit!=0){
                limitNo=limit;
            }
int count = fieldManageService.getFieldmanageCount(name);
List<Fieldmanage> list = fieldManageService.getFieldmanageList(name, start, limitNo);
            this.setData(list);
            this.setTotalCount(count);
        } catch(Ex