Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.
In the Spring Web Framework, its typical to implement a Controller as a class that implements
org.springframework.web.servlet.mvc.Controller, for example:
This class would then be defined within an application's Spring context XML file (typically appname-servlet.xml)
Using Spring annotations however, its possible to remove the need to implement the
org.springframework.web.servlet.mvc.controller and remove the bean definition within the XML file.
change the Controller class to use annotations, the class needs to be
annotated with the @Controller and @RequestMapping annotations as shown
below. The method that will handle the request also needs to be
annotated with the @RequestMapping. Note that this method no longer
needs to confirm to the same signature as defined in
org.springframework.web.servlet.mvc.Controller and now simply returns a
that we've redefined the Controller class, we can remove the bean
definition from the application's context file. The final stage then to
allow Spring to use the annotated Controller is to specify in the
application's context file that we want to use annotations. This is
achieved by adding the <annotation-driven /> annotation into the
application context file.
The Java Zone is brought to you in partnership with JetBrains. Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.