Open source Java EE framework JBoss Seam details

Source: Internet
Author: User
Tags form input text http post relational database table sessions jboss tomcat server jbpm

Summary: JBoss seam is "a lightweight framework for Java EE 5.0". What does that mean? is Java EE (Enterprise Edition) 5.0 not a set of "frameworks" in itself? Why is there a need for another framework outside of the official norm? Well, let's think of seam as a "missing frame" that should have been included in Java EE 5.0. It is on the upper level of the Java EE 5.0 framework, providing a unified, easy-to-understand programming model for all components in enterprise Web applications. It also makes development of state-based applications and business-process-driven applications a breeze. In other words, seam is committed to developer productivity and application extensibility.

JBoss seam is "a lightweight framework for Java EE 5.0". What does that mean? is Java EE (Enterprise Edition) 5.0 not a set of "frameworks" in itself? Why is there a need for another framework outside of the official norm? Well, let's think of seam as a "missing frame" that should have been included in Java EE 5.0. It is on the upper level of the Java EE 5.0 framework, providing a unified, easy-to-understand programming model for all components in enterprise Web applications. It also makes development of state-based applications and business-process-driven applications a breeze. In other words, seam is committed to developer productivity and application extensibility.
Open source Java EE framework JBoss Seam details

    1. Consolidating and hardening the Java EE framework
      The core framework of Java EE5.0 is EJB (Enterprise JavaBeans) 3.0 and JSF (JavaServer Faces) 1.2. EJB 3.0 (hereinafter referred to as EJB3) is a lightweight framework for business services and database persistence based on a pojo (Plain old Java Objects). JSF is an MVC (Model-view-controller)-based Web application framework. Most Web apps will contain EJB3 components with business logic and JSF components displayed on the front end of the web App. EJB3 and JSF are complementary, but they are independent frameworks designed according to their respective concepts. For example, EJB3 uses annotations (annotation) to configure the service, and JSF uses an XML file. Further, EJB3 and JSF components are non-sensitive at the frame level. To integrate EJB3 and JSF, developers must manually construct facade objects (such as JSF support beans), and link business components to Web pages and boilerplate code (also known as plumbing code) so that methods can be called across the framework. Gluing these technologies together is one of seam's responsibilities.

Seam breaks the artificial layer between EJB3 and JSF, which provides a consistent, annotation-based approach to consolidating EJB3 and JSF. With only a few simple annotations, the EJB3 business component in seam can be used directly to support JSF Web Forms or to handle Web UI events. Seam allows developers to apply the "same thing"-annotated pojos--to all application components. In contrast to applications developed by other web frameworks, seam application concepts are concise and the same functionality requires less code (in Java and XML). If you have no patience, or want a quick preview, how easy is a seam, you can now look at the Hello World described in this article.

In the JSP difficult task, seam can be easily completed. For example, one of the problems with JSF headaches is being overly reliant on HTTP POST. This makes it difficult to add a JSF Web page to a bookmark, which is accessed via HTTP GET. But with seam, it's easy to build a rest page. Seam provides a series of JSF component tags and annotations that add Web-friendly and page efficiency to JSF apps.

At the same time, seam expands the EJB3-to-pojo component pattern, from the Web tier to the business layer, with state contexts. Further, seam incorporates a number of major other open source frameworks, such as JBPM, JBoss Rules (aka Drools), JBoss Portal, JBoss Microcontainer, and so on. Seam can not only "organically" combine them, but can also strengthen the original framework as if it were integrated with JSF and EJB3.

Seam is located at the bottom of Java EE 5.0, but its application is not limited to Java EE 5.0 servers. A seam application can be deployed on the Java EE 1.4 Application Server and the Tomcat server. This means that product support is now available in seam applications.

1 + 1 > 2
There may be a misunderstanding that seam is just another integrated framework that strings together different frameworks. Seam provides its own state context for management, allowing the framework to be deeply integrated with other frameworks through annotations and El (Expression language) expressions. The integrated program comes from the seam developer's understanding of the third-party framework.

    1. A web framework designed for ORM
      Object-Relational mapping (ORM) solutions are widely used in today's enterprise applications. However, most current business and web frameworks are not designed for ORM, and they do not manage the persistence context throughout the Web interaction lifecycle-from request arrival to response completion. This leads to a variety of ORM anomalies, including scary lazyinitializationexception, that bring ugly tricks such as "data transfer Objects (DTOs)" (Ugly hacks).

Gavin King invented seam, and he also invented the world's most widely used ORM solution, hibernate. Seam was redesigned to inherit and carry out the best practices of ORM. With seam, there's no need to write DTOs, and all you do is delay loading. Because the extended persistence context is like a natural cache that reduces interaction with the database, the performance of the ORM can be greatly improved.

Further, because seam integrates the ORM layer, the business layer, and the presentation layer, developers can display ORM objects directly in the presentation layer, as well as use database validation annotations for input forms, and redirect ORM exceptions to custom error pages.

3. Designed for stateful Web applications
Seam is designed for stateful Web applications. Web applications are inherently multi-user applications, and e-commerce applications are inherently stateful and transactional. However, most existing Web application frameworks are intended for stateless applications. Developers must manipulate HTTP session objects to manage user state, and code unrelated to the core business logic will not only confuse your application, but also create a series of performance issues.

In seam, all the underlying application components are inherently stateful. They are easier to use than HTTP sessions because their state is publicly managed by seam. There is no need to write a troublesome state management code in a seam application--simply annotate its domain, lifecycle method, and other state attributes on its components, and seam will be in charge of the other [translator note: The life cycle of these components]. The seam state component is better able to manage user state than the HTTP sessions (session). For example, you can have multiple "sessions", each of which consists of a series of Web requests and business method calls in an HTTP session (session).

Further, in seam, database caches and transactions are automatically connected to the state of the application. Seam automatically saves database updates in memory until the end of the conversation is committed to the database. In-memory caching can greatly reduce the load of databases in complex state applications.

In addition to these, seam supports the integration of the open source JBoss jbpm Business program engine, which greatly improves state management in Web applications. Instead of relying on user interface event processing and databases, you can now specify workflows for different workers in an organization, such as customers, managers, technical support staff, and so on, to leverage workflows to drive applications.

    1. Support Web 2.0
      Seam has been fully optimized for Web2.0 applications. It provides a variety of support for Ajax (asynchronous JavaScript and XML, a technology that increases web page interaction)-from the built-in "0 JavaScript" Ajax component to the AJAX-enabled JSF component to the custom JavaScript library, seam is the browser-side JavaScript objects provide a way to directly access the seam server components. Seam provides an advanced concurrency model that effectively manages multiple AJAX requests from the same user.

For AJAX applications, the growing database load is a huge challenge. An AJAX application sends more frequent requests to the server than a non-AJAX application. Once the database has to respond to these AJAX requests, the database is overburdened. The state persistence context in seam is just like an in-memory cache, which can save information at the beginning of a session and ultimately help reduce database interaction.

Web2.0 applications often use complex relational models for their data (for example, a network communication site is dealing with and displaying the relationship between "users"), and for these sites, lazy loading is critical for the ORM layer. Otherwise, a simple query loads the entire database in the same level. As we discussed earlier, seam is the only web framework that correctly supports delayed loading of Web applications today.

5. Pojo services that rely on bidirectional mapping
Seam is a "lightweight" framework because it uses pojo (plain old Java objects) as a service component. In the application, Pojo does not use an interface or abstract class to "hook" the component. The question, of course, is how to make pojo interact to form this application? How do they interact with container services (for example, a database persistence service)?

Seam joins all Pojo components using a popular design pattern called Dependency Injection (DI). In this mode, the seam framework manages the lifecycle of all components. When one component needs to use the other, it declares this dependency to seam through annotations (annotation). Seam obtains this dependent component based on the current state of the application and injects it into the required component.

By extending the dependency injection concept, a seam component A can not only construct another component B, but also "throw" this component B into seam for future use by other components, such as component C.

This type of bi-directional dependency management is even widely used in simple seam web applications (e.g., Hello World in Chapter two). In seam terminology, we call this "dependent bidirectional mapping".

6. Non-conventional configuration
[Translator Note: A configuration that implicitly maps as a theme to explicitly map exceptions]
The main design principle that makes seam easy to use is "unconventional configuration." The idea is to provide a set of default behaviors for these components, and developers only need to configure the components in the display when the expected behavior is not the default. For example, when seam injects component A as a property into component B, by default, component A is just named after the name of the property that component B is injected into. Seam also has very similar details. The overall result is that the configuration metadata in seam is much simpler than other Java frameworks. As a result, most seam applications can be fully configured with a series of simple Java annotations. Developers benefit from the complexity of the reduction, and finally, the same functionality is achieved with less code than other Java frameworks.

7. Avoid misuse of XML
Perhaps you have noticed that Java annotations play an important role in the presentation and processing of seam configuration metadata. This design makes the framework easier to operate.

Early in the development of the Java EE, XML was once regarded as the "holy Grail" of configuration management. The framework designer throws all the configuration information, including Java classes and method names, into the XML document without regard to the consequences for the developer. After reflection, I found that this was a serious mistake. The XML configuration document is too repetitive. The developer must repeat the information already in the code, thus linking the configuration and the code. These repetitions make the application prone to errors (for example, a misspelled class name may appear at run time as a difficult debug error). The lack of a reasonable default configuration further complicates this issue. In fact, in some frameworks, a considerable amount of boilerplate code is disguised as XML, which may be equivalent to or exceed the number of Java code in the actual application. For Java EE developers, the misuse of XML is often referred to as "XML hell".

The Java Community recognizes the misuse of XML and has been very successful in replacing XML with annotations in Java code. EJB3 is a result of the use of annotations in Java Enterprise components promoted by the official Java Standardization Organization. EJB3 is fully selectable using an XML document, which takes a positive step in the right direction. Seam joins EJB3 annotations, extending the annotation-based programming model for the entire Web application.

Of course, XML is not entirely detrimental to configuration data. Seam designers recognize that XML is appropriate for Web applications that specify page processes or define business processes. XML documents make it possible for developers to centrally manage the workflow of the entire Web application, while also opposing the distribution of configuration information in the Java source files. Workflows are rarely coupled to source code, so it is not necessary to re-type the information that already exists in the code in the XML document.

8. Designed for testing
Seam is redesigned for ease of testing. Because all seam components are annotated pojo, they are easy to unit test. Developers only construct instances by using regular Java new keywords, and then run any method in the test framework (for example, JUnit or testng). If you need to test the interaction of multiple seam components, the developer instantiates the components individually and then manually establishes their interrelationships (that is, the display uses the setter method instead of relying on seam dependency injection).

Integration testing the entire seam application is more complex because the developer must run the application in the seam container. Seam uses embedded lightweight containers to help with this kind of testing. In the test framework, developers can load seam containers by step, and then run tests.

    1. Excellent tool support
      For an application framework focused on developer productivity, the support of development tools is critical. Seam has published a command-line-based generator called Seamgen. Seamgen is similar to the generator in Ruby-on-rails, which supports features such as generating a full crud application from a database, and smart developers can improve Web applications through such things as "edit/save/Reload in browser" and test-supported features.

But more importantly, the Seamgen build project does not depend on the mainstream Java integrated development environment, such as Eclipse and NetBeans. With Seamgen, developers can get started at any time.

    1. Let's start coding.
      All in all, seam has cut development costs for Java EE applications, while adding powerful new features that are not yet in use by the EE 5.0. In the next section (excerpt from chapter II of this book), we will show you some practical code examples to illustrate how seam works. You can download the source code for all the examples in this book via the website http://www.michaelyuan.com/seam/.

Seam Hello World

JBoss seam is the glue between EJB3 and JSF, which is the most basic and extensive application of JBoss seam. With seam-managed components, seam allows for seamless (not intentionally pun) integration between the two frameworks. It expands the annotation-based EJB3 Pojo programming model for the entire Web application. Between layers and layers, there is no need for manual jndi lookups, no lengthy JSF support bean declarations, no too many facade methods, no hard objects to pass, fast!

Continue to use Java EE mode in seam
In traditional Java EE applications, some design patterns, such as jndi lookups, XML declaration components, value objects, and facade, are forced to be used. Seam uses annotation-based Pojo to eliminate these human needs. However, these patterns are still free to use when they are really needed in seam applications.

The concept of writing a seam Web application is simple. You only need to encode the following components:

The entity object represents the data model. The entity object may be a Pojo object in JPA or hibernate. They are automatically mapped to relational database tables.
The SF Web page shows the user interface. The page captures the user's input through the form and displays the results. form fields and their data display database tables, which are mapped to entity beans or collections of entity beans.
The EJB3 session bean or the annotated Seam Pojo can act as the UI event handler for the JSF Web page. They handle user input encapsulated in the entity bean and generate the displayed data object for the next (or page).
All of the above components are managed by seam itself and are automatically injected into the correct page or object at run time. For example, when a user clicks a button to submit a JSF form, seam automatically parses the form field and constructs an entity bean. Seam then passes the entity bean to the same seam-constructed event handler session Bean. Developers do not need to manage the interaction between the component's life cycle and components in code. There are no boilerplate code and XML files in the process of dependency processing.

In this chapter, we use the Hello World example to clearly demonstrate how seam adheres to a Web application. The example works as follows: Users can enter their name in a Web form to "greet" Seam. Once she submits the form, the app saves her name to a relational database and displays all users who have "greeted" the seam. The project sample is in the HelloWorld folder in the source code of the book download. In order to build it, you must install the Apache ANT 1.6 version above (http://ant.apache.org/). Entering the HelloWorld directory, running command ant, generates a build/jars/helloworld.ear file that can be copied directly to the Server/default/deploy directory of the JBoss as instance. Now, start JBoss as and open the URL http://localhost:8080/helloworld/.

In order to run the examples in this book, we recommend that you use the Jems GUI installer to install a seam-compatible jboss as. You can download the Jems installer from Http://labs.jboss.com/portal/jemsinstaller/downloads. If you need more installation of JBoss as and application Deployment help, see Appendix A, "Installing and deploying JBoss as"

You are welcome to use the example as a template to quickly start your own seam project (see Appendix B, "Using the app sample as a template"). Alternatively, you can use the command line tool seam Gen (see Chapter Fourth, "Rapid application Development tools") to automatically generate project templates, including all configuration files. In this chapter, I'll spend a small amount of time explaining the directory structure in the source code project. Instead, we'll focus on the code and configuration, which is necessary for developers to build a seam application. In this way, we can apply the knowledge to any project structure without being constrained by the template.

Source code Directory
A seam application consists of a Java class and an XML or text configuration file. In the project example of this book, the Java source code file is in the SRC directory, the Web page is in the view directory, and all the configuration files are in the resources directory. For more information, see Annex B, using the application example as a template.

    1. Create a data model
      The data model in the HelloWorld app is simply a person class with a name and id attribute. Note @entity tells the container to map the class to a relational database table, each attribute corresponds to a field in the table, and each person instance is equivalent to a record in the table. Because seam is configured in an unconventional way, the container uses only the class name and attribute name in the table name and field. The @id and @generatedvalue annotations on the property ID imply that the ID field is the primary key, and its value is automatically generated by the application server for each Peron object saved to the database.

@Entity
@Name ("person")
public class Person implements Serializable {

Private long ID;
private String name;

@Id @GeneratedValue
Public long GetId () {return ID;}
public void SetId (long id) {this.id = ID;}

Public String GetName () {return name;}
public void SetName (String name) {this.name = name;}
}
The most important annotation in the person class is @name, which specifies a name for the person bean that will be registered in seam. In other seam components, such as pages and session beans, developers can refer to the managed person bean directly using "person".

    1. Map a data model to a Web form
      In the JSF page, we use the person bean to support form input text fields. The #{person.name} symbol refers to the name property of the seam component that is nominee as "person", and the seam component named "Man" is an instance of the people entity bean.


Please enter your name:



Open source Java EE framework JBoss Seam details

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.