facebook

Using Spring Annotators to Implement a Spring Controller

This tutorial walks you through using the Spring Annotator for implementing a Spring controller. A standard Java class is added to a scaffolded project, and the Spring Annotator Spring-enables the Java class.

While this tutorial focuses on Spring controllers, the Spring Annotator can also be used for Spring services, components, and repositories.  In this tutorial, you will learn how to:

  • Create a Java class
  • Configure the class as a Spring controller
Customizing Spring code generation requires a MyEclipse Spring or Bling subscription.

1. Create a new Java Class

First, create the Java class. It doesn’t matter how the Java  class is created. It could be hand-coded or generated by Spring MVC  scaffolding. For this tutorial you will create a new Java class in a scaffolded project.

  1. Scaffold a Spring MVC Application.
  2. Right-click CustomersApp, and select New>Class.
  3. Type org.customerapp.web as the package name, and MyController in the Name field. Click Finish.

    Adding a new class
  4. Replace the default code with the following. Note that this code contains nothing specific to Spring.
    package org.customerapp.web;
    
    import org.customerapp.dao.CustomerDAO;
    import org.customerapp.service.CustomerService;
    
    public class MyController {
    
       public CustomerService custService;
    
       public CustomerDAO custDAO;
    
       public String doSomethingOne(){
          return "";
       }
    
       public String doSomethingTwo(String xyz){
          return "";
       }
    }
  5. Right-click the class file in the Explorer, and select Source>Generate Getters and Setters.
  6. Select all getters and setters, and click OK.

    Generating getters and setters
  7. Save the file.


2. Configure as a Spring Controller

Now that you have the Java class in place, use the Spring Annotator to  handle the Spring-specific annotation and configuration of the Java  class.

  1. While the MyController class is open in the Java editor, switch to the Spring Annotations view. If the view isn’t visible, open it by selecting Window>Show View.

    Note: If nothing appears in the Spring Annotations view, close and reopen the MyController class file.

    Spring annotation assistant
  2. Specify how you would like to stereotype the new Java class. For this tutorial, click Controller. When you select Controller, the Java class is immediately annotated with the @Controller annotation. Double-click the code in the code editor, and the Spring annotation editor reloads with the current configuration.

    The Spring annotation assistant now lets you further configure the Java class with annotations relevant to Spring controllers.
  3. Double-click the class name (MyController) in the code or select it in the annotator’s outline view. The configuration panel displays a list of available class-level annotations.

    Selecting the class
  4. Type MyNewController as the controller name, and select singleton for the scope. As you configure the Java class using the Spring annotation assistant, the Java code is immediately updated to reflect the configuration.

    Configuring the classIn this example, the MyController class is being annotated as a Spring Controller. The bean name is MyNewController, and the scope of the controller is singleton. If needed, you can switch to the Transactional tab to configure and specify the transactional properties for the controller.

    Note:
    If you need help understanding what a specific annotation/configuration means, you can get more information in the online help by clicking the annotation or configuration.
  5. Double-click the custService variable declaration in the code or select it in the annotator’s outline view. The configuration panel displays a list of available variable-level annotations.
  6. Select the @Resource radio button, and enter CustomerService as the name.

    Configuring a class variableIn this example, the variable is being configured to be injected by Spring with a bean that’s named CustomerService, which is one of the originally scaffolded Spring Services (from the scaffolding tutorial).

    Note:
    If you know the name of the resource to be injected, you can type it directly into the Name field. If the Spring Nature has been added to the current project, you can also select the bean using content assist (CTRL+ Space), which lists available Spring beans.
  7. Double-click the doSomethingOne() method in the code or select it in the annotator’s outline view. The configuration panel displays a list of available method-level annotations.
  8. Select the @RequestMapping radio button, enter /dosomething.html as the URL, and select GET and POST as the methods.

    Configuring the class methodsIn this example, the doSomethingOne() method is being configured as a request handler. It’s being bound to the /dosomething.html URL, and specifically to GET and POST methods. If needed, you can switch to the Transactional tab to configure and specify the transactional properties for the request handler.
  9. Double-click the doSomethingTwo() method in the code or select it in the annotator’s outline view. The configuration panel displays a list of available method-level annotations.
  10. Select the @RequestMapping radio button, enter /dosomethingelse.html as the URL, and select GET and POST as the methods. You can configure the individual method arguments, including how the method argument is mapped (@RequestParm or @ModelAttribute) and the parameter name (if different from the method argument name).

    Configuring method parametersIn this example the doSomethingTwo() method is being configured as a request handler. It’s being bound to the /dosomethingelse.html URL, and specifically to GET and POST methods. The xyz method argument is configured as a request parameter (@RequestParam) and bound to the username request parameter.