parameterizing object properties

前端 未结 1 1716
死守一世寂寞
死守一世寂寞 2020-12-12 06:53

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

相关标签:
1条回答
  • 2020-12-12 07:22

    Since you asked for a non-verbose solution, you could just do this kind of semi-dirty fix.

    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

    0 讨论(0)
提交回复
热议问题