450 likes | 703 Views
Lightweight Architecture. Issue Date: 03/10/2008 Author: Vu Tran. Agenda. Objectives Lightweight Architecture Overview each component (Struts 2, Tiles 2, Spring 2.5, Hibernate 3.2) Demo Analysis the sample. Objectives. Opportunity from CSC Improve researchable, new technology
E N D
Lightweight Architecture Issue Date: 03/10/2008 Author: Vu Tran
Agenda • Objectives • Lightweight Architecture • Overview each component (Struts 2, Tiles 2, Spring 2.5, Hibernate 3.2) • Demo • Analysis the sample
Objectives • Opportunity from CSC • Improve researchable, new technology • Technologies bring you increase productivity and quality. • Next action. • …
Productivity and Quality • Increase Productivity • Increase Quality • Productivity Quality • Technologies. • Struts, JSF… • Spring… • Hibernate, EJB, Top Link, JPA, JDO … • Struts 2.x, Tiles 2.x, Spring 2.5.x, Hibernate 3.2.x
Presentation Layer Business Layer DAO Layer Architecture
Struts2 • Apache Struts2 was originally known as WebWork 2. • Goal of Struts2: • To make web development easier for developer. • To reduce XML configuration (intelligent defaults, annotations, …) • Actions are now POJOs increases testability and reduces coupling in the framework. • Spring, SiteMesh and Tiles integration. • OGNL expression language integration. • Themes based ta libraries and Ajax tags. • Multiple view options (JSP, Freemarker, Velocity, XSLT)
Configuration • Web.xml <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Configuration • Struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="person" extends="struts-default"> <action name="list" method="execute" class="personAction"> <result>pages/list.jsp</result> <result name="input">pages/list.jsp</result> </action> <action name="save" method="save“ class="personAction"> <result>/home.jsp</result> <result name="input">pages/list.jsp</result> </action> </package> </struts>
Header Leftnav Body Footer Footer Tiles • What is Tiles? • To be a templating framework built to simplify the development of web application user interfaces. • To define page fragments which can be assembled into a complete page at runtime
Configuration • Web.xml <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class> </listener> <context-param> <param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name> <param-value> /WEB-INF/tiles.xml </param-value> </context-param>
Configuration • Tiles.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="myapp.homepage" template="/template/layout.jsp"> <put-attribute name="title" value="everyone" /> <put-attribute name="header" value="/include/header.jsp" /> <put-attribute name="footer" value="/include/footer.jsp" /> </definition> <definition name="myapp.homepage.home" extends="myapp.homepage"> <put-attribute name="detail" value="/pages/detail.jsp" /> <put-attribute name="list" value="/pages/list.jsp" /> </definition> </tiles-definitions>
JSP file • JSP file: <%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> <tiles:insertDefinition name="myapp.homepage.home" />
Spring • What is Spring? • Spring is an open source framework for developing enterprise application. • Spring is a light-weight framework for the development of enterprise-ready applications • Spring can be used to configure declarative transaction management, remote access to your logic using RMI or web services, mailing facilities and various options in persisting your data to a database. • Spring framework can be used in modular fashion; it allows using in parts and leaving the other components, which is not required by the application.
Spring • What are features of Spring? • Lightweight • Inversion of control (IOC) • Aspect oriented (AOP) • Container • MVC Framework • Transaction Management • JDBC Exception Handling
Spring • What are features of Spring? • Lightweight • Inversion of control (IOC) • Aspect oriented (AOP) • Container • MVC Framework • Transaction Management • JDBC Exception Handling
Spring • What are features of Spring? • Lightweight • Inversion of control (IOC) • Aspect oriented (AOP) • Container • MVC Framework • Transaction Management • JDBC Exception Handling
Spring • What are features of Spring? • Lightweight • Inversion of control (IOC) • Aspect oriented (AOP) • Container • MVC Framework • Transaction Management • JDBC Exception Handling
Spring • What are features of Spring? • Lightweight • Inversion of control (IOC) • Aspect oriented (AOP) • Container • MVC Framework • Transaction Management • JDBC Exception Handling
Spring • What are features of Spring? • Lightweight • Inversion of control (IOC) • Aspect oriented (AOP) • Container • MVC Framework • Transaction Management • JDBC Exception Handling
Spring • What are features of Spring? • Lightweight • Inversion of control (IOC) • Aspect oriented (AOP) • Container • MVC Framework • Transaction Management • JDBC Exception Handling
Spring • What are modules of Spring? • The core container • Spring context • Spring AOP • Spring DAO • Spring ORM • Spring Web module • Spring MVC framework
Spring • What are modules of Spring? • The core container • Spring context • Spring AOP • Spring DAO • Spring ORM • Spring Web module • Spring MVC framework
Spring • What are modules of Spring? • The core container • Spring context • Spring AOP • Spring DAO • Spring ORM • Spring Web module • Spring MVC framework
Spring • What are modules of Spring? • The core container • Spring context • Spring AOP • Spring DAO • Spring ORM • Spring Web module • Spring MVC framework
Spring • What are modules of Spring? • The core container • Spring context • Spring AOP • Spring DAO • Spring ORM • Spring Web module • Spring MVC framework
Spring • What are modules of Spring? • The core container • Spring context • Spring AOP • Spring DAO • Spring ORM • Spring Web module • Spring MVC framework
Spring • What are modules of Spring? • The core container • Spring context • Spring AOP • Spring DAO • Spring ORM • Spring Web module • Spring MVC framework
Spring • What is BeanFactory? • A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked for by clients. • BeanFactory is able to create associations between collaborating objects as they are instantiated. This removes the burden of configuration from bean itself and the beans client. • BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods.
Spring • What is Application Context? • Like BeanFactory, load bean definitions, wire beans together, and dispense beans upon request. But it also provides: • A means for resolving text messages, including support for internationalization. • A generic way to load file resources. • Events to beans that are registered as listeners.
Spring • Application Context config file sa
Hibernate • What is hibernate? • A pure Java object-relational mapping (ORM) and persistence framework. • Why do you need a tool like Hibernate? • Improved productivity • High-level object-oriented API • Less Java code to write • No SQL to write • Improved performance • Sophisticated caching • Lazy loading • Eager loading • Improved maintainability • A lot less code to write • Improved portability • ORM framework generates specific DB script for you.
Hibernate • What does Hibernate simplify? • Saving and retrieving your domain objects • Making DB column and table name changes • Complex joins for retrieving related items • Schema creation from object model • Core Hibernate Framework • Session • SessionFactory • Configuration • Transaction • Query and Criteria
Hibernate • Session • A single threaded, short-lived object representing a conversation between application and the persistent store. • Session role • Wraps a JDBC connection • Factory for Transaction • Holds a mandatory cache of persistent objects, used when navigating the object graph or looking up objects by identifier • Session session = sessionFactory.openSession(); • SessionFactory • There is a single SessionFactory for the whole application and created during application initialization • Cache session object • Cache generate SQL statements and other mapping metadata that Hibernate uses at runtime • Cached data that has been read in one unit of work and may be reused in a future unit of work
Hibernate • Flow of Hibernate communication with RDBMS? • Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files • Create session factory from configuration object • Get one session from this session factory • Create HQL query • Execute query to get list containing Java objects
Configuration • hibernate.cfg.xml <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost/tranquang?useUnicode=true&characterEncoding=UTF-8</property> <property name="connection.username">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.password"></property> <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="connection.pool_size">10</property> <property name="current_session_context_class">thread</property> <property name="show_sql">false</property> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
Deployment • JDK5 • Ajax: jquery • Struts2 • Tiles2 • Spring2.5 • Hibernate3.2 • Server: Jetty