walnutmon
walnutmon

Reputation: 5953

JSP Spring-MVC Reusing Controller Logic

psuedo:

@RequestMapping("/news/feed/featurednews/{feedname}")
public List<NewsModel> getFeed(String feedname, @RequestParam("start", optional) Integer startIndex) {
   return feedService.getFeaturedNewsByName(feedname);
}

@RequestMapping("/news/{newsPageName}")
public String goToNewsPage(Model m, String newsPageName) {
   m.addAttribute("stories", feedService.getFeaturedNewsByName(newsPageName));
   return getGenericNewsViewName();
}

as you can see I'm reusing the service that gets the feed, is that the best I can do here, or can I reuse the getFeed() method?

Upvotes: 2

Views: 1507

Answers (1)

Daff
Daff

Reputation: 44215

It's perfectly fine to write

@RequestMapping("/news/feed/featurednews/{feedname}")
public List<NewsModel> getFeed(String feedname, @RequestParam("start", optional) Integer startIndex) {
   return feedService.getFeaturedNewsByName(feedname);
}

@RequestMapping("/news/{newsPageName}")
public String goToNewsPage(Model m, String newsPageName) {
   m.addAttribute("stories", this.getFeed(newsPageName, 0));
   return getGenericNewsViewName();
}

The Controller by itself is a plain Java class, you just tell the Spring request dispatcher on where to map requests to using the annotations (which doesn't affect any normal method call).

Upvotes: 3

Related Questions