2. Integration with Spring controllers

 Documentation Summary
 Page Summary

Server-Side Paging with Spring controllers

The last option when using Spring MVC is to use a controller on the server to implement the query.

To use this, you have to define a request mapping to the controller method :

@Controller("personController")
public class PersonController {

    @PersistenceContext
    protected EntityManager entityManager;
    
    @Transactional(readOnly=true)
    @RequestMapping("/person/find")
    public ModelMap findPersons(@RequestParam("filter") Person examplePerson, 
    		@RequestParam("first") int first, 
    		@RequestParam("max") int max, 
    		@RequestParam("order") String order, 
    		@RequestParam("desc") boolean desc) {
        String from = "from Person e ";
        String where = "where lower(e.lastName) like :lastName ";
        String orderBy = order != null ? "order by e." + order + (desc ? " desc" : "") : "";
        String lastName = examplePerson.getLastName() != null ? examplePerson.getLastName() : "";
        Query qc = entityManager.createQuery("select count(e) " + from + where);
        qc.setParameter("lastName", "%" + lastName.toLowerCase() + "%");
        long resultCount = (Long)qc.getSingleResult();
        if (max == 0)
            max = 36;
        Query ql = entityManager.createQuery("select e " + from + where + orderBy);
        ql.setFirstResult(first);
        ql.setMaxResults(max);
        ql.setParameter("lastName", "%" + lastName.toLowerCase() + "%");
        List<?> resultList = ql.getResultList();
        ModelMap result = new ModelMap();
        result.addAttribute("resultCount", resultCount);
        result.addAttribute("resultList", resultList);
        entityManager.flush();
        return result;
    }
}

The client-side use of this component would be :

Tide.getInstance().addComponentWithFactory("people", PagedQuery, 
{ remoteComponentName: "personController",
    useController: true,
    filterClass: Person,
    maxResults: 36 }, 
);

 


Browse Space

- Pages
- Blog
- Labels
- Attachments
- Bookmarks
- Mail
- Advanced

Explore Confluence

- Popular Labels
- Notation Guide

Your Account

Log In

Other Features

Add Content


Copyright © 2011 Granite Data Services S.A.S. All Rights Reserved.