Can someone show me a code efficient way to have an object property in spring mvc change based on parameters sent to it from a hyperlink?
I am modifying the spring
Since you asked for a non-verbose solution, you could just do this kind of semi-dirty fix.
@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;
}
[...]
<h3>Cats</h3>
<c:forEach var="cat" items="${owner.cats}">
<p>Name: <c:out value="${cat.name}" /></p>
</c:forEach>
<h3>All pets</h3>
[...]
/**
* 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;
}