如何从控制器获取数据?

时间:2016-08-10 13:29:51

标签: java arraylist playframework querydsl

我有两个控制器(类)。在其中一个我正在提出请求及其结果数据我想转移到另一个控制器。但不知何故,它只返回一个空列表。 第一个发出请求并发送数据的控制器:

package controllers;

import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQueryFactory;
import models.*;
import play.db.jpa.JPAApi;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.addAll;

public class TestingController extends Controller {



    private JPAApi jpaApi;
    @Inject
    public TestingController(JPAApi jpaApi) {
        this.jpaApi = jpaApi;
    }

    private static ArrayList<ArrayList<Object>> listOfLists = new ArrayList<>();

    @Transactional
    public Result resolving_requests() {
        QClaim claim = QClaim.claim;
        QFinancialClaimData financialClaimData = QFinancialClaimData.financialClaimData;
        QDate_diff date_diff=QDate_diff.date_diff;
        EntityManager em=jpaApi.em();
        JPAQueryFactory queryFactory = new JPAQueryFactory(em);

        //QUERY1: Costs and amounts of different claims for the last year
        String columns[] = {"Month, Year", "Type of risk", "Risks count", "Risks amount"};
        List<Tuple> q1=queryFactory.select(claim.typeOfRisk, financialClaimData.overAllAmount.sum(), claim.claimDate_finish.count(), claim.claimDate_start.month(), claim.claimDate_start.year())
                .from(claim)
                .where(claim.claimDate_start.year().gt(LocalDate.now().getYear()-2).and(claim.claimDate_start.month().gt(LocalDate.now().getMonthValue())).or(claim.claimDate_start.year().eq(LocalDate.now().getYear())))
                .groupBy(claim.typeOfRisk, claim.claimDate_start.month(), claim.claimDate_start.year())
                .orderBy(claim.claimDate_start.year().asc(), claim.claimDate_start.month().asc(), claim.typeOfRisk.asc())
                .fetch( );
        ArrayList<Object> singleList = new ArrayList<>();
        addAll(singleList, columns); //Adding all columns' names to the first row, simailar to foreach
        this.listOfLists.add(singleList);
        for (Tuple l: q1){
            singleList = new ArrayList<>();
            singleList.add(l.get(claim.claimDate_start.month()).toString() + "/" + l.get(claim.claimDate_start.year()).toString());
            singleList.add(l.get(claim.typeOfRisk));
            singleList.add(l.get(claim.claimDate_finish.count()));
            singleList.add(l.get(financialClaimData.overAllAmount.sum()));
            this.listOfLists.add(singleList);
        }

        System.out.println(listOfLists);
        for (List i: listOfLists){
            for (Object i_inner: i){
                System.out.print(i_inner+"|"); //That works fine and I see a result
            }
            System.out.print("\n");
        }
        return ok("Your new application is ready.");
    }

    public static ArrayList<ArrayList<Object>> get_arr_list() {
        return listOfLists; //Probably that returns empty list
    }
}

第二个控制器,应该接收ArrayList: 包控制器;

import com.fasterxml.jackson.databind.JsonNode;
import play.mvc.*;
import java.util.ArrayList;


public class ReportsController extends Controller {

    public Result generating_reports(){
                ArrayList<ArrayList<Object>> listOfLists = TestingController.get_arr_list();
                System.out.println("Here should be a list: "+listOfLists); //This returns an empty list []

        return  ok("All is ready!");
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:1)

首先发布一个演示问题的最小可运行代码至关重要。 这意味着删除所有不必要的东西,并包含编译和运行代码所需的所有内容。这是一个例子(见解释评论):

import static java.util.Collections.addAll;
import java.util.ArrayList;

public class TestingController {

    private static ArrayList<ArrayList<Object>> listOfLists = new ArrayList<>();

    //in the code you posted this method never gets executed
    public void resolving_requests() {

        String columns[] = {"Month, Year", "Type of risk", "Risks count", "Risks amount"};

        ArrayList<Object> singleList = new ArrayList<>();
        addAll(singleList, columns); //Adding all columns' names to the first row, simailar to foreach

        listOfLists.add(singleList);
    }

    public static ArrayList<ArrayList<Object>> get_arr_list() {
        return listOfLists; //Probably that returns empty list
    }

    public static void main(String args[]) {

        //this will print an empty list because resolving_requests was not executed
        System.out.println("listOfLists "+ TestingController.get_arr_list()) ;

        //execute resolving_requests
        new TestingController().resolving_requests();

        //now array list is not empty 
        System.out.println("listOfLists "+ TestingController.get_arr_list()) ;
    }
}