Thursday 23 February 2012

BEGINNING STRUTS2

I began with struts2-blank.war  in the Struts distribution.
I placed this war file tomcat6  /webapps folder.
Then I started tomcat6.
( d:\tomcat6\bin>tomcat6)
--------
I expected the blank.war to be automatically expanded by tomcat.
So, I shutdown tomcat6.
( just ctrl+c)
---------
  Now, I explored tomcat6\webapps folder.
As expected, I found  struts2-blank FOLDER.
---
I copied this folder to the desktop.
Then, I renamed it as 'myapp'.
After renaming, I copied and pasted it in
tomcat6\webapps  folder.
---
I started tomcat6 again.
--
In the browser, I typed the URL as
'http://localhost:8080/myapp/

I got the following display.
( after some time 'loading')
----------------------------
Struts is up and running ...
Languages

    English
    Espanol
--------------------------
So, my context is working.

 I have a background in Struts1.1
So, I wanted to see where Struts2 differs in deployment.

The sample makes use of package 'example';.
As usual, web.xml is directly under WEB-INF

But, they have placed struts.xml  inside classes folder.
This is important.

Also, I found  two xml files inside the classes folder.

One file has been given name as 'struts.xml'.
The other file has been named 'example.xml'
My package name is going to be 'mypack'.
I renamed 'example.xml' as 'myapp.xml'.

I renamed 'example'  folder as 'mypack'.
Then I removed all the  files already found there.

Now , I am ready!
===============
I am in g: drive

g:>md struts2example

g:>cd struts2example

  >md mypack
  >cd  mypack

 >edit  setpath.bat
set path=c:\windows\command;c:\jdk1.5\bin

>edit  setcpath.bat

set  classpath=g:\struts2example;commons-logging-1.0.4.jar;ognl-2.6.11.jar;
xwork-2.0.7.jar;freemarker-2.3.8.jar;struts2-core-2.0.14.jar

( I found these jars in struts2-blank\WEB-INF\lib)

>edit  demoAction.java

package  mypack;

import com.opensymphony.xwork2.ActionSupport;

public class demoAction  extends 
                        ActionSupport
{
String     username=null;
String     password=null;

public void setUsername  (String  s)
          {username=s;}

 public String getUsername()
          {return   username;}

 public  void  setPassword(String s)
           {password=s;}
 public String getPassword()
                {return   password;}

 public  String  execute () 
              throws    Exception
 {
  String s1=  getUsername();
  String s2=  getPassword();

  if(!s1.equals("sam")||!s2.equals("admin"))
  {

  addActionError
("Invalid  user  name  or password! please try again.");
  return ERROR;
  }

  else
  {
  return SUCCESS;
  }
 }

}
============================================

>setpath
>setcpath
>javac demoAction.java


 copy the   class  file to  tomcat6\webapps\myapp\WEB-INF\classes\mypack  (as   usual)

>edit  demoInput.jsp

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

  <s:form  namespace="/mypack"  action="demoAction"  method="POST">
    <s:actionerror />
    <s:fielderror  />

    <s:textfield   name="username" 
                   label="user name" />

    <s:password   name="password" 
                  label="password" />

    <s:submit  value="Login" />

  </s:form>

 </body>

</html>

-----------------------------

>edit  demoOutput.jsp

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

<html>

  <body>

    Welcome... 
    <s:property     value="username"  />

  </body>

</html>

-------------------------------------

open a new command  then go to

d:\tomcat6\webapps\myapp\WEB-INF\classes 

rename example.xml   as 'myapp.xml'

then  edit that file as follows

<?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="mypack"   
         namespace="/mypack"
         extends="struts-default">

        <action name="demoAction"
                class="mypack.demoAction">

     <result>/demoOutput.jsp</result>
   <result name="error">/demoInput.jsp
     </result>
        </action>

    </package>
</struts>

then  goto  struts.xml  which is available  in same directory(classes)

>edit  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>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />

    <include file="myapp.xml"/>



</struts>



Now, I am ready to test the program.
I started tomcat6.
In the browser, I typed the address as


I got two textfields.
I typed ‘sam’ in text1 and ‘admin’ in text2 and submitted.

There was a slight delay and then I got output as welcome sam.

   Them I typed tom in text1 and ‘admin’ in text2 and submitted.



I think, this procedure is easy to execute and can be followed for advanced experiments.