Monday, June 29, 2009

a basic application in Struts

Currently i am learning struts and i want to share some basics of it.Here i will tell how to create a basic struts application.

Struts is a free open-source framework for creating Java web applications.

Model-View-Controller (MVC) architecture:
The Model represents the business or database code, the View represents the page design code, and the Controller represents the navigational code. The Struts framework is designed to help developers create web applications that utilize a MVC architecture.(Learn MVC before learning Struts)

The framework provides three key components:

A "request" handler provided by the application developer that is mapped to a standard URI.

A "response" handler that transfers control to another resource which completes the response.

A tag library that helps developers create interactive form-based applications with server pages.

Setting Up Development Environment:

1)jdk 1.6 2)Tomcat 6.0 3)Eclipse

Step 1:-On Eclipse create new Web project and add Struts capabilities.(In myEclipse-->Click on myEclipse and project capabilities and add Struts capabilities)

Step 2:-Now create a New Form,action and JSP.

(select project-->new-->other-->MyEClipse-->WebStruts-->Struts 1.2-->Form,action,jsp)

Give name ,super class(ActionForm)

if you everything is right,it should be there.

Inside src:

1)A form bean:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
* MyEclipse Struts
* Creation date: 05-20-2009
*
* XDoclet definition:
* @struts.form name="myForm"
*/
public class MyForm extends ActionForm {
/*
* Generated fields
*/


/*
* Generated Methods
*/

/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}

/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/*here you will get getter and setter of Properties,which you gave*/


}

3)A property file is also there.

# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project StrutsDemo

key=value

2)Action class

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.MyForm;

/**
* MyEclipse Struts
* Creation date: 03-20-2009
*
* XDoclet definition:
* @struts.action path="/my" name="myForm" input="/form/my.jsp" scope="request" validate="true"
*/
public class MyAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

/* Write logic here or function which you want to perform*/
MyForm myForm = (MyForm) form;// TODO Auto-generated method stub


return mapping.findForward null;
}

}

Inside WEB-INF

3)Struts_config

I feel its the heart of struts.

Entries are given below:












attribute="myForm"
input="/form/my.jsp"
name="myForm"
path="/my"
scope="request"
type="com.yourcompany.struts.action.MyAction" >

name="success"
path="/Success.jsp"
redirect="true" />








4)web.xml


action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml


debug
3


detail
3

0


action
*.do


index.jsp

Step 3:Now you can run struts application.Give some fields in your form and try to print them in your JSP,which you will call.

For any mistake,sorry!!

and for query,plz ask.

Thanks

Deepak