JSP Spring-MVC Reusing Controller Logic
psuedo:
@RequestMapping("/news/feed/featuredn开发者_如何转开发ews/{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?
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).
精彩评论