简体   繁体   中英

Best practice for adding data field to a header jsp

I am writing a small Spring 4 MVC app. I'm new to it and thought I might try and get some advice about a point (apologies I've been looking at jave/spring for a couple of months now and I have a mixed level of unconsolidated knowledge which leaves me wildly fluctuating between jargon and lack of understanding).

I am adding a header jsp (or jspf) to all the pages (jsp's). At this early stage I am adding them with a tag as the include-prelude stopped working at some point although I may go back and sort this out if I get time. I don't think that is relevant though...

In the header.jsp I just want to include a logo, title, link to a help pdf and the date the underlying data was last updated (ideally updated once a day). This last has got me thinking...

Firstly is it reasonable to include data in a header ? And if it is presumably it makes more sense to call the date once and store it somewhere in the application (It is not mission critical that it refreshes if it changes but I'm sure there are reasonable ways to do this which ever way it goes).

Secondly, if it is reasonable, what would be the best way of doing this ? Would I create a bean with one property and fill the date on application load (I could call it appConfig and add more properties if they are needed I guess).

Thirdly, If this is reasonable then, and I have a horrible feeling this is a complete numpty question..., how would I access this object/data from the header. Or should I give up and just add it as a dependency injected field into the home page and give up putting it in the header.

Thanks if anyone does have time to help...

Use a HandlerInterceptor for this. Example:

public class LoadSiteDataInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private SiteDataRepository siteDataRepository;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        request.setAttribute("siteData", siteDataRepository.findOne(1L));

        return super.preHandle(request, response, handler);
    }
}

In mvc-dispatcher-servlet.xml :

<mvc:interceptors>
  <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <mvc:exclude-mapping path="/static/**"/>
    <bean class="com.example.LoadSiteDataInterceptor"/>
  </mvc:interceptor>
</mvc:interceptors>

This loads data from the database on every request. You'd probably want to either load the data into a singleton bean first, or implement caching.

In a JSP, you can then use ${siteData.someAttribute}

Check out SiteMesh for JSP templating.

One another way to achieve it is through @ControllerAdvice and add it to ModelAttribute. That way you have it in all pages accessed via that controllers. The data is available in your model with "headerData" as name.

@ControllerAdvice
public class ControllersAdvice {
@ModelAttribute("headerData")
public HeaderData getHeaderData(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
    HeaderData headerData = new HeaderData();
    // Get the header data and cache it. Set the cache timeframe to 1 day or whatever you want the frequency to be. This will be done using Spring @cacheable annotation.
    // dump all the data that you want to headerData like logo, title , link etc
    return headerData;
}
}

In your header.jsp, you can access it via ${headerData} . With caching you can control the frequency f refresh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM