Download struts2 from http://struts.apache.org
Use necessary jars from strust2 framework downloaded if you work with Eclipse3.0 or above. (prefer Eclipse3.2)
Create Samples by using the following steps:
1) Create File - New - Project - Struts2_Sample
2) create necessary folders and files such as
src
package named com.apache.struts2 ( as per ur wish give name) under src
Action class under this package -- say LoginAction.java
lib -- copy necessary jar files.
jsp
create jsp files under jsp -- say Login.jsp, Success.jsp, Failure.jsp
config
create struts.xml file under config.
WEB-INF
web.xml under WEB-INF
build.xml
3) Right click -- Run As -- Ant Script (2nd one). it will display i dailog box.
4) Select war. It will create war under build directoy.
5) copy war file to tomcat/webapps/ directory.
6) start tomcat.
7) Type url in browser like http://localhost:8080/Struts2_Sample/jsp/Login.jsp
8) Now you have come to know about working of struts2.
Source Code:
LoginAction.java
package
com.apache.struts2;
import
com.opensymphony.xwork2.ActionSupport;
public
class LoginAction extends ActionSupport
{
/** **/ private static final long serialVersionUID = 1L; private String userName; private String password; public String execute()
{
if(getUserName().equals("Struts2")&& getPassword().equals("Sample")) return SUCCESS; else return ERROR;
}
public String getPassword() { return password;
}
public void setPassword(String password) { this.password = password;
}
public String getUserName() { return userName;
}
public void setUserName(String userName) { this.userName = userName;
}
}
Login.jsp
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="Login">
<s:textfield name="userName" label="User Name" />
<s:textfield name="password" label="Password" />
<s:submit value="Login" />
</s:form>
</body>
</html>
Success.jsp
<html>
<body>
Login Successfully.
</body>
</html>
Failure.jsp
html>
<body>
Login Failed.
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default" namespace="/">
<action name="Login" class="com.apache.struts2.LoginAction">
<result name="success">/jsp/Success.jsp</result>
<result name="error">/jsp/Failure.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Rarus Balkan</display-name>
<!-- BEGIN: copied from struts howto -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- END: copied from struts howto -->
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
build.xml
<project
name="Struts_Sample" default="war" basedir="."> <path id="classpath.path"> <pathelement location="${classes}"/> <pathelement location="${lib}"/> <fileset dir="lib"> <include name="*.jar"/> </fileset> </path> <target name="init"> <tstamp/> <property name="project" value="${ant.project.name}"/> <property name="build" value="build"/> <property name="src" value="src"/> <property name="classes" value="${build}/classes"/> <property name="lib" value="lib"/> <property name="jsp" value="jsp"/> <mkdir dir="${build}"/></target> <target name="clean" depends="init"> <delete includeemptydirs="true"> <fileset dir="${build}" includes="**/*"/></delete></target> <target name="compile" depends="init, clean"><mkdir dir="${classes}"/> <javac srcdir="${src}" destdir="${classes}" debuglevel="lines,source" debug="on" nowarn="on" fork="yes"> <classpath> <path refid="classpath.path"/> </classpath> </javac> </target> <target name="war" depends="compile"> <copy todir="${classes}" file="config/struts.xml"/><war destfile="${build}/${ant.project.name}.war" webxml="WEB-INF/web.xml" followsymlinks="on"> <classes dir="${classes}"/> <lib dir="${lib}"/><fileset dir="${basedir}" includes="jsp/**"/></war></target> <target name= "deploy" depends ="war"> <delete file="C:\tomcat-6.0.13\webapps\${ant.project.name}.war"/> <delete dir="C:\tomcat-6.0.13\webapps\${ant.project.name}"/> <copy toDir="C:\tomcat-6.0.13\webapps" file="${build}/${ant.project.name}.war" failonerror="false"/></target>
</project>
Here i have specified tomcat path to deploy my application. This will avoid deleting every time our application while copying new war manually. Antscript will do deletion of old war and updation of new war whenever we are executing. But when you are using this deploy target in build.xml you should stop tomcat before run ant script. Nothing will happen, it will create war file, but it doesnt copy to tomcat/webapps. Anyhow you have to stop tomcat before copying/updating the existing war file there.
I have attached the same sample here. Download it and include in Eclipse. Then run build.xml. Before that change tomcat path as in your system. This is a very simple example.