2 yjwzsrzj YJWZSRZJ 于 2017.09.01 19:38 提问

json ssh ztree 无数据返回到页面
 package com.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.base.BaseService;
import com.entity.Administrator;
import com.entity.PowerEntity;
import com.instrument.Parameter;

public class IndexAction {
    @Autowired
    BaseService baseservice;

    @Autowired
    SessionFactory sessionFactory;

    List<String> pnamelist = new ArrayList<String>();

    List<ZtreeJS> ztreelist = null;

    @Action(value = "index", results = { @Result(name = "success", location = "/WEB-INF/jsp/index.jsp"),
            @Result(name = "defeated", location = "/login.jsp") })
    // 登录的方法
    public String index() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Parameter.CoustomParameter(Administrator.class, request);
        String hql = "from admin where account=? and apassword=? ";
        Session session = sessionFactory.openSession();
        Query<Administrator> query = session.createQuery(hql);
        query.setParameter(0, request.getParameter("username"));
        query.setParameter(1, request.getParameter("password"));
        List<Administrator> adminlist = query.list();
        if (adminlist.size() > 0) {
            System.err.println("登录成功");
            session.close();
            for (Administrator administrator : adminlist) {
                qureyPostId(administrator.getAid());
            }
            return "success";
        } else {
            System.err.println("登录失败");
            session.close();
            return "defeated";
        }

    }

    public void qureyPostId(int uid) {
        List<Integer> pidlist = new ArrayList();
        Session session = sessionFactory.openSession();
        Query<Object[]> query = session.createQuery("select r.rid from role r  join r.listadmin u  where u.aid=" + uid);
        List<Object[]> userlist = query.list();
        for (Object ob : userlist) {
            pidlist.add((Integer) ob);
            session.close();
        }
        qureyjuriId(pidlist);
        session.close();
    }

    public void qureyjuriId(List<Integer> pidlist) {
        List<Integer> powerlist = new ArrayList();
        Session session = sessionFactory.openSession();
        int pidlsitsize = pidlist.size() - 1;
        Query<Object[]> query = session.createSQLQuery("select pid from role_power p where rid=" + pidlist.get(pidlsitsize));
        List<Object[]> userlist = query.list();
        for (Object ob : userlist) {
            powerlist.add((Integer) ob);
            session.close();
        }
        qureyjuriName(powerlist);
        session.close();
    }

    public void qureyjuriName(List<Integer> jidlist) {
        pnamelist = new ArrayList();
        Session session = sessionFactory.openSession();
        for (Integer integer : jidlist) {
            Query<PowerEntity> query = session.createQuery("from power p where p.pid=" + integer);
            List<PowerEntity> userlist = query.list();
            for (PowerEntity ob : userlist) {
                pnamelist.add(ob.getPname());
            }
        }
        for (String integer : pnamelist) {
            System.out.println(integer);
        }
        session.close();
    }

    @Action(value = "ztree")
    public void ztree() {
        HttpServletResponse response = ServletActionContext.getResponse();
        List<ZtreeJS> ztreelist = new ArrayList<ZtreeJS>();
        for (String string : pnamelist) {
            ZtreeJS z = new ZtreeJS();
            z.setName(string);
            ztreelist.add(z);
        }
        String data = JSON.toJSONString(ztreelist);
        try {
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.write(data);
            out.flush();
            out.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 后台查询有数据,前台接收不了,
Csdn user default icon
上传中...
上传图片
插入图片