DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Converting a Spring Controller into a @Controller

Converting a Spring Controller into a @Controller

David Salter user avatar by
David Salter
·
Apr. 15, 11 · Java Zone · Interview
Like (0)
Save
Tweet
16.06K Views

Join the DZone community and get the full member experience.

Join For Free

In the Spring Web Framework, its typical to implement a Controller as a class that implements
org.springframework.web.servlet.mvc.Controller, for example:

public class InventoryController implements Controller { 
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Handle the request here
}
}
This class would then be defined within an application's Spring context XML file (typically appname-servlet.xml)
 
<bean name="/home.htm" class="springapp.web.InventoryController"> 
...
</bean>
 
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.
To 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 ModelAndView instance.
@Controller 
@RequestMapping("/home.htm")
public class InventoryController {

@RequestMapping(method=RequestMethod.GET)
public ModelAndView handleRequest() {
// Handle the request here
} }
 
 
Now 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.
<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<annotation-driven />
...
</beans:beans>

 

From http://www.davidsalter.co.uk/1/post/2011/04/converting-a-spring-controller-into-a-controller.html

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Are Microservices?
  • Low Code and No Code: The Security Challenge
  • 12 Modern CSS Techniques For Older CSS Problems
  • Hard Things in Computer Science

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo