Creating a Spring MVC controller for a specific JPA entity

The controller all command let's you create controllers for all JPA entities for which a corresponding controller doesn't exist. If you want to control the web request path to which the controller is mapped or the operations supported by the controller, then you should use the controller scaffold command.

Getting ready

Delete the contents of the ch04-recipe sub-directory inside the C: oo-cookbook directory.

Copy the ch04_web-app.roo script into the ch04-recipe directory.

Execute the ch04_web-app.roo script that creates the flight-app Roo project, sets up Hibernate as the persistence provider, configures MySQL as the database for the application, creates Flight and the FlightDescription JPA entities, and defines a many-to-one relationship between the Flight and FlightDescription entities. If you are using a different database than MySQL or your connection settings are different than what is specified in the script, then modify the script accordingly.

Start the Roo shell from the C: oo-cookbookch04-recipe directory.

How to do it...

The following steps show how to use the controller scaffold command to create controllers for JPA entities:

  1. Execute the controller scaffold command to create FlightController, as shown here:
    ..roo> controller scaffold --class ~.web.FlightController --entity ~.domain.Flight --path /myflightpath --disallowedOperations update,delete
    
  2. Execute the perform eclipse command to update the classpath settings, and import the project in your Eclipse IDE.

How it works...

The following table describes the purpose of each of the arguments passed to the controller scaffold command:

Argument

Description

class

(mandatory)

Fully-qualified name of the controller class, which you want to create.

entity

(optional )

Fully-qualified name of the Roo-managed JPA entity class (a class annotated with @RooEntity annotation), which is used as a form-backing object by the generated controller. The value of this argument translates into the value of the formBackingObject attribute of the @RooWebScaffold annotation.

path

(optional)

Identifies the sub-directory inside /WEB-INF/views/, which contains the JSPX views corresponding to the controller. The value of this argument translates into the value of the path attribute of the @RooWebScaffold annotation.

The value of the path argument also translates into the value of the @RequestMapping class-level annotation in the generated controller.

disallowedOperations

(optional)

Comma-separated list of operations, which is not supported by the generated controller. For instance, if the value is update,delete, then the generated controller doesn't contain methods to update and delete the JPA entity corresponding to which the controller was generated. The only valid values for this argument are update, delete, and create.

The following code from the FlightController.java file shows the FlightController created by the controller scaffold command:

@RooWebScaffold(path = "myflightpath", formBackingObject = Flight.class, update = false, delete = false)
@RequestMapping("/myflightpath")
@Controller
public class FlightController {
}

As the given code shows, the value of the path attribute of the @RooWebScaffold annotation and the @RequestMapping annotations are derived from the value of the path argument of the controller scaffold command. The value of the disallowedOperations argument of the controller scaffold command is used in the @RooWebScaffold annotation to specify the operations that are not supported by the generated controller.

The following table describes the elements of the @RooWebScaffold annotation:

Element

Description

path

Specifies the folder inside /WEB-INF/views/, which contains the JSPX views created corresponding to the controller.

formBackingObject

Specifies the JPA entity class, which the controller uses as the form-backing object.

update

Indicates if the update operation is defined by the *_Roo_Controller.aj AspectJ ITD of the controller. If true, Roo creates the JSPX view for updating the corresponding Roo-managed JPA entity. Default value is true.

create

Indicates if the create operation is defined by the *_Roo_Controller.aj AspectJ ITD of the controller. If true, Roo creates the JSPX view for creating the corresponding Roo-managed JPA entity. Default value is true.

delete

Indicates if the delete operation is defined by the *_Roo_Controller.aj AspectJ ITD of the controller. If true, the Roo generated JSPX view provides an option to delete the corresponding Roo-managed JPA entity. Default value is true.

exposeFinders

Exposes finder methods defined in the Roo-managed JPA entity. If the finder methods are exposed, a *_Roo_Controller_Finder.aj ITD is created that contains methods for rendering the form for entering search criteria, and for searching entity instances and showing search results. Default value is true.

exposeJson

Indicates that if the corresponding Roo-managed JPA entity is annotated with the @RooJson class-level annotation, then expose controller functionality (create, update, show, and delete) using JSON. The default value is true.

Refer to the Adding JSON support to domain objects and controllers recipe for details on how to add JSON support to Roo-managed JPA entities and Spring Web MVC controllers.

The following table describes methods that form part of the *_Roo_Controller.aj ITD, assuming that all controller operations were generated:

Method

Description

createForm

Shows the form for creating the entity.

Creates a new instance of the form-backing object (which is the Roo-managed JPA entity specified by the formBackingObject attribute of the @RooWebScaffold annotation), adds dependencies required for persisting the Roo-managed JPA entity (these dependencies include entities that participate in the relationship, such as FlightDescription is required for persisting a Flight entity), and adds date/time patterns (if required).

create

Persists Roo-managed entity exposed by the controller. Also performs JSR 303 (if available) validation on the entity instance. Adds date/time patterns (if required).

show

Shows details of a persisted entity instance. Adds date/time patterns (if required).

list

Shows the list of persistent entity instances. Adds date/time patterns (if required). Also adds support for pagination of data.

updateForm

Shows the form for updating an entity instance. Adds date/time patterns (if required).

update

Persists changes to an entity instance. Adds date/time patterns (if required).

delete

Deletes an entity instance.

There's more...

As shown in this recipe, the controller scaffold command provides options to help create a customized controller. Even if you have created controllers using the controller all command, you can still customize the controller by setting the attributes of the @RooWebScaffold annotation.

Let's now see how you can override the auto-generated methods in the *_Roo_Controller.aj file:

Overriding auto-generated controller methods

In some scenarios, you may want to override the auto-generated methods of the *_Roo_Controller.aj file to provide custom implementation. To override a method defined in ITD, all you need to do is to define a method with the same or different arguments and return types, but with the same name, in your controller Java file.

Let's say that in our flight-app web application we need to address the following requirements:

  • Currently, when a Flight entity instance is updated the FlightController shows the updated entity instance in a read-only view. This default functionality needs to be changed such that after update, the controller shows the list of Flight instances.
  • To address this requirement, we need to override the default behavior of the update method defined in the FlightController_Roo_Controller.aj ITD. To override the default behavior of the update method, all you need to do is to define an update method (with the same or different arguments and return types) in the FlightController.java file, as shown here:
    @RequestMapping(method = RequestMethod.PUT)
    public String update(@Valid Flight flight, ..) {
      ....
       return "redirect: /myflightpath/list";
    }

    In the given code, the update method redirects the user to myflightpath/list (instead of /myflightpath/{flightId}) after persisting changes to the Flight entity.

    Note

    In Spring Roo 1.1.3, if you attempt to override a method defined in the *_Roo_Controller.aj file by defining it in your *Controller.java file, then Roo complains that the method is already defined in your *Controller.java file. This issue is resolved in Spring Roo 1.1.4 and later versions.

See also

  • Refer to the Manually creating a Spring MVC controller for a JPA entity recipe for manually creating a controller.
  • Refer to the Auto-generating Spring MVC controllers and JSPX views from JPA entities recipe for details on how the controller all command is used for generating entities.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194