Saturday, December 6, 2008

Create Struts Project

How To Develop Struts


Struts framework


Introduction:

The Jakarta Struts project, an open−source project sponsored by the Apache Software Foundation, is a server−side Java implementation of the Model−View−Controller (MVC) design pattern. The Struts project was originally created by Craig McClanahan in May 2000.

Requirements:

1. Eclipse3.2

2. MyEclipse5.1


Note: Eclipse without MyEclipse - Used for running only java application.


To run struts web application, we extract the MyEclipse into the Eclipse Folder.

Componenets


There are some components that are needed to develop the struts application.

1. Actionform

2. Action

3. Struts-config.xml



STEPS


Step 1:

Open Eclipse3.2 and go to File----Project----MyEclipse----web Project and click Next. Give project Name(BlogPro) and click Finish. Here Your Project(example BlogPro) is created.


Step 2:

Add Struts capabilities. Right click on ProjectName(BlogPro). Select MyEclicpse---Add struts capabilities and click Finish.

Step 3:

Create ActionForm. Right click on Project Name(BlogPro). New---Other----web-struts---struts1.1---struts 1.1 form.----New Form window open---give form name in usecase(must start lower letter). Select org.apache.struts.action.ActionForm in super class. If you want to give form fields ,we specify the field (Optional Detail) Form properties Add and create property window opened. There give name and select type. Add more fields like this if desire. And click finish.

Step 4:

Create Action. Right click on Project Name(BlogPro). New---Other----web-struts---struts1.1---struts1.1 action. New Action window Opened. There Give action name and select org.apache.struts.action.Action in superclass. And click finish.

Step 5:

Open Action Servlet like (src—com.yourcompany.struts.action).

public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)
{
//create object for actionform

Loginform sform=(Loginform)form;

//check validation

If((sform.getUsername.equals(“super”))
{
Request.setAttribute(“Login”,sform);

return mapping.findForward("success");
}
else
{
request.setAttribute(“Login”,sform);
return mapping.findForward(“Failure”);
}
}

Note:

we get form field values through actionform. so create object for actionform and use this object to retrieve form filed.

Step 6:

deploy deployment descriptor file.( struts-config.xml)


webroot---WEB-INF—struts-config.xml

we give name (form bean name displayed above form-bean tag) in action tag

step 7:

finally create two jsp file have to create.

success.jsp




success





failure.jsp




failure




How To Run


Enter url of the Jsp loginform.jsp. the control goes to web.xml.




action
*.do



check servlet-name in servlet-mapping with servlet-name in servlet(above)

true control goes to action servlet.

check username=”super” true control forward into struts-config.xml--

it invokes success.jsp file.

otherwise it Is false control forward into struts-config.xml--


it invokes failure.jsp file.



Note:

It is very easy to learn struts. Here after I am going to write struts framework continuously.

You have any doubt please send post your question to me. Through post.


Next Post Is Form Validation

by yours

S.Sureshkumar,M.Sc,M.Phil

No comments: