参数化对象属性

时间:2013-11-29 19:16:51

标签: java spring hibernate jpa

根据从超链接发送给它的参数,有人能告诉我一个代码有效的方法让spring mvc中的对象属性发生变化吗?

我正在修改spring petclinic示例应用程序,以便“所有者”详细信息页面可以显示特定“所有者”拥有的每种“宠物”的单独列表。目前,“宠物”列表是每个“所有者”的属性,并且可以在jstl中作为owner.pets访问。我想要的是我的jstl代码能够从jstl调用owner.cats,owner.dogs,owner.lizards等,并在网页的不同部分填充几个单独的列表,即使所有的猫,狗和蜥蜴存储在同一个基础数据表中。

我如何做到这一点?

以下是JpaOwnerRepositoryImpl.java的相关方法:

@SuppressWarnings("unchecked")
public Collection<Owner> findByLastName(String lastName) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em.createQuery("SELECT DISTINCT owner FROM Owner owner left join fetch owner.pets WHERE owner.lastName LIKE :lastName");
    query.setParameter("lastName", lastName + "%");
    return query.getResultList();
}

@Override
public Owner findById(int id) {
    // using 'join fetch' because a single query should load both owners and pets
    // using 'left join fetch' because it might happen that an owner does not have pets yet
    Query query = this.em.createQuery("SELECT owner FROM Owner owner left join fetch owner.pets WHERE owner.id =:id");
    query.setParameter("id", id);
    return (Owner) query.getSingleResult();
}

以下是Owner.java的相关方面:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
private Set<Pet> pets;

protected Set<Pet> getPetsInternal() {
    if (this.pets == null) {this.pets = new HashSet<Pet>();}
    return this.pets;
}

public List<Pet> getPets() {
    List<Pet> sortedPets = new ArrayList<Pet>(getPetsInternal());
    PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
    return Collections.unmodifiableList(sortedPets);
}

这是OwnerController.java的一部分,它管理网址模式“/所有者”,我希望我的jstl能够在页面的不同部分单独列出猫,狗,蜥蜴等(不是在一个分组中)列表,但在几个单独的列表中。):

@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(@RequestParam("ownerID") String ownerId, Owner owner, BindingResult result, Map<String, Object> model) {
    Collection<Owner> results = this.clinicService.findOwnerByLastName("");
    model.put("selections", results);
    int ownrId = Integer.parseInt(ownerId);
    model.put("sel_owner",this.clinicService.findOwnerById(ownrId));
    return "owners/ownersList";
}

1 个答案:

答案 0 :(得分:1)

既然你要求一个非冗长的解决方案,你可以做这种半脏的修复。

Owner.java

@Transient
private Set<Pet> cats = new HashSet<Pet>();

[...]

// Call this from OwnerController before returning data to page.
public void parsePets() {
  for (Pet pet : getPetsInternal()) {
    if ("cat".equals(pet.getType().getName())) {
      cats.add(pet);
    }
  }
}

public getCats() {
  return cats;
}

ownerDetail.jsp

[...]
<h3>Cats</h3>
<c:forEach var="cat" items="${owner.cats}">
    <p>Name: <c:out value="${cat.name}" /></p>
</c:forEach>

<h3>All pets</h3>
[...]

OwnerController.java

/**
 * Custom handler for displaying an owner.
 *
 * @param ownerId the ID of the owner to display
 * @return a ModelMap with the model attributes for the view
 */
@RequestMapping("/owners/{ownerId}")
public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) {
    ModelAndView mav = new ModelAndView("owners/ownerDetails");
    Owner owner = this.clinicService.findOwnerById(ownerId);
    owner.parsePets();
    mav.addObject(owner);
    return mav;
}

Screenshot of the change