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.
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.
The following steps show how
to use the controller
scaffold
command to create controllers for JPA entities:
controller
scaffold
command to create FlightController
, as shown here:..roo> controller scaffold --class ~.web.FlightController --entity ~.domain.Flight --path /myflightpath --disallowedOperations update,delete
perform eclipse
command to update the classpath settings, and import the project in your Eclipse IDE.The following table describes the purpose of each of the arguments passed to the controller
scaffold
command:
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:
The following table describes methods that form part of the *_Roo_Controller.aj
ITD, assuming that all controller operations were generated:
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:
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:
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.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.
18.191.176.194