Wednesday, 15 May 2013

spring - Best practice to access and display data from DB that is common across all pages -


I define Spring MVC controllers for each JSP page, such as HomePageController, CategoryPageController, ProductPageController, CartPageController etc. Along with a webstore. And all these controllers call DB to bring and display menus at the top of the JSP pages. Call DB to bring each of the mentors mentioned above.

I'm using the spring 3.0 framework.

HomePageController.java

  @Controller @RequestMapping (HOME_PAGE_CONTROLLER) public class HomePageController extends BasePageController {@RequestMapping (method = RequestMethod.GET) protected ModelAndView ProcessRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ModelAndView MAV = new ModelAndView (); Mav.addObject ("menu categories", masterdata service .gate all menu categories ()); Return Show Page (Maui, HOME_PAGE_URL); }}  

I think in a way that this call is unnecessary, because 1) all the controllers menu 2) difficult to maintain to maintain because of changes in the business logic changes There are many controllers this call will be required

What is the best practice in this type of scenario? How can I get this call from multiple controllers? Is this a way to call a central location so that this piece of code does not cut into all the controllers?

Avoid address duplicate controller sections - If the expansion of all controller classes BasePageController :

  public abstract class BasePageController {... @ModelAttribute public void initMenuCategories (model) {model.addAttribute ( "menuCategories", masterDataService.getAllMenuCategories ()); } ...  

Decrease database calls per page - Consider caching menu categories if they do not change too many times:

  @ Service Public Class MasterDataService {... @Cacheable (key = "menu", value = "menu") Public list & lt; MenuCategory & gt; GetAllMenuCategories () {...} ...}  

To do this you need to add a cache called menu in your service level configuration for details. See the documentation.


No comments:

Post a Comment