Monday, August 24, 2009

Struts 2 Hello World Example

I had seen many new deveopers struggling against struts2 hello world example. So I decided to write a small example.
Below are the required libraries to run this example which are easily availabel

struts2-core-2.0.11
xwork-2.0.4
commons-logging-1.0.4
commons-logging-api-1.1
freemarker-2.3.8
ognl-2.6.11

The structure of the applictaion which I am following is (Eclipse IDE)
Struts2Demo
|---src
| |----org
| | |----vinod
| | | |----action
| | | | |----HelloWorld।java
|---struts.xml

|---WebContent
| |---jsp
| |---HelloWorld।jsp
|---index.jsp
|---WEB-INF
| |---lib
| |---web.xml


It is true that different IDE's use different structure, but at last when war is build they follow same structure.
Lets start...

HelloWorld.java

package org.vinod.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{

String greetings = null;

public String execute() throws Exception {
setGreetings("Hello World");
return SUCCESS;
}

/**
* @return the greetings
*/
public String getGreetings() {
return greetings;
}

/**
* @param greetings the greetings to set
*/
public void setGreetings(String greetings) {
this.greetings = greetings;
}

}

HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="s" uri="/struts-tags" %>



Struts 2 Example






index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="s" uri="/struts-tags" %>



Struts 2 Example






struts.xml

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">


class="org.vinod.action.HelloWorld">
/jsp/HelloWorld.jsp





web.xml




struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*


index.jsp

No comments: