|
To explore the more powerful and flexible solutions, we will investigate the synergy between the Tiles and Struts frameworks. Struts is an open source framework for developing Web applications using the popular Model-View-Controller (MVC) or Model 2 architectural pattern. Struts comes with a large set of reusable tags for which the Tiles tag library makes an excellent enhancement. This article assumes prior familiarity with Struts 1.0. If you are familiar with Model-View-Controller (MVC)-based Web development but haven't used Struts before, you may want to read "Struts 2.0 in action" (JavaWorld, October 2007) to learn the basics of Java Web application development using the Struts framework. The Struts framework unarguably eases the development and maintenance of user interfaces for enterprise applications. However, after working with Struts on even a simple application, one quickly realizes the nightmare that is struts-config.xml. That file can quickly become unwieldy at best. When building an enterprise application, struts-config.xml can grow in excess of 500 action mappings, making it virtually unmanageable. Though covering all the aspects of this framework and documenting the best practices may not be possible in one article, the subsequent sections discuss some of the best practices for developing with Struts. In "Mix Protocols Transparently in Web Applications" (JavaWorld, February 2002), I proposed extending Struts to incorporate a solution for mixing the HTTP and HTTPS protocols in Web applications. In this article, I walk through the steps necessary to extend Struts in that way and leverage the framework to add even more features to our solution. In the book Hibernate in Action (Manning, October 2004), authors Christian Bauer and Gavin King reveal the paradigm mismatch between the two worlds of the object-oriented domain model and the relational database. Hibernate does an excellent job at the persistence layer of gluing these paradigms together; however, a mismatch remains between the domain model (the Model-View-Controller model layer) and HTML pages (the MVC view layer). In this article, we examine this mismatch and consider an approach for resolving the disparity. Unit testing is an important part of a good development process. Although there are many unit testing frameworks and technologies, such as JUnit, Cactus, EasyMock, HttpUnit, and so on, developers often find that it is difficult to unit test their Struts applications. Struts is a Model-View-Controller (MVC) framework compatible with Sun's J2EE platform and primarily based on servlet and JSP technology. The Struts framework has been around for about a year now and was originally conceived by Craig McClanahan. Struts is now part of the Jakarta project (which is part of ASF, the Apache Software Foundation). The official home for Struts is at http://jakarta.apache.org/struts. The framework has gained considerable attention over the course of the last year because of its ease of use and ability to fit today's developers' needs in building applications and building them fast. Struts combines Java servlets, Java Server Pages (JSP), custom tags, and message resources into a unified framework and saves the developer the time of coding an entire MVC model, a considerable task indeed. Developing portal sites without a framework in place can be a difficult job. Using templates can reduce the pain and help with sites where the content and layout can change in the blink of an eye. Struts can help you develop template-based portal sites, with the Struts Template tags. In part 1 in this series of book excerpts on using tiles from Programming Jakarta Struts, learn how to use tiles, and gain an understanding of templates. The Jakarta Struts framework has only been around for a short time, but the impact it has made for Web developers is significant. The framework is based on widely accepted design patterns and is very extensible. Although the learning curve for Struts is manageable, there are still best practices when using the framework. These lessons become apparent during development of any medium- to large-size development project. However, a much faster way to get up to speed is to leverage lessons learned by others in the Struts community. Several of those lessons are offered here and are designed to increase your productivity and efficiency when building applications using Struts. Programming Jakarta Struts: Using Tiles, Part 4 We conclude this book excerpt series on using tiles from Programming Jakarta Struts by showing you how to use definitions. You'll also find coverage on support for internationalization of tiles. Programming Jakarta Struts: Using Tiles, Part 3 In part 3 in this series of book excerpts on using tiles from Programming Jakarta Struts, learn how to use the tile tag library. Editor's Note: This is part two of a two-part series on Jakarta Struts 1.1. The first part is available here. Portions of this article are excerpted from Sue Spielman's book, The Struts Framework: Practical Guide for Java Programmers (Morgan-Kaufmann), one of the first books on the market covering Struts 1.1 in detail. You can reach Sue at . Programming Jakarta Struts: Using Tiles, Part 4 We conclude this book excerpt series on using tiles from Programming Jakarta Struts by showing you how to use definitions. You'll also find coverage on support for internationalization of tiles. What if you need to have the possibility for certain users of your application to delegate their actions to users under their control? What if you have just built a great human resources management application, but the human resources manager comes back to you and explains that he wants to be able to authorize different access levels to the employees in his department -- user X must be able to view Action A but cannot edit it, and user Y must be able to view and edit Action B but cannot be granted any authorization on Action B? Can you solve this kind of delegation of security responsibilities with Struts? Will you dive into your code and implement it programmatically, or is there a more generic way of adding this functionality to your application? There are two parts to this article. In this first part, we'll cover integration of Jakarta common libraries and multiple application support. The second part will hit the nested tag library, the plug-in API, and declarative exception handling. There have been some significant improvements to the framework, and chances are that you’ll want to start new development with this release. In fact, I would highly recommend doing just that. It has always been a goal of the committers on the Struts project to maintain backwards compatibility with v1.0.2, and they have done an excellent job in the v1.1 release. The last section of this article will point out some things to consider if you're upgrading from v1.0.2 applications to v1.1. In the first article, Introduction to Jakarta Struts Framework, I defined the Struts framework, discussed what it can accomplish, and provided an overview of the various components used throughout the framework. In the second article, Learning Jakarta Struts, I described building a simple sample application from scratch using Struts 1.0. This article will show you how to use the Struts tags to access the ApplicationResource file from a JSP. It should be pointed out that the Validator was originally created for use with the Jakarta Struts framework. The creator of the Validator, David Winterfeldt, was using Struts and realized that there was a great deal of redundancy in programming the same validation rules over and over again inside of the Struts ActionForm classes. He decided to create the Valdiator framework to eliminate that redundancy and subsequently, the Validator was born. 20. Extending Struts That means ActionServlet is responsible for handling all of your requests to Struts. You can create a sub-class of the ActionServlet class if you want to do something at application startup or shutdown or on every request, but you should try creating a PlugIn or RequestProcessor before extending the ActionServlet class. Before Servlet 1.1, the Tiles framework was based on extending the ActionServlet class to decorate a generated response. But from 1.1 on, it's used the TilesRequestProcessor class.This article assumes a working knowledge of JSP, Servlets, Custom Tag libraries, and XML. Additionally, I will be using a number of Jakarta projects throughout this article, including Tomcat 3.2.1, the servlet container used in the official reference implementation for the Java Servlet and JSP technologies (the latest version, 3.2.3, implements some security fixes to 3.2.1), and Ant, a Java-based build tool. A less well known fact is that the increasing adoption of Java 5 has brought in a third way forward for Struts. In this article I introduce Strecks, a set of Java 5 based extensions which offer a greatly streamlined programming model for Struts applications, without introducing any new compatibility issues. If you're confused about the name, think of it as a shortened, misspelled version of "Struts Extensions for Java 5". In Example 5, we use validation by annotation (another standard Struts 2 feature) to ensure that a message is entered. (Annotations, introduced in Java 5, are specified in the program source by using the @ symbol.) Struts 2 and the SmartURLs plugin use annotations as an alternative to XML gluecode. In Example 5, we use an annotation to "glue" a RequiredStringValidator to the message property. The core framework provides annotations for all the usual validators. Since the release of Struts 1.0, Struts has gradually become a de facto standard for MVC (a.k.a. Model-2) implementation for developing medium-to-large scale web-based applications on the Java platform. Struts fits nicely in the J2EE technology stack and fills the gap which is not addressed neither by the Servlet/JSP standard or the EJB standard. Giants like IBM and Oracle have started embracing and supporting Struts in their product set. In this article, Peter Pilgrim looks at the best practice of using a popular web application toolkit, The Expresso Framework. The Expresso Framework, until version 3, had its own Model View Controller engine. It now integrates Struts, further reducing the amount of code that developers need to write. As well as having an MVC framework, Struts also has its own powerful custom tag actions. Custom tag libraries and their powerful bean introspection core further reduce the total amount of Java scriptlets in Java Server Pages. The new chapter will include a solution to one of the most frequently heard complaints about Struts: that it doesn't provide a simple way to meaningfully nest POJO graphs in ActionForms. The provided example code solves this problem and includes an integrated solution for automating conversion, formatting, and validation. The Testing: Cactus and JUnit chapter examines pitfalls when building a real-world test set with these tools. It looks at various pitfalls such as 'Unreasonable Assert', 'Console-Based Testing', 'Unfocused Test Method', 'Failure to Isolate Subject' and others. The Struts TagLibs and JSPs chapter looks at common pitfalls such as 'Not Using Struts Tags for Error Messaging', 'Performing Business Logic in JSPs', and others. Examples and solutions are provided for each pitfall. TheServerSide and Manning Publications are pleased to present two feature chapters from the popular 'Struts In Action', a comprehensive book based on the practical experience of the core Struts team and its development community. Writes Craig McClanahan, the founding developer for Struts, "[In this book]...some of Struts' most important developers have done an outstanding job of explaining how Struts works and how it is used in practice. " The call sequence does not differ from the full-action case. The problem is, that Struts calls reset() and validate() methods again for the target action, and it populates form bean fields as well. To use form bean as a transport/command object we must prevent overwriting of form bean fields. Jonathan Lehr has taken over the updating of Rick Hightower's book Struts Live. In this excerpt , Jonathan takes you through Integrating ActionForms with POJOs. He writes that one of the complaints about Struts "is that unlike some of the newer web application frameworks (Spring, WebWork, JavaServer Faces, etc.), it can't deal directly with POJOs. As a result, people developing Struts applications often feel forced to spend a considerable amount of time and energy devising solutions to bridge the gap, usually by transferring values between instances of ActionForm and POJO graphs. Often there are translations that need to be performed that complicate this process and make for tedious work. [In] this chapter, we'll take a look at a new approach that solves the problem at its root and thereby eliminates the overhead of working with POJOs for Struts developers." By 2000, McClanahan was employed at Sun, as web application frameworks began to proliferate and the need for a standard became clear. JSR 127 was introduced in the Java Community Process (JCP) to solve the problem of creating base-level APIs for user interface components in web applications. JavaServer Faces 1.0 came into being in March 2004, with McClanahan serving as co-specification lead. With JavaServer Faces offering substantial improvements over Struts as an application framework, and the new release of Java Studio Creator providing a more productive development-time experience, many developers want to know how to best use all three. Writing code to validate Web-form input can be even more of a chore than implementing form-processing logic. But help is at hand, thanks to the Struts 2 framework. Oleg Mikheev looks under the hood of the Struts 2 validation mechanism and shows you how its Java, JavaScript, and Ajax support can take the pain out of Web-form validation. Ajax developers know that tables can do a lot more than display static information in an organized, readable format. By syncing up your table rows with a server-side database, you can create dynamic tables that sort, filter and paginate data on the fly. In this article, Sun Certified Java Developer Oleg Mikheev shows you how to use Struts 2, Dojo and JSON to display server-side data in an Ajax-style table. In the process, he introduces some of the Ajax-friendly features of Struts 2, including its integration with WebWork, Guice and the Dojo toolkit. This article helps developers who want to migrate their Struts applications to Spring MVC understand the logical mapping between the two frameworks and how to transform Struts applications into Spring MVC applications. Application developers with good Struts skills also will learn how key Struts concepts relate to Spring MVC concepts. Finally, this article should help architects understand and estimate the migration paths from Struts to Spring. To fully appreciate the subjects discussed, readers should have a working knowledge of the Struts framework. Keep Mindful of Your Tests Your foray into unit testing with StrutsTestCase should arm you with the knowledge necessary to begin outfitting your Web applications, whether Struts-based or not, with tests that improve robustness and accelerate the software development process. As you begin to use the testing frameworks described in my previous HttpUnit articles and this article, keep in mind that tests are only as good as the thought you put into them. You should exercise as much care with tests as with the application code. The first question in any decision about porting an application should be "Why not stay with the framework I have?" In my case, Struts was a stable, well-documented framework with a large developer community, but configuration was cumbersome and the separation of forms, actions, application flow, and validation sometimes made following a thread through an application like trying to untangle a knotted fishing line. This tendency only got worse as my Struts applications grew. Eventually, from a maintenance perspective alone, migrating to a new framework made sense. Along with developing JavaServer Faces itself (I am the specification lead for JSR-127), I have been working on just such an integration library for Struts (1.1 only; sorry in advance to 1.0 users). While not completed yet, it is clear that the goals stated in the previous paragraph are achievable with the current evolving design of JSF. While things can always change in the future, many JSR-127 expert group members consider high quality integration with Struts to be an important success factor for JavaServer Faces to be accepted. Therefore, I do not expect that JSF will evolve in ways that make this kind of integration difficult or impossible. The Apache Struts Project offers two major versions of the Struts framework. Struts 1 is recognized as the most popular web application framework for Java. The 1.x framework is mature, well-documented, and widely supported. Struts 1 is the best choice for teams who value proven solutions to common problems. JavaServer Faces: JSF technology makes it easier to build Web applications by letting users more easily assemble reusable UI components in a page, connect these components to an application data source, and wire client-generated events to server-side event handlers. JSF includes a set of APIs for representing UI components and managing their state, handling events and input validation, defining page navigation, and supporting internationalization and accessibility. It also includes a JSP custom tag library for expressing a JavaServer Faces interface within a JSP page. Learn more about JSF in the tutorial "UI development with JavaServer Faces" (see Resources). First, we'll explain the Struts components in the context of best practices and the role each one plays in your Web application development. While the multi-line selectbox typically provides a better look and feel when the choices are limited, a group of checkboxes is the better choice for any enterprise application where selection boxes must be rendered dynamically and contain preselection functionality. Fortunately, creating a group of dynamic checkboxes is easy to do with the Struts framework. The code in Listing 1 and Listing 2 isn't special or specific to a domain. It's simple password-change logic contained in tons of applications. If you're testing Struts legacy apps, you'll likely have to deal with password logic sometime, but how will you test it in a repeatable way? Of all the Web frameworks to come out of the past five years, Jakarta Struts is among the most popular ever to be used by Java? developers, so the arrival of its offspring is a noteworthy event. While Shale isn't yet the most popular framework -- or even the most known -- its pedigree is impressive. Even more exciting is the fact that Shale isn't just a significant upgrade or a new version release of Struts: it's a complete reworking of many of the core principles of Struts, along with the newest thinking in Web development. If you have worked on a large-scale Web application, you understand the term change. Model-View-Controller (MVC) is a design pattern put together to help control change. MVC decouples interface from business logic and data. Struts is an MVC implementation that uses Servlets 2.2 and JSP 1.1 tags, from the J2EE specifications, as part of the implementation. You may never implement a system with Struts, but looking at Struts may give you some ideas on your future Servlets and JSP implementations. To try out the security framework outlined here, you need a Web container that supports Servlets 2.3 or higher. Apache Tomcat works for this purpose; get the download info for it from Resources. The latest version of Tomcat has the required support for this security implementation. The framework also relies on the taglibs that are defined in Apache Struts; you can download this from Resources as well. Finally, if you want to look at the skeleton of the code samples given above, click the Code button at the top or bottom of this article. The sample zip file, wa-webappcode.zip, contains SecurityHelper.java, SecurityFilter.java, and SecurityCombo.java. Struts is an open-source framework for building Web applications based on the MVC pattern. Struts encourages application architectures based on the MVC pattern and provides services common to most Web applications. The Easy Struts plug-in manages all the complex configuration files, so you can focus on developing logic. Let's look at the 10 functions of Easy Struts that help you build a complete application: The advantages of integrating a Struts application into the Spring framework are manifold. First off, Spring was explicitly designed to resolve some of the real-world problems of JEE, such as complexity, poor performance, testability, and much more. Second, the Spring framework includes an AOP implementation that lets you apply aspect-oriented techniques to normal object-oriented code. Third, some might say that the Spring framework just handles Struts better than Struts handles itself. But that's a matter of opinion, so follow along as I demonstrate three approaches to integrating Struts applications into the Spring framework, and then decide for yourself. This recipe follows on the heels of what you learned in my last article, "Dynamic checkboxes with Struts." Like dynamic checkboxes, radio buttons with dynamically selected elements can add a lot of sophistication to your Web pages, and with Struts, they're also easy to create. I'll use a familiar search use case to illustrate the Struts-Velocity integration recipe. In this example, a simple application lets a user search for a book by its ISBN number. The application's results page presents a book matching the ISBN number. By using Struts, Tiles, and JavaServer Faces (JSF) together, developers can ensure a robust, well-presented Web application that is easy to manage and reuse. Struts and Tiles are aids for Web development, so you'll need to set up a Web container to experiment with them; the process for setting up Tomcat as your container and then the Struts and Tiles packages is described in the next section, in step-by-step fashion. These instructions also show you how to install this article's sample code. Once you've finished with that, you're ready to continue with the article. The Example 1 application doesn't take advantage of Struts and Tiles; it demonstrates the page-centric approach. By comparing it with Example 2, you'll see how much more structured and manageable Struts and Tiles can make your Web development. Finally, Example 3 demonstrates how straightforward it is to add functionality to a Struts and Tiles Web application that's already up and running. In the lesson entitled Alignment Properties and BoxLayout, Part 1, I recommended a list of Swing tutorials for you to study prior to embarking on a study of this set of lessons. To see how DynaBeans (and the Struts implementation, Dynaforms) work, let's begin with a simple Struts Form that records name, address, and telephone. Here's how it's currently implemented using ActionForm. There is no denying that there is a lot to learn in Struts 2. Changes have been numerous, but the influence of its predecessor is still evident throughout the framework. Most of the new features and capabilities provided by Struts 2, I think you will agree, make it a worthy successor. This article was intended to provide a brief comparison between Struts 1 and Struts 2. The source files from this article can be downloaded here.There is a good deal of information that this article did not cover and I strongly encourage you to visit the Struts 2 web site and peruse the tutorials and documentation available there to make the most out of the framework. This article lays out the steps for installing Struts and a basic "Hello World!" sample application 'from scratch.' It assumes that you are brand new to Struts and that you're also fairly new to Java ServerPages (JSP) and programming in general. Why not put it in the form's instantiator? Two reasons. First, because Struts is allowed to pool form objects. So, the form you get may not be newly instantiated; it may have been allocated from the pool, which means the instatiantor won't be run. Also, the instantiator doesn't take any arguments, so there's no good handle on the request to fetch the parameters from. On the other hand, the reset method does get handed the request as one of the arguments, so it's readily available. Here's your reset method: This is Chapter 11: Developing Applications with Tiles from the book Struts in Action (ISBN:1-93011-050-2), written by Ted N. Husted, Cedric Dumoulin, George Franciscus, and David Winterfeldt, published by Manning Publications Co.. In the previous installment of Succeeding with Struts, I alluded to the ability of DynaForms to dynamically size forms at run time. In other words, the ability to have a form that could be 5 rows long, or 10 rows, or 15 rows as needed. Perhaps a bit unwisely, I let the actual implementation of such a strategy as an exercise to the reader. In the following months, I've received dozens of inquires from readers looking for the dirty details, so this month I'll demonstrate not one, but two seperate ways to implement dynamically sizable forms. , I recommended a list of my earlier Swing tutorials for you to study prior to embarking on a study of this set of lessons. Welcome to the first in a series of articles on Jakarta Struts (or simply, "Struts"), the Java/JSP-based framework for building Web-based applications. While later articles will get deep into the technology behind Struts, this first article provides an introduction to Struts and evaluates the case for using it. It tries to cut through the technology and put its finger on the "value add" that Struts provides. In the previous lesson entitled Swing from A to Z: Demystifying Glue and Struts, Part 1, I introduced you to the output of a program that is designed to take the mystery out of glue and struts. I showed you some screen shots produced by that program. I promised that in this lesson, I would introduce you to the code from that program. A framework for Web development is a set of related classes, utilities, and modules that simplify a project's creation by providing pre-build parts. In this article, I will focus exclusively on Apache Struts J2EE framework, even though a cornucopia of other frameworks exists for any imaginable development problem. Struts is extremely flexible and is a good fit for most Web-based projects; however, if you find that, after reading this article (and perhaps a few other books on Struts), it is not a 100 percent match for your project, please look at another viable framework. I have described several in my previous article "Modern Java Frameworks for Web Development". I have also described WebWork framework in more detail in a separate article, "Working with the WebWork Framework". The list of some other frameworks is provided in the references section. In the previous lesson entitled Swing from A to Z: Demystifying Glue and Struts, Part 2, I developed three new methods for producing invisible components that can take the place of the glue and struts produced by the standard factory methods. This approach, however, is both inelegant and inflexible, in order to add another line you'd need to manually add three more form-properties, and change both your JSP form and Action to know about them. Luckily, Struts 1.1 offers a convenient way to both deal with this type of form, and to expand it dynamically (or even at runtime) without having to change any code or JSP. Developers often spend a lot of time manually handling conversions on a request-by-request basis. By leveraging type conversion features in Struts2 and other frameworks that provide them, developers can significantly reduce the amount of boiler plate code they need to write. |
w__w__w.ja__v__a_2__s__._c___om_ | Contact Us |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |