Thứ Bảy, 5 tháng 5, 2012

Developing a Struts2 application step by step

Introduction
Struts is a well-organized framework based on MVC architecture. In Model-View-Controller Architecture, Model stands for the business or database code, View represents the page design code and the Controller for navigational code. All these together makes Struts an essential framework for building Java applications. But with the development of new and lightweight MVC based frameworks like Spring, Stripes and Tapestry, it becomes necessary to modify the Struts framework. So, the team of Apache Struts and another J2EE framework, WebWork of OpenSymphony joined hand together to develop an advanced framework with all possible developing features that will make it developer and user friendly.

The strut-2 framework is designed for the compilation of the entire development cycle including of building, developing and maintaining the whole application. It is very extensible as each class of the framework is based on an Interface and all the base classes are given an extra application and even you can add your own. The basic platform requirements are Servlet API 2.4, JSP API 2.0 and Java 5.
Struts 2 Architecture
The following diagram depicts the architecture of Struts 2 Framework and also shows the initial request goes to the servlet container such as tomcat, which is then passed through standard filer chain.
clip_image001

Thứ Sáu, 4 tháng 5, 2012

Application-3 : Creating a Simple onLineDraw Application with Struts

Creating a Simple OnlineDraw Application with Struts
In this chapter, we have create a very simple onLineDraw application with struts. This application design inNetBeans IDE- 6.8  with Struts 1.3.
Requirement  for your struts application-
  • At least one view component is required in every Application.
  • A subclass of ActionForm is used to submit the date as application needed.
  • A subclass of Action class which provide specific processing functionality for the application.
  • A web.xml file is required for calling ActionServlet and struts-config.xml.
  • A struts-config.xml file is required for updating and mapping the update.
Now, Following the following steps for develop your Third application name as onLineDraw. onLineDraw application contain following page-
  • Four View file name as index.jsp, welcomeStruts.jsp, Register.jsp and success.jsp
  • One JavaBeans file name as LoginForm.java, used for store data as application need.
  • one Action file name as LoginAction.java, used for provide specific processing functionality.
  • Two XML file name as web.xml and struts-config.xml file, used for provide control, mapping and validation in application.
Step- 1: Start your IDE (NetBeans 6.8 with struts 1.3).
Step- 2: Open a new project( Crtl+Shift+N), name it , add server( Apache Tomcat 6.0.20 ), then add struts framework 1.3 , and don't forget to add Struts TLDs.
When you click Finish button, NetBeans IDE put and configure everything in place and project open in your IDE.

Step- 3:  Open welcome.jsp page from your struts application and code it as describe below.

Application-2 : Creating a Simple Login Application with Struts

Creating a Simple Login Application with Struts
In this chapter, we have create a very simple Login application, user can be login( provide own detail) which store into JavaBean Class and retrieve into another file. For each piece of the application, we show you the code and then explain the code. This application design in NetBeans IDE- 6.8  with Struts 1.3.
Requirement  for your struts application-
  • At least one view component is required in every Application.
  • A subclass of ActionForm is used to submit the date as application needed.
  • A subclass of Action class which provide specific processing functionality for the application.
  • A web.xml file is required for calling ActionServlet and struts-config.xml.
  • A struts-config.xml file is required for updating and mapping the update.

Application-1 : Creating a Simple Web Application with NetBeans IDE in Struts

Creating a Simple Web Application with Struts
In this chapter, you create a simple Struts application from start to finish. In such application, your message is successfully forward to another page. When your message successfully submit  then struts-config.xml find the forward file name and path,, forward your request to that page and your message will be display on that page.  For each piece of the application, we show you the code and then explain the code. This application design in NetBeans IDE- 6.8  with Struts 1.3.
Requirement  for your First struts application-
  • At least one view component is required in every Application.
  • A subclass of org.apache.struts.action.ActionForm is used to submit the date as application needed.
  • A subclass of org.apache.struts.action.Action class which provide specific processing functionality for the application.
  • A web.xml file is required for calling ActionServlet, TagLibrary and struts-config.xml.
  • A struts-config.xml file is required for updating, forward and mapping the update.
 Let's Start the application, this application provide a good understanding how struts framework works and also understanding the development of Struts applications.
Now, Following the following steps for develop your first application name as StrutsTest. StrutsTest application contain following page-

Building a sample Web App with STRUTS part 3

8. Now the final piece - lets write the TestResult.jsp: Here is the code for that:
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<jsp:useBean id="TestFormBean" scope="request" 

type="com.openstack.struts.testapp.TestFormBean"/>

<html:html>
<head>
<title>test results</title>
</head>
<body bgcolor="white">
<h3><jsp:getProperty name="TestFormBean"  


property="ipString"/></h3>
</body>
</html:html>



Looking at the JSP, it defines a bean instance and uses it to print a bean property ipString:
<bean:write name="TestFormBean"  property="ipString"/>
This prints the changed ipString as part of "ActionHandler".
9. Now we are ready to test. Click on compile all from the main menu, just to make sure that we have all our java code compiled. Right click on theTestFormPage.jsp and click on Execute. Depending on your settings NetBeans might give some warning like HTTP server could not be started etc. Click OK till it displays a browser window. Soon your page should show up.
10. Enter your name and click submit. The next page should come with a welcome message!!!

Building a sample Web App with STRUTS part 2

6. Let us compile this java file. First let us set a parameter in NetBeans - to set target directory where the java files will be compiled (classes will be stored). Go to tools - options compiler types- external compilation (the external compiler, javac is typically the default compiler. Set the target directory to StrutsNetBeansSample /WEB-INF/classes. Click on the file (TestFormBean.java) in the explorer window and click build - > compile from main menu. The o/p window should show a message like 'Finished TestFormBean. Now we have a form bean we need an Action class that will take this form bean - use the ipString entered by user to pass to the next page.
7. Lets create the TestActionHandler.java under src/com/openstack/struts/testapp. Here is the class definition:

Building a sample Web App with STRUTS

Prerequisites:

We will assume that as a reader you are familiar with web application development, specifically using JSP and Java.

Preparation:

We need to install NetBeans IDE (and JDK needed for the IDE). You can install NetBeans from www.netbeans.org. Or click here for simple instructions on how to install the IDE.

Quick overview:

We will create a very simple web application just to demonstrate screen flow and action handling using STRUTS. We will use NetBeans to create the JSP aNDjava files, use tools such as auto generation of getters and setters and finally use the IDE to compile and test the app. NetBeans comes with integrated TomCat. 

Giới thiệu về tôi

Ảnh của tôi
I'm a Software developer, an expert in Objective-C, PHP,Java. My current projects are for iOS, Mac OS X, Microsoft Windows, Microsoft Windows Mobile/CE.