This article describes the basic components of Struts and it is the continuation of the Introduction To Struts. libraries for working with view technologies other than JSP, and so on.
The Struts framework is a rich collection of Java libraries and can be broken down into the following major pieces:
- Base framework
- JSP tag libraries
- Tiles plugin
- Validator plugin
A brief description of each follows.
Base Framework
The base framework provides the core MVC functionality and is comprised of the building blocks for your application. At the foundation of the base framework is the Controller servlet: ActionServlet. The rest of the base framework is comprised of base classes that your application will extend and several utility classes. Most prominent among the base classes are the Action and ActionForm classes. These two classes are used extensively in all Struts applications. Action classes are used by ActionServlet to process specific requests. ActionForm classes are used to capture data from HTML forms and to be a conduit of data back to the View layer for page generation.
JSP Tag Libraries
Struts comes packaged with several JSP tag libraries for assisting with programming the View logic in JSPs. JSP tag libraries enable JSP authors to use HTML-like tags to represent functionality that is defined by a Java class.
Following is a listing of the libraries and their purpose:
- HTML Used to generate HTML forms that interact with the Struts APIs.
- Bean Used to work with Java bean objects in JSPs, such as accessing bean values.
- Logic Used to cleanly implement simple conditional logic in JSPs.
- Nested Used to allow arbitrary levels of nesting of the HTML, Bean, and Logic tags that otherwise do not work.
Tiles Plugin
Struts comes packaged with the Tiles subframework. Tiles is a rich JSP templating framework that facilitates the reuse of presentation (HTML) code. With Tiles, JSP pages can be broken up into individual ’tiles’ or pieces and then glued together to create one cohesive page. Similar to the design principles that the core Struts framework is built on, Tiles provides excellent reuse of View code. As of Struts 1.1, Tiles is part of and packaged with the core Struts download. Prior to Struts 1.1, Tiles was a third-party add-on, but has since been contributed to the project and is now more tightly integrated.
Validator Plugin
Struts comes packaged, as of version 1.1, with the Validator subframework for performing data validation. Validator provides a rich framework for performing data validation on both the server side and client side (browser). Each validation is configured in an outside XML file so that validations can easily be added to and removed from an application declaratively versus being hard-coded into the application. Similar to Tiles, prior to Struts 1.1, Validator was a third-party add-on, but has since been included in the project and is more tightly integrated.
Struts is the premier framework for building Java-based Web applications. Using the Model-View-Controller (MVC) design pattern, Struts solves many of the problems associated with developing high-performance, business-oriented Web applications that use Java servlets and JavaServer Pages. It is important to understand that Struts is more than just a programming convenience. Struts has fundamentally reshaped the way that Web programmers think about and structure a Web application. It is a technology that no Web programmer can afford to ignore.
MVC Framework:
Because an understanding of the Model-View-Controller architecture is crucial to understanding Struts, this section takes a closer look at each of its parts. As a point of interest, MVC is based on an older graphical user interface (GUI) design pattern that has been around for some time, with its origins in the Smalltalk world. Many of the same forces behind MVC for GUI development apply nicely to Web development.
- Model Components: In the MVC architecture, model components provide an interface to the data and/or services used by an application. This way, controller components don’t unnecessarily embed code for manipulating an application’s data. Instead, they communicate with the model components that perform the data access and manipulation. Thus, the model component provides the business logic. Model components come in many different forms and can be as simple as a basic Java bean or as intricate as Enterprise JavaBeans (EJBs) or Web services.
- View Components: View components are used in the MVC architecture to generate the response to the browser. Thus, a view component provides what the user sees. Often times the view components are simple JSPs or HTML pages. However, you can just as easily use WML or another view technology for this part of the architecture. This is one of the main design advantages of MVC. You can use any view technology that you’d like without impacting the Model (or business) layer of your application.
- Controller Components: At the core of the MVC architecture are the controller components. The Controller is typically a servlet that receives requests for the application and manages the flow of data between the Model layer and the View layer. Thus, it controls the way that the Model and View layers interact. The Controller often uses helper classes for delegating control over specific requests or processes
Brief History of Struts :
Struts was originally created by Craig R. McClanahan and then donated to the Jakarta project of the Apache Software Foundation (ASF) in 2000. In June of 2001, Struts 1.0 was released. Since then, many people have contributed both source code and documentation to the project and Struts has flourished. Today, Struts has become the de facto standard for building Web applications in Java and has been embraced throughout the Java community.
When Craig McClanahan donated Struts to the Apache Jakarta project, it became open source software. This means that anyone can download the source for Struts and modify that code as he or she sees fit. Struts is available free of charge and can be downloaded from the Apache Jakarta site at: http://jakarta.apache.org/struts/
One of the advantages of open source software is that bugs can be fixed in a timely fashion. For ASF projects, bugs are handled by the committers, but anyone can fix a bug and provide a patch that the committers will then evaluate and ‘commit’ if they deem it appropriate. Thus, open source enables rapid development and maintenance cycles. Being open source, Struts is completely free of charge and allows you to make changes to it without any consequence so long as you abide by and preserve the ASF license.
Support for Struts comes in three forms. First is the API documentation that comes with Struts. Second, Struts has a very active mailing list where you can get support for virtually any question. Third, several third-party consulting companies specialize in Struts support and development and also many books have been written on Struts framework.
This article is just an introduction of Struts project and framework, we will discuss about the working and component of the Struts framework in future articles.
Foreach is one of new features introduced in Java 5. I simply love this feature because it provides simplicity and readability and reduces the code. It is more powerful when used with generics (another feature added in Java 5). Lets check out basic syntax of foreach loop and compare it with traditional for loop.
//Traditional For Loop Syntax:
String []strings = {”For”, “each” , “loop”, “rocks”};
for (int i=0; i<strings.size(); ++i)
{
String s = (String) strings.get(i);
System.out.println(s);
}
//Foreach Loop Sytax:
String []strings = {”For”, “each” , “loop”, “rocks”};
for (String s : strings)
{
System.out.println(s);
}
Many developer think of Java foreach loop, is something like “foreach x in myList” but that is not the case. The same for keyword is used as shown in above code snippet. When you see a colon (:) read it as in. The loop above reads as for each String s in strings.
Lets take another example that uses generics and compare them with old for loop syntax. Java generics saves us from the burdon of type casting.
public class MyClass
{
String type = "MyClass";
int id = 10;
public String getType(){return type;}
public void setType(String type){this.type = type;}
public int getId(){return id;}
public void setId(int id){this.id = id;}
}
//Traditional For Loop Syntax:
ArrayList<MyClass> myObjects = getMyClassObjects(); //function returns an array list with myClass objects
MyClass myClass;
for (int i=0; i < myObjects.size(); i++)
{
myClass = myObjects.get(i);
//do something like
myClass.getType();
myClass.getId();
}
//another way using iterator
for(Iterator<MyClass> itr = myObjects.iterator(); itr.hasNext();)
{
//do something like
itr.next().getType();
itr.next().getId();
}
//Foreach Loop Sytax:
ArrayList<MyClass> myObjects = getMyClassObjects(); //function returns an array list with myClass objects
for(MyClass myClass: myObjects)
{
//do something like myType.getType();
myClass.getType();
myClass.getId();
}
As you can see, the foreach construct combines beautifully with generics. It preserves all of the type safety, while removing the remaining clutter. Because you don’t have to declare the iterator, you don’t have to provide a generic declaration for it. (The compiler does this for you behind your back, but you need not concern yourself with it.)
HashMap traversing was never so easy before foreach introduction, this is one the best uses of for each loop.
//Traditional For Loop Syntax:
HashMap m = new HashMap();
//code for filling the map
Iterator it = m.keySet().iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value = (Vector) m.get(key);
}
//Foreach Loop Sytax:
HashMap m = new HashMap();
//code for filling the map
for (String key : m.keySet()) {
Object value = m.get(key);
}
The foreach loop provides a simple, consistent solution for iterating arrays, collection classes, and even your own collections. It eliminates much of the repetitive code that you would otherwise require. The foreach loop eliminates the need for casting as well as some potential problems. The foreach loop is a nice new addition to Java that was long overdue. If you want to share your experience with me and other readers, please drop a comment.
Version 6 of the Java Platform, Standard Edition (Java SE), was released for general availability in December 2006. So here are the top 10 things you need to know about the release:
1. Web Services
All developers get first-class support for writing XML web service client applications. No messing with the plumbing: You can expose your APIs as .NET interoperable web services with a simple annotation. Not your style? Want to handle the XML directly? Knock yourself out: Java SE 6 adds new parsing and XML to Java object-mapping APIs, previously only available in Java EE platform implementations or the Java Web Services Pack.
2. Scripting
You can now mix in JavaScript technology source code, useful for prototyping. Also useful when you have teams with a variety of skill sets. More advanced developers can plug in their own scripting engines and mix their favorite scripting language in with Java code as they see fit.
Perhaps You ThougHt yOu couldN’t program with a scripting language and Java togetheR. Which will yoU Be trYing ?
3. Database
For a great out-of-the-box development experience with database applications, the Java SE 6 development kit – though not the Java Runtime Environment (JRE) – co-bundles the all-Java JDBC database, Java DB based on Apache Derby. No more need to find and configure your own JDBC database when developing a database application! Developers will also get the updated JDBC 4.0, a well-used API with many important improvements, such as special support for XML as an SQL datatype and better integration of Binary Large OBjects (BLOBs) and Character Large OBjects (CLOBs) into the APIs.
4. More Desktop APIs
Much has been said about this spoonful of sugar (to go with the Desktop team’s cake), so we will only skim a little. GUI developers get a large number of new tricks to play like the ever popular yet newly incorporated SwingWorker utility to help you with threading in GUI apps, JTable sorting and filtering, and a new facility for quick splash screens to quiet impatient users.
5. Monitoring and Management
The really big deal here is that you don’t need do anything special to the startup to be able to attach on demand with any of the monitoring and management tools in the Java SE platform. Java SE 6 adds yet more diagnostic information, and we co-bundled the infamous memory-heap analysis tool Jhat for forensic explorations of those core dumps.
6. Compiler Access
Really aimed at people who create tools for Java development and for frameworks like JavaServer Pages (JSP) or Personal Home Page construction kit (PHP) engines that need to generate a bunch of classes on demand, the compiler API opens up programmatic access to javac for in-process compilation of dynamically generated Java code. The compiler API is not directly intended for the everyday developer, but for those of you deafened by your screaming inner geek, roll up your sleeves and give it a try. And the rest of us will happily benefit from the tools and the improved Java frameworks that use this.
7. Pluggable Annotations
It is becoming a running joke in Java technology circles, at least some that contain us, that for every wished-for feature missing in Java technology, there’s a budding annotation that will solve the problem. Joke no more, because Java tool and framework vendors can put a different smile on your face, defining their own annotations and have core support for plugging in and executing the processors that do the heavy lifting that can make custom annotations so cool.
8. Desktop Deployment
Those of you deploying applications to the desktop will soon discover that it’s a tale of a large number of smaller changes that add up to a big difference to existing applications: better platform look-and-feel in Swing technology, LCD text rendering, and snappier GUI performance overall. Java applications can integrate better with the native platform with things like new access to the platform’s System Tray and Start menu. At long last, Java SE 6 unifies the Java Plug-in technology and Java WebStart engines, which just makes sense. Installation of the Java WebStart application got a much needed makeover.
9. Security
You can have all the security features you like in the platform  and this release adds a few more, like the XML-Digital Signature (XML-DSIG) APIs for creating and manipulating digital signatures  but if you don’t have well supported security administrators, your security may be at risk. So Java SE 6 has simplified the job of its security administrators by providing various new ways to access platform-native security services, such as native Public Key Infrastructure (PKI) and cryptographic services on Microsoft Windows for secure authentication and communication, Java Generic Security Services (Java GSS) and Kerberos services for authentication, and access to LDAP servers for authenticating users.
10. The -lities: Quality, Compatibility, Stability
You probably knew that Sun has done regular feature releases of the Java SE platform over the last 10 years. So we certainly feel like we’ve built up some expertise in this area, such as the ever growing 80,000 test cases and several million lines of code testing conformance (being just one aspect of our testing activity). You probably noticed that, unlike the last release, people have been downloading binary snapshots for the last 20 (not just 6) months. And what’s more, they’ve been filing bugs. So, before we even got to beta, we’d fixed a number of quality and regression issues. Doesn’t that add up to a better product? Oh, and by the way, performance is looking better than J2SE 5.0.
Sun Microsystems kicked off its JavaOne conference by announcing two upcoming Java software products JavaFX Script for writing interactive Web sites and JavaFX Mobile for running software on mobile phones designed to persuade the world’s 6 million Java developers to use the technology to link older Java systems with newer software.
Rich Green, executive vice president of Sun Software, announced JavaFX Mobile, a mobile phone software system available via OEM license to carriers, content owners and consumer electronics manufacturers, and JavaFX Script, a new scripting language for rich internet applications, targeted at creative individuals.
Gosling, showing off JavaFX Script with creator Chris Oliver, claimed JavaFX Script would help developers produce “buttons and sliders gone wild. Gosling and Oliver proceeded to show a website featuring exactly the same buttons, tabs and fades as any other site online today using AJAX or HTML, built in just “days.”
JavaFX Mobile demo used two Nokia handsets with a suspiciously Apple iPhone touch-screen interface and buttons for the standard search, messaging, calendar and phone functions, which at one point went dead and failed to reboot. Green described the device as “serious eye candy”.
Sun released the early alpha version of JavaFX Script today. JavaFX Script applications will run on any JavaSE technology-based platform and Sun plans to enhance the JavaFX family with content tools, widgets and other offerings over time. All JavaFX software, like all Java software at Sun, will be available to the open source community via the GNU General Public License (GPL) license.
Java language was developed by Sun Microsystems, Inc, in 1991. The language was initially called OAK but was renamed Java in 1995. The original impetus for Javawas not the Internet! Instead, the primary motivation was the need for a plaform-independent (i.e architectural neutral) language that could be used to create software to be embedded in various consumer electronic devices, such as microwave ovens and remote controls.
The Java programming language lets you write powerful, enterprise-worthy programs that run in the browser, from the desktop, on a server, or on a consumer device. Java programs are run on — interpreted by — another program called the Java Virtual Machine (Java VM). Rather than running directly on the native operating system, the program is interpreted by the Java VM for the native operating system. This means that any computer system with the Java VM installed can run a Java program regardless of the computer system on which the application was originally developed.
The Java platform is a software-only platform that runs on top of other hardware-based platforms. Because hardware-based platforms vary in their storage, memory, network connection, and computing power capabilities, specialized Java platforms are available to address applications development for and deployment to those different environments.
Java technology has grown to include the portfolio of specialized platforms listed below. Each platform is based on a Java VM that has been ported to the target hardware environment. This means, for example, in the case of Desktop Java, desktop applications written in the Java programming language can run on any Java VM-enabled desktop without modification.
Java 2 Platform, Standard Edition (J2SE), provides an environment for Core Java and Desktop Java applications development, and is the basis for Java 2 Platform, Enterprise Edition (J2EE) and Java Web Services technologies. It has the compiler, tools, runtimes, and Java APIs that let you write, test, deploy, and run applets and applications.
Java 2 Platform, Enterprise Edition (J2EE), defines the standard for developing component-based multitier enterprise applications. It is based on J2SE and provides additional services, tools, and APIs to support simplified enterprise applications development.
Java 2 Platform, Micro Edition (J2ME), is a set of technologies and specifications targeted at consumer and embedded devices, such as mobile phones, personal digital assistants (PDA’s), printers, and TV set-top boxes.
Java Card technology adapts the Java platform to enable smart cards and other intelligent devices with limited memory and processing capabilities to benefit from many of the advantages of Java technology.
OSJP.org is a place for discussing Open Source Java Ps. What are Ps, Ps basically represents following areas:
- Patterns & Practices: Discussion about Java design patterns and design practices. What are the advantages of using best practices and design patterns.
- Performace & Profiling: How to improve performace and testing Java application for better response.
- Persistence & ORM: To discuss the Java persistence and Object Relational Mapping (ORM).
- Platform: Platform discusses about J2SE, J2EE and J2ME developments and issues related to Java platform.
- Programming: All about Java programming, discuss what are the common problems that later become programmer’s nightmares.
- Projects: Open Source Java Projects will discuss the new and old java projects that must be part of your applications and other trouble shootings related to these open source projects.
Lets get started then.