Spring MVC framework[2] Controller

来源:互联网 发布:手机淘宝免费注册账号 编辑:程序博客网 时间:2024/05/17 04:53

All contents are referenced from Spring in Action 

Controller Class:

//@Controller annotation indicates that this class is a controller class. This annotation// is a specialization of the  @Component annotation, which means that <context:component-scan>// will pick up and register @Controller-annotated classes as beans,// just as if they were annotated with @Component. @Controllerpublic class HomeController {public static final int DEFAULT_SPITTLES_PER_PAGE = 25;private SpitterService spitterService;@Inject //inject service public HomeController(SpitterService spitterService) {this.spitterService = spitterService;}@RequestMapping({"/","/home"}) //request-handling method, the annotation Mapping URL to handlerpublic String showHomePage(Map<String, Object> model) {model.put("spittles", spitterService.getRecentSpittles(DEFAULT_SPITTLES_PER_PAGE));return "home"; //return view name to DispatcherServlet so that it can use the name to consult View Resolver for real<span style="white-space:pre"></span>// view implementation}}
XXX-context.xml
<!-- Enables the Spring MVC @Controller programming model --><annotation-driven /><!--scan the package containing @Controller annotated class to create a controller bean --><context:component-scan base-package="your.package.name" />



0 0
原创粉丝点击