Wednesday, May 15, 2013

Struts 2 - HelloWorld

Download Struts 2 from struts downloads, and unzip, for example, C:\struts-2.3.14. Create dynamic web project in Eclipse.

Web Application File Structure
/WebContent/
/WebContent/META-INF/
/WebContent/WEB-INF/
/WebContent/WEB-INF/classes/struts.xml
/WebContent/WEB-INF/lib/
/WebContent/WEB-INF/lib/minimum JARs + any plugin JARs + plugin dependencies
/WebContent/WEB-INF/web.xml

See the structure as below -
























The following files are a minium requirement for your application.
FilenameDescription
struts2-core.jarFramework library itself, found in distribution root directory
xwork.jarXWork 2 library on which Struts 2 is built (version 2.0 or later)
ognl.jarObject Graph Navigation Language (OGNL), the expression language used throughout the framework
freemarker.jarAll UI tag templates are written in Freemarker (also a good option for your own views)
commons-logging.jarCommons logging, which the framework uses to support transparently logging to either Log4J or JDK 1.4+
commons-fileupload.jarThe Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.
commons-io.jarCommons IO is a library of utilities to assist with developing IO functionality.
web.xmlJava web application configuration file that defines the filters (and other components) for your web application
struts.xmlFramework configuration file that defines the actions, results, and interceptors for your application

Copy to the WEB-INF/lib directory
  • the required JARs
  • any Struts plugin JARs,
  • any plugin dependencies.

Create ../WEB-INF/classes/struts.xml -

    
    
    
        
            /index.jsp
        
        
            /HelloWorld.jsp
        
     


Snip of web.xml -

    My Application
    
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    
    
        struts2
        /*
    


Action Class HelloWorld -
package com.mqin.struts2.demo;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {

    public static final String MESSAGE = "Struts is up and running ...";

    public String execute() throws Exception {
        setMessage(MESSAGE);
        return SUCCESS;
    }

    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }
}


Running Result -

No comments:

Post a Comment