在College.java模型类中添加的方法
public static List listCollegesForCity(String pCity){ return entityManager()。createQuery(“SELECT o FROM College o WHERE o.addressInfoId.city =?1”,College.class) .setParameter(1,pCity) .getResultList(); }
public static List<College> listColleges(String pCity, String pState, String pCountry) {
return entityManager().createQuery("SELECT o from College o WHERE o.addressInfoId.city=?1 AND (?2 IS null or o.addressInfoId.state=?2) AND (?3 IS null OR o.addressInfoId.country=?3)", College.class)
.setParameter(1, pCity)
.setParameter(2, pState)
.setParameter(3, pCountry)
.getResultList();
}
public static List<College> listColleges(String pCity, String pState, String pCountry, int pFirstResults, int pMaxResults) {
return entityManager().createQuery(" ", College.class)
.setParameter(1, pCity)
.setParameter(2, pState)
.setParameter(3, pCountry)
.setFirstResult(pFirstResults)
.setMaxResults(pMaxResults)
.getResultList();
}
public static long countColleges(String pCity, String pState, String pCountry) {
return entityManager().createQuery("SELECT COUNT(o) from College o WHERE o.addressInfoId.city=?1 AND (?2 IS null or o.addressInfoId.state=?2) AND (?3 IS null OR o.addressInfoId.country=?3)", Long.class)
.setParameter(1, pCity)
.setParameter(2, pState)
.setParameter(3, pCountry)
.getSingleResult();
}
}
这是我在collegeController.java中的常规
// http://localhost:8080/college/colleges/find?city=pune&country=india&state=maharashtra
@RequestMapping(value="/find", method = RequestMethod.GET)
public String findCollegeForCity(Model uiModel, @RequestParam(value = "city", required = true) String pCity,
@RequestParam(value = "country", required = false) String pCountry,
@RequestParam(value = "state", required = false) String pState,
@RequestParam(value = "page", required = false) Integer page,
@RequestParam(value = "size", required = false) Integer size) {
if (page != null || size != null) {
int sizeNo = size == null ? 10 : size.intValue();
final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
uiModel.addAttribute("colleges", College.listColleges(pCity, pState, pCountry, firstResult, sizeNo));
float nrOfPages = (float) College.countColleges(pCity, pState, pCountry) / sizeNo;
uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
} else {
uiModel.addAttribute("colleges", College.listColleges(pCity, pState, pCountry));
}
return "colleges/list";
}
}
答案 0 :(得分:1)
我认为您在方法签名中缺少@PathVariable String city
:
@RequestMapping(value="/{city}",params = { "find=city", "form" }, method = RequestMethod.GET)
public String findCollegeForCity(Model uiModel, @PathVariable String city) {
uiModel.addAttribute("colleges", College.listCollegesForCity(city));
return "colleges/find";
}