我需要将此对象列表转换为 JSON,以便将其传递给我的 AJAX。
到目前为止我有(使用java playframework)
应用程序.java
@Transactional
public static Result fetchOptions(String strCategoryId, String strCategoryName) {
Main mainAppModel = new Main();
mainAppModel.populateOptions(strCategoryId, strCategoryName);
return ok(Json.toJson(mainAppModel));
}
主.java
public void populateCountryOptions() {
Countries countriesDb = new Countries();
lstCountries = countriesDb.getCountriesByRegion(strRegionId);
}
public void populateLocationOptions() {
Locations locationsDb = new Locations();
lstLocations = locationsDb.getLocationByCountry(strCountryId);
}
public void populateDepartmentOptions() {
Departments departmentsDb = new Departments();
lstDepartments = departmentsDb.getDepartmentByLocationId(strLocationId);
}
public void populateOptions(String strCategoryId, String strCategoryName) {
switch (strCategoryName) {
case "region":
strRegionId = strCategoryId;
populateCountryOptions();
break;
case "country":
strCountryId = strCategoryId;
populateLocationOptions();
break;
case "location":
strLocationId = strCategoryId;
populateDepartmentOptions();
break;
default:
break;
}
}
配置/路由
POST /fetchOptions/:strCategoryId/:strCategoryName controllers.Application.fetchOptions(strCategoryId: String, strCategoryName: String)
Departments.java(删除了 getter 和 setter)
package models.db;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import play.db.jpa.JPA;
@Table(name = "DEPARTMENTS")
@Entity
public class Departments {
@Id
@Column(name = "DEPARTMENT_ID")
private String strDepartmentId;
@Column(name = "DEPARTMENT_NAME")
private String strDepartmentName;
@Column(name = "MANAGER_ID")
private String strManagerId;
@Column(name = "LOCATION_ID")
private String strLocationId;
@SuppressWarnings("unchecked")
public List<Departments> getDepartmentByLocationId(String strLocationId) {
StringBuilder sb = new StringBuilder();
sb.append(" SELECT ");
sb.append(" DEPARTMENT_ID ");
sb.append(" , DEPARTMENT_NAME ");
sb.append(" , MANAGER_ID ");
sb.append(" , LOCATION_ID ");
sb.append(" FROM ");
sb.append(" DEPARTMENTS ");
sb.append(" WHERE ");
sb.append(" LOCATION_ID = '" + strLocationId + "'");
sb.append(" ORDER BY ");
sb.append(" DEPARTMENT_NAME ASC ");
List<Departments> lstDepartments = new ArrayList<Departments>();
try {
lstDepartments = JPA.em().createNativeQuery(sb.toString(), Departments.class).getResultList();
} catch (Exception e) {
// TODO: handle exception
}
return lstDepartments;
}
}
Country.java(用于比较)
package models.db;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import play.db.jpa.JPA;
@Table(name = "COUNTRIES")
@Entity
public class Countries {
@Id
@Column(name = "COUNTRY_ID")
private String strCountryId;
@Column(name = "COUNTRY_NAME")
private String strCountryName;
@Column(name = "REGION_ID")
private String strRegionId;
@SuppressWarnings("unchecked")
public List<Countries> getCountriesByRegion(String strRegionId) {
StringBuilder sb = new StringBuilder();
sb.append(" SELECT ");
sb.append(" COUNTRY_ID ");
sb.append(" , COUNTRY_NAME ");
sb.append(" , REGION_ID ");
sb.append(" FROM ");
sb.append(" COUNTRIES ");
sb.append(" WHERE ");
sb.append(" REGION_ID = '" + strRegionId + "'");
sb.append(" ORDER BY ");
sb.append(" COUNTRY_NAME ASC ");
List<Countries> lstCountries = new ArrayList<Countries>();
try {
lstCountries = JPA.em().createNativeQuery(sb.toString(), Countries.class).getResultList();
} catch (Exception e) {
// TODO: handle exception
}
return lstCountries;
}
}
我认为不再需要 javascript,因为将mainAppModel对象返回为Json时发生错误。
所以我在Main.java上有 3 个方法,在Application.java上使用相同的方法。如果我使用populateCountryOptions()和populateLocationOptions() Json 工作正常,但每当我使用populateDepartmentOptions()时就会发生无限递归。该模型看起来彼此相似。我不知道是什么导致了这个错误。谢谢你的帮助!