gpt4 book ai didi

java - 如何从我的 Controller 获取数据?

转载 作者:行者123 更新时间:2023-12-01 09:36:20 25 4
gpt4 key购买 nike

我有两个 Controller (类)。在其中一个 Controller 中,我发出请求,并将其结果数据传输到另一个 Controller 。但不知何故它只返回一个空列表。第一个发出请求并发送数据的 Controller :

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
}
}

第二个 Controller ,应该接收 ArrayList:包 Controller ;

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!");
}
}

如何修复它?

最佳答案

首先,发布演示问题的最小可运行代码至关重要。这意味着删除所有不必要的内容,并包括编译和运行代码所需的所有内容。这是一个示例(请参阅注释以获取解释):

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()) ;
}
}

关于java - 如何从我的 Controller 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com