In some scenarios, you may want to set the focus of your commands to a particular Java type. For instance, you may want the Roo shell to execute field
commands on a particular Java type, so that you don't need to specify the class
argument in your field
commands.
Start the Roo shell from the C:\roo-cookbook\ch01-recipe
directory, which contains the flight-app
Roo project.
Spring Roo provides a focus
command, which lets you change the target of your commands to a different Java type. The following sequence of steps shows how we can use the focus
command to switch from one type to another:
Execute the following
focus
command to specify that you want to work with theflight-app
project. This will change the Roo prompt to reflect the top-level package name of Roo project:roo> focus --class ~
Create
FlightDesc
class using theclass
command. Use~
to denote that you are specifying the package name for the class with respect to the top-level package of the project. This will change the Roo prompt to refer to theFlightDesc
type, shown as follows:sample.roo.Flightapp roo> class --class ~.domain.FlightDesc --rooAnnotations Created SRC_MAIN_JAVA\sample\roo\flightapp\domain\FlightDesc.java
Create a
Flight
class using theclass
command. This will change the Roo prompt fromFlightDesc
type to refer to the newly createdFlight
type
, as shown here:~.domain.FlightDesc roo> class --class ~.domain.Flight --rooAnnotations Created SRC_MAIN_JAVA\sample\roo\flightapp\domain\Flight.java
Use a
focus
command to switch to theFlightDesc
type. This will change the Roo prompt from referring toFlight
type toFlightDesc
type, as shown here:~.domain.Flight roo> focus --class ~.domain.FlightDesc
Add
from_city
andto_city
attributes to theFlightDesc
class as shown here. As the currently referred type by Roo prompt isFlightDesc
, you don't need to specify theclass
argument:~.domain.FlightDesc roo> field string from_city ~.domain.FlightDesc roo> field string to_city
Without changing focus to the
Flight
class, add aflight_Id
attribute to theFlight
class by using thefield
command along with theclass
argument, as shown here. Theclass
argument specifies that the target of the command is theFlight
class and not the currently referredFlightDesc
class:~.domain.FlightDesc roo> field number --fieldName flight_Id --type java.lang.Integer --class ~.domain.Flight
The class
argument of the focus
command lets you specify the fully-qualified name of the Java type with which you want to work. The ~
symbol is used to indicate the top-level package of the Roo project that you specified during creation of the Roo project.
The use of the focus
command is mainly to simplify writing commands targeting a particular Java type. If you don't want to use the focus
command in a situation, then you can always use the class
argument of the command to specify the target Java type of the command.