Thứ Năm, 14 tháng 3, 2013

Struts 2 Architecture

From a high level, Struts2 is a pull-MVC (or MVC2) framework. The Model-View-Controller pattern in Struts2 is realized with following five core components:
  • Actions
  • Interceptors
  • Value Stack / OGNL
  • Results / Result types
  • View technologies
Struts 2 is slightly different from a traditional MVC framework in that the action takes the role of the model rather than the controller, although there is some overlap.
Struts 2 Architecture
The above diagram depicts the Model, View and Controller to the Struts2 high level architecture. The controller is implemented with a Struts2 dispatch servlet filter as well as interceptors, the model is implemented with actions, and the view as a combination of result types and results. The value stack and OGNL provide common thread, linking and enabling integration between the other components.
Apart from the above components, there will be a lot of information that relates to configuration. Configuration for the web application, as well as configuration for actions, interceptors, results, etc.
This is the architectural overview of the Struts 2 MVC pattern. We will go through each component in more detail in the subsequent chapters.

Request life cycle:

Based on the above digram, one can explain the user's request life cycle in Struts 2 as follows:
  • User sends a request to the server for requesting for some resource (i.e pages).
  • The FilterDispatcher looks at the request and then determines the appropriate Action.
  • Configured interceptors functionalities applies such as validation, file upload etc.
  • Selected action is executed to perform the requested operation.
  • Again, configured interceptors are applied to do any post-processing if required.
  • Finally the result is prepared by the view and returns the result to the user.

Shall we do some Spring together?

Shall we do some Spring together?

About: In this tutorial I will show you, how to write Spring webapplication from the scratch and we will do it with step-by-step approach. Maybe you’ve already tried some Spring but you didn’t have the opportunity or will to use more than one particular Spring technology. Maybe you gave up because it looked hard to get all the libraries correct. Don’t worry, we will go together through every step and create working application from the scratch.

Targeted audience: Java programmers. If you’re new to Spring, I hope this will help you to start with Spring development. We will specifically look at assembling complete application with handful of technologies.

Prerequisites: You should be familiar with Java programming language. You will require some general knowledge about web applications, Servlet technology, SQL, REST, ORM , unit testing and dependency injection. We won’t go too deep in any of those, but if something is new to you please check at least some wiki pages or presentations or maybe even tutorials.

List of chapters:
Read more: http://vrtoonjava.wordpress.com/2012/06/17/shall-we-do-some-spring-together/

Thứ Tư, 13 tháng 3, 2013

Presentation Layer - Business Layer - Data Access Layer

Presentation Layer

JSF is used to build the presentation layer of the application. JFS allows us to create rich GUI for web application. It resolves the technical challenges of creating rich GUI web application. In this layer we have JSP pages
consists of JSF components. All the requests to the web server pass through FacesServlet.

Business Layer

The POJO classes and classes to process the business logic are used to create the Business Layer. The POJO classes, with the help of spring framework, create an ideal solution to implement the Business Layer.

Data Access Layer

The data access layer handles all the logic to save and retrieve the data from database. Hibernate O/R mapping tools is an ideal solution for enterprise application of any size. Hibernate handles all the logic to store and retrieve POJO objects. It also handles resource management and transaction management activities.

Java Serialization.

Java has a built in mechanism for converting the state of an object into a specific form of bytes. All of the data , type , and member information of the object is converted into this byte format by a process called "Serialization" and is stored in a file.

The beauty of this technique is that , that Serialization is a JVM independent process. Hence you can serialize an object from Java , and deserialize it from the .NET platform.

Transient Keyword.

The transient keyword is attached to a member field of an object , whose state you do not wish to persist during serialization.

Difference between Hibernate Sessions and Transactions.


Session

Most technology terms mean literally what they represent in the English dictionary, well with a little ketchup. A session is a lease of time that you’d take with a fellow, or any other entity. Once you are in a session with the other entity, you can actively use it ; or communicate with whatever communication mechanism that entity has.

Transaction

A transaction occurs AFTER a session. Once you are in a session, you perform transactions. You are in absolute need to be in a session to perform a transaction. You cannot perform a transaction with an entity without being in session with it. For example let’s take an example of a ATM machine.
You go and stand in queue of waiting customers who wish to acquire a session with the ATM. Once the ATM is free, you initiate your session. Now you and the ATM are tunneled, face to face and ready to do business. You dial your pin, and this is where you start your transaction. So any material work you perform with an entity will be called a transaction. So basically the purpose of acquiring a session IS in effect because you wished to perform a transaction. The transaction is the real action that full fills the business you have with the other entity.
Making the other entity aware of yourself is hence called a session, in which you and the entity have now shaken hands. After that as you work, it’ll be termed as a transaction.

What's the difference between @Component, @Repository & @Service annotations in Spring?


In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.
Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.
Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.
Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.
| Annotation | Meaning                                             |
+------------+-----------------------------------------------------+
| @Component | generic stereotype for any Spring-managed component |
| @Repository| stereotype for persistence layer                    |
| @Service   | stereotype for service layer                        
| @Controller| stereotype for presentation layer (spring-mvc) | 

Thứ Hai, 25 tháng 2, 2013

Tổng quan về J2EE

Ngày nay, càng có nhiều hơn các nhà phát triển muốn viết các ứng dụng theo kiểu giao dịch cho các doanh nghiệp và do đó nó yêu cầu về mặt tốc độ, bảo mật, cũng như sự đáng tin cậy của công nghệ phía máy chủ. Nếu bạn đang làm việc trong lĩnh vực này, chắc hẳn các bạn cũng biết rằng sự chuyển động nhanh và các yêu cầu của thế giới về thương mại điện tử và công nghệ thông tin, thì các ứng dụng doanh nghiệp phải được thiết kế, xây dựng và xử lý  sao cho chi phí là thấp nhất, tốc độ nhanh nhất và ít tốn nguồn tài nguyên hơn bao giờ hết.
Để giảm chi phí và để theo dõi một cách nhanh nhất việc thiết kế và phát triển các ứng dụng thì hệ nền Java™2, va2 Enterprise Edition (J2EE™) đã cung cấp một phương pháp tiếp cận dựa trên các thành phần để thiết kế, phát triển, gom lại và triển khai các ứng dụng doanh nghiệp.
Hệ nền J2EE cung cấp một mô hình ứng dụng theo kiểu đa tầng, tái sử dụng các thành phần, bảo mật thống nhất, linh hoạt trong việc kiểm soát các giao dịch và các dịch vụ web được hỗ trợ thông qua trao đổi dữ liệu tích hợp trên Extensible Markup Language (XML) – theo các tiêu chuẩn mở và các giao thức.
Không chỉ giúp các bạn cung cấp các giải pháp kinh doanh của bạn ra thị trường một cách nhanh nhất, mà còn làm cho các giải pháp về các thành phần cơ bản độc lập hệ nền J2EE không phụ thuộc vào các sản phẩm, cũng như các ứng dụng lập trình giao diện của bất cứ nhà cung cấp nào. Các nhà cung cấp và khách hàng có thể tự do chọn lựa các sản phẩm và các thành phần đáp ứng tốt nhất các yêu cầu kinh doanh và công nghệ của họ. Bài hướng dẫn này sử dụng các ví dụ để mô tả các tính năng và chức năng có sẵn trong phiên bản nền tảng J2EE 1.4 cho việc phát triển các ứng dụng doanh nghiệp.  Cho dù bạn là một người mới hoặc là một nhà phát triển giàu kinh nghiệm, bạn cũng nên theo dõi các ví dụ và xem các lời chú giải kèm theo để đánh giá và kiểm tra kiến thức cũng như tìm ra cho mình một cách tiếp cận.
Nếu bạn là một người mới làm quen với J2EE thì chương này là một nơi tốt để bắt đầu. Tại đây bạn sẽ xem xét các vấn đề cơ bản về phát triển, tìm hiểu về cấu trúc J2EE và các API, làm quen với các thuật ngữ và khái niệm quan trọng, và tìm hiểu làm thế nào để tiếp cận lập trình ứng dụng J2EE, lắp ráp và triển khai.
Các ứng dụng phân tán đa tầng
Hệ nền J2EE sử dụng một mô hình ứng dụng theo kiểu đa tầng cho các ứng dụng doanh nghiệp. Về mặt logic ứng dụng được chia theo các thành phần theo chức năng, và các thành phần ứng dụng khác nhau tạo nên một ứng dụng J2EE được cài đặt trên các máy khác nhau tùy thuộc vào các tầng trong môi trường phân tầng của J2EE nơi mà các thành phần ứng dụng thuộc về.
Hình 1.1
Hình 1.1 trình bày 2 ứng dụng phân tầng J2EE được chia thành các lớp được mô tả trong danh sách sau đây. Các thành phần của ứng dụng J2EE được trình bày trong hình 1.1 sẽ được trình bày kỹ hơn trong mục Các thành phần của J2EE.
  • Các thành phần tầng khách hành chạy trên máy khách hàng
  • Các thành phần tầng web chạy trên máy chủ J2EE
  • Các thành phần nghiệp vụ kinh doanh chạy trên máy chủ J2EE
  • Phần mềm hệ thống thông tin doanh nghiệp(EIS) chạy trên EIS Server
Mặc dù 1 ứng dụng J2EE có thể bao gồm 3 hay 4 tầng như trong hình 1.1 nhưng các ứng dụng phân tầng J2EE thường được coi như là 3 tầng bởi vì chúng được phân chia theo địa điểm : máy khách hàng, máy chủ J2EE, và database. Ứng dụng 3 tầng này chạy bằng cách mở rộng tiêu chuẩn mô hình 2 tầng máy khách hàng và máy chủ bằng cách đặt 1 máy chủ ứng dụng đa luồng giữa máy khách hàng và các lưu trữ phía sau.
Các thành phần của J2EE
Ứng dụng J2EE được tạo thành từ các thành phần. Một thành phần J2EE là một đơn vị chức năng phần mềm khép kín được lắp ghép vào một ứng dụng J2EE, nó có liên quan đến các lớp, các tập tin và nó giao tiếp với các thành phần khác. Các J2EE đặc tả định nghĩa các thành phần J2EE sau đây:
  • Ứng dụng khách hàng và các applet là những thành phần chạy trên máy khách.
  • Thành phần công nghệ Java Servlet và JavaServer Pages ™ (JSP ™) là các thành phần web chạy trên máy chủ.
  • Enterprise JavaBeans ™ (EJB ™) là thành phần nghiệp vụ kinh doanh chạy trên máy chủ.
J2EE được viết bằng ngôn ngữ lập trình Java và được biên dịch trong cùng một cách như bất kỳ chương trình cùng ngôn ngữ. Sự khác biệt giữa J2EE và các lớp Java “tiêu chuẩn” là các thành phần J2EE lắp ráp thành một ứng dụng J2EE, được xác minh và tuân thủ với các đặc tả J2EE, và được triển khai tại nơi mà chúng chạy và được quản lý bởi máy chủ J2EE.
Thành phần khách hàng
Một thành phần khách hàng có thể là 1 trang web dành cho khách hàng hoặc là 1 ứng dụng dành cho khách hàng.