質問

Usually when building a page, you build a controller for that page with a RequestMapping tied to a URL as well as a RequestMethod.GET where you can set variables that needs to be displayed on the page.

What I have is a news widget which will be displayed on every page, if it was only displaying on one page, I'd be doing something like:

@Controller
@RequestMapping("/news")
public class NewsController {

    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(
            HttpServletRequest request,
            @Valid @ModelAttribute("RA") RA ra,
            ModelAndView modelAndView,
            Model model) {
        // do some stuff to ra, put values in model, ....
    }

}

And on the JSPX page I'd have something like:

<c:forEach var="article" items="${newsarticles}" >
    <h1>${article.title}</h1>
    ${article.body}
    <br />
</c:forEach>

The problem I'm trying to solve is how to duplicate the above JSPX snippet on all pages or even in the main template without having to explicitly list a gazillion URLs in the RequestMapping.

In JSF I would have simply used #{newsBean.newsarticles} and it would work on all pages.

A request interceptor will work, but is there a better way to do this without a RequestInterceptor, or is RequestInterceptor my only option?

役に立ちましたか?

解決

One way might be to implement the RequestInterceptor, but instead of grabbing the articles on every request, place the article list into the session, then the list is only grabbed once, saved to the session, and you just check to see if the list already exists within the session.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top