我有兩個控制器(類)。在其中一箇中,我正在提出請求,並將其結果數據傳輸到另一個控制器。但不知何故,它只返回一個空列表。 第一控制器,使一個請求,併發送數據:如何從我的控制器獲取數據?
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!");
}
}
我該如何解決它?
[MCVE]如何用最少的代碼來證明問題? – c0der
@Bogdan除非執行了'resolving_requests()'(通過自己的http req或調用),否則該列表將爲空...另外,併發設置中的靜態ArrayList會給您帶來很多問題... – Salem
@Salem我該如何執行它? –