The task of developing the Spring MVC application is incomplete without packaging, deploying, and using it. In this recipe, we will look at how a Roo-generated Spring Web MVC application is packaged, deployed, and run.
Delete the contents of ch04-recipe
sub-directory inside the C:
oo-cookbook
directory.
Copy the ch04_web-app.roo
script into the ch04-recipe
directory.
The 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 FlightDescription
JPA entities, and defines many-to-one relationships 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 Roo shell from the C:
oo-cookbookch04-recipe
directory.
For packaging, deploying, and using a Roo-generated Spring Web MVC application follow the given steps:
controller
all
command to create controllers and views corresponding to JPA entities in the flight-app
project, as shown here:roo> controller all --package ~.web
flight-app
web application using the perform
package
command of Roo:roo> perform package
perform
package
command will create a WAR file named flight-app-0.1.0.BUILD-SNAPSHOT.war
in the target
directory of your flight-app
project. You can now deploy the WAR file to your application server.flight-app
project as a dynamic web application in an embedded Tomcat instance, then exit the Roo shell and execute the following maven command:..ch04-recipe> mvn tomcat:run
Now, you can access the flight-app
application by accessing the following URL: http://localhost:8080/flight-app
If you see the following web page, then it means you have successfully deployed the flight-app
application on the embedded Tomcat instance:
Save the entered Flight Description by clicking the Save button.
As the Departure Date and Arrival Date fields are date type fields, a popup calendar is shown to simplify entering dates for these fields. You may notice that the Flight Description field is a drop-down field, which shows all Flight Descriptions that you have created. Roo shows a drop-down of Flight Descriptions because there exists many-to-one relationships between Flight
and FlightDescription
JPA entities.
Now, save Flight details by clicking the Save button.
ch04_web_app.roo
script) for our FlightDescription
JPA entity.flight-app
application: standard
and alt
(represented by the standard.properties
and alt.properties
files of the flight-app
project). The default theme of the Flight-app
application is standard
. You can change the theme of the Flight-app
application by selecting the alt
theme, as shown in the following screenshot:alt
theme moves menu options to the right and the main content of the page is moved to the left. As mentioned earlier, theme selected by a user is saved in the browser cookie; therefore, if you close the browser and reopen it, you will find that the alt
theme is applied by default on the web pages of the Flight-app
application.The perform
package
command runs maven's package
command, which does the packaging of the project. The project is packaged as a WAR file because the packaging (as per the <packaging>
element the in pom.xml
file) specified for the flight-app
project is war
. It is important to note that when the perform
package
command is executed, tests are not executed. It executes maven's package
command and specifies that the tests are skipped, as shown here:
..ch04-recipe>mvn package -Dmaven.test.skip
As mentioned earlier, the perform
command
of Roo can be used to execute maven commands. You can execute maven's package
command (and execute tests) from the Roo shell, as shown here:
.. roo> perform command --mavenCommand package
Don't run an embedded Tomcat instance from a Roo shell
You can run the flight-app
project as a web application in the embedded Tomcat instance using Roo's perform
command
, but you should not do it because it will result in the creation of a different process, which you can't stop using CTRL-C and also you can't execute Roo commands from the Roo shell.
The flight-app
project's pom.xml
file configures Tomcat
Maven
Plugin
(http://mojo.codehaus.org/tomcat-maven-plugin/index.html), which makes it possible to run the flight-app
project as a dynamic web application in an embedded Tomcat instance. The following XML fragment shows the configuration of the Tomcat Maven Plugin:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.0</version> </plugin>
You can configure the plugin to affect the behavior of the embedded Tomcat instance. For instance, if you want to change the default 8080 HTTP port to 8090, then you can do so by supplying the maven.tomcat.port
system property when running the project on the Tomcat instance, as shown here:
..ch04-recipe> mvn tomcat:run –Dmaven.tomcat.port=8090
Roo also provides a perform
assembly
command, which executes an assembly
goal of Maven
Assembly
Plugin
(http://maven.apache.org/plugins/maven-assembly-plugin/, configured in the pom.xml
file of the Roo project). You should use the perform
assembly
command only if you want to distribute your project as an archive, such as ZIP, TAR, WAR, JAR, and so on. This distributable archive contains configuration files, project documentation, and runtime dependencies of the project.
The Roo configures the Jetty
Maven
Plugin
(http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin) in the pom.xml
file to support running the Roo project as a web application in an embedded Jetty container. To run the flight-app
project in an embedded Jetty container, all you need to do is to execute the jetty:run
goal of the plugin from the directory containing your project's pom.xml
file:
..ch04-recipe> mvn jetty:run
If you want to change the default 8080 HTTP port to 8090 on which the Jetty container listens for HTTP requests, then you can configure it by specifying the jetty.port
system property, as shown here:
..ch04-recipe> mvn jetty:run –Djetty.port=8090
3.144.48.204