Struts 2 QuickStart on Ubuntu 16.04 Xenial LTS Linux Easy Guide

March 10th, 2017 by thelinuxevangelist

Getting-Started with Struts 2 on Ubuntu




Hello Ubuntu User! This Tutorial Shows You Step-by-Step How-to Install and Getting-Started with a Struts 2 Hello-World Java Web Application Ubuntu 16.04 Xenial Xerus LTS 32/64bit GNU/Linux.

The first Benefit of Using Struts is that you don’t have to write a Controller and can concentrate on writing Business Logic in action classes.

Struts 2 Features List:

  • Struts provides a Filter Dispatcher, saving you writing one.
  • Struts employs an XML-based Configuration file to match URIs with actions.
  • Struts instantiates the Action Class and populates Action Properties with user inputs. If you don’t specify an action class, a default action class will be instantiated.
  • Struts Validates user input and redirects user back to the input form if validation failed. Input validation is optional and can be done programmatically or declaratively. On top of that, Struts provides Built-in Validators for most of the tasks you may encounter when building a web application.
  • Struts invokes the Action Method and you can change the method for an action through the Configuration File.
  • Struts examines the Action Result and executes the result. The most common result type, Dispatcher, forwards control to a JSP. However, Struts comes with various result types that allow you to do things differently, such as generate a PDF, redirect to an external resource, send an error message, etc.

This Tutorial make Use of the Maven Java Tool Facility to Getting-Started Easy with a Basic Struts2 Web App on Ubuntu Linux.

Struts 2 can Use either an XML Configuration file or Java Annotations (or both) to specify the relationship between a URL, a Java class, and a view page (such as index.jsp).

Struts 2 Quick-Start on Ubuntu 16.04 Xenial LTS Linux - Featured
  1. Open a Command Line Terminal Window
    Ctrl+Alt+t on desktop
    (Press “Enter” to Execute Commands)

    Struts 2 Quick-Start on Ubuntu 16.04 Xenial LTS Linux - Open Terminal
  2. Download All Apache Struts 2 Release

    Get Struts2 All .zip
  3. Double-Click on Archive and Extract into Home

     - Extract Apache Struts 2
    Or from Command Line:

    unzip -d $HOME/struts2 struts-2*-all.zip
  4. Relocate Struts 2

    mv $HOME/struts2* $HOME/struts2
  5. Access Target Directory

    cd $HOME/struts2
  6. Check if Maven 3+ is Setup on Ubuntu

    mvn -version

    How-to Install Latest Apache Maven on Ubuntu

  7. Generate a Maven Standard Java Web App Project:

    Maven Create Web App Project
  8. Edit the pom.xml Config File

    cd myWebApp
    nano pom.xml

    Modify the “build” Entry like:

    <build>
      <finalName>basic_struts</finalName>
    </build>
    

    Ctrl+x to Save & Exit from nano :)

  9. Add Struts 2 Jar Files To Class Path

    nano ./pom.xml

    Include in the “dependencies” Section:

    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>[X.X.X.X]</version>
    </dependency>
    

    Just Subsitute to X.X.X.X the Suited Struts 2 Version (May simply Look the Latest Downloaded Struts Release!)

  10. Building and Packaging to Easy Deploy on the Servlet Container

    mvn clean package

    Find the Artifact Product Ready to Deploy on:

    ./target/basic_struts.war
  11. How-to Deploy a Servelet on Apache Tomcat Container:

    Tomcat Easy Deploying Guide
  12. Then You can Browse the Struts2 StartUp Hello-World

    http://localhost:8080/basic_struts/
  13. Add Logging

    This to see what’s happening under the hood…

    nano ./pom.xml

    Include in the “dependencies” Section:

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
    </dependency>
    

    Then Create a Log4J Config File

    nano ./src/main/resources/log4j.xml

    Append:

    
    
     
    
       
      
        
         
        
      
     
      
      
        
      
     
      
         
      
      
      
       
        
        
       
       
     
    
  14. Add a Struts 2 Servlet Filter

    nano ./src/main/webapp/WEB-INF/web.xml

    Insert:

    
     struts2
     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
     struts2
      /*
    
    
  15. Struts 2 App Minimal XML Configuration

    nano ./src/main/resources/struts.xml

    Append:

    
    
    
      
      
        
          /index.jsp
        
      
    
    
  16. Building and Packaging Again

    mvn clean package
  17. Deploy and Access the Final Struts2 Hello-World App

    http://localhost:8080/basic_struts/
(Visited 24 times, 1 visits today)
Share on Tumblr

Tags: , , , , , , , , , , , , ,


Comments are disabled