作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个 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/
我是一名优秀的程序员,十分优秀!