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.
@Controller
public 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 handler
public 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" />