Book Image

Spring Roo 1.1 Cookbook

Book Image

Spring Roo 1.1 Cookbook

Overview of this book

Spring Roo is an easy-to-use productivity tool for rapidly developing Java enterprise applications using well-recognized frameworks such as Spring, Hibernate, AspectJ, Spring Web Flow, Spring Security, GWT, and so on. Spring Roo takes care of creating maven-enabled projects, enterprise application architecture based on your choice of technologies, unit/integration tests based on your choice of testing framework, and so on. The bottom line is that if you're using Spring, then you must consider using Spring Roo for increased productivity. Spring Roo 1.1 Cookbook brings together a collection of recipes that demonstrate how the Spring Roo developer tool simplifies rapidly developing enterprise applications using standard technologies/frameworks such as JPA, GWT, Spring, Flex, Spring Web Flow, Spring Security, and so on. It introduces readers to developing enterprise applications for the real world using Spring Roo tool. The book starts off with basic recipes to make readers comfortable with using Spring Roo tool. As the book progresses, readers are introduced to more sophisticated features supported by Spring Roo in the context of a Flight Booking application. In a step-by-step by fashion, each recipe shows how a particular activity is performed, what Spring Roo does when a command is executed, and why it is important in the context of the application being developed. Initially, you make a quick start with using Spring Roo through some simple recipes. Then you learn how Spring Roo simplifies creating the persistence layer of an enterprise application using JPA. You are introduced to the various roo commands to create JPA entities, create relationships between JPA entities, create integration tests using Spring TestContext framework, and so on. Following this, the book shows you how Spring Roo simplifies creating the web layer of an enterprise application using Spring Web MVC, Spring Web Flow, and how to create selenium tests for controller objects. Subsequently, we focus on using Spring-BlazeDS, GWT, JSON, and so on. Spring Roo commands that are used to incorporate e-mail/messaging features into an enterprise application are demonstrated next. Finally, we wrap it up with some miscellaneous recipes that show how to extend Spring Roo via add-ons, incorporate security, create cloud-ready applications, remove Spring Roo from your enterprise application, and so on.
Table of Contents (14 chapters)
Spring Roo 1.1 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Referring to a type from the Roo shell


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.

Getting ready

Start the Roo shell from the C:\roo-cookbook\ch01-recipe directory, which contains the flight-app Roo project.

How to do it...

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:

  1. Execute the following focus command to specify that you want to work with the flight-app project. This will change the Roo prompt to reflect the top-level package name of Roo project:

    roo> focus --class ~
    
  2. Create FlightDesc class using the class 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 the FlightDesc type, shown as follows:

    sample.roo.Flightapp roo> class --class  ~.domain.FlightDesc --rooAnnotations
    
    Created SRC_MAIN_JAVA\sample\roo\flightapp\domain\FlightDesc.java
    
  3. Create a Flight class using the class command. This will change the Roo prompt from FlightDesc type to refer to the newly created Flight type, as shown here:

    ~.domain.FlightDesc roo> class --class ~.domain.Flight --rooAnnotations
    
    Created SRC_MAIN_JAVA\sample\roo\flightapp\domain\Flight.java
    
  4. Use a focus command to switch to the FlightDesc type. This will change the Roo prompt from referring to Flight type to FlightDesc type, as shown here:

    ~.domain.Flight roo> focus --class ~.domain.FlightDesc
    
  5. Add from_city and to_city attributes to the FlightDesc class as shown here. As the currently referred type by Roo prompt is FlightDesc, you don't need to specify the class argument:

    ~.domain.FlightDesc roo> field string from_city
    ~.domain.FlightDesc roo> field string to_city
    
  6. Without changing focus to the Flight class, add a flight_Id attribute to the Flight class by using the field command along with the class argument, as shown here. The class argument specifies that the target of the command is the Flight class and not the currently referred FlightDesc class:

    ~.domain.FlightDesc roo> field number --fieldName flight_Id --type java.lang.Integer --class ~.domain.Flight
    

How it works...

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.

Tip

The use of the ~ symbol simplifies providing the value of any argument that expects a fully-qualified name of a Java type in your Roo project.

There's more...

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.

See also

  • The Creating a Java class recipe shows how to create a Java class using Spring Roo

  • The Adding attributes to a Java class recipe shows how to add attributes to a Java class using Spring Roo