Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SPRING COOKBOOK

You're reading from   SPRING COOKBOOK Over 100 hands-on recipes to build Spring web applications easily and efficiently

Arrow left icon
Product type Paperback
Published in May 2015
Publisher Packt
ISBN-13 9781783985807
Length 234 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Murat Yilmaz Murat Yilmaz
Author Profile Icon Murat Yilmaz
Murat Yilmaz
Jerome Jaglale Jerome Jaglale
Author Profile Icon Jerome Jaglale
Jerome Jaglale
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Creating a Spring Application FREE CHAPTER 2. Defining Beans and Using Dependency Injection 3. Using Controllers and Views 4. Querying a Database 5. Using Forms 6. Managing Security 7. Unit Testing 8. Running Batch Jobs 9. Handling Mobiles and Tablets 10. Connecting to Facebook and Twitter 11. Using the Java RMI, HTTP Invoker, Hessian, and REST 12. Using Aspect-oriented Programming Index

Running a Spring web application

In this recipe, we will use the Spring web application from the previous recipe. We will compile it with Maven and run it with Tomcat.

How to do it…

Here are the steps to compile and run a Spring web application:

  1. In pom.xml, add this boilerplate code under the project XML node. It will allow Maven to generate .war files without requiring a web.xml file:
    <build>
        <finalName>springwebapp</finalName>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.5</version>
          <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
          </configuration>
        </plugin>
      </plugins>
    </build>
  2. In Eclipse, in the left-hand side pane Package Explorer, select the springwebapp project folder. In the Run menu, select Run and choose Maven install or you can execute mvn clean install in a terminal at the root of the project folder. In both cases, a target folder will be generated with the springwebapp.war file in it.
  3. Copy the target/springwebapp.war file to Tomcat's webapps folder.
  4. Launch Tomcat.
  5. In a web browser, go to http://localhost:8080/springwebapp/hi to check whether it's working.
    How to do it…

How it works…

In pom.xml the boilerplate code prevents Maven from throwing an error because there's no web.xml file. A web.xml file was required in Java web applications; however, since Servlet specification 3.0 (implemented in Tomcat 7 and higher versions), it's not required anymore.

There's more…

On Mac OS and Linux, you can create a symbolic link in Tomcat's webapps folder pointing to the .war file in your project folder. For example:

ln -s ~/eclipse_workspace/spring_webapp/target/springwebapp.war ~/bin/apache-tomcat/webapps/springwebapp.war

So, when the.war file is updated in your project folder, Tomcat will detect that it has been modified and will reload the application automatically.

You have been reading a chapter from
SPRING COOKBOOK
Published in: May 2015
Publisher: Packt
ISBN-13: 9781783985807
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image