Transcript Spring
Java 2 Enterprise Edition
SSH Frameworks
Presenter : Lee, Gun
2007-02-05 Updated
Agenda
1
Model 1 VS Model 2
2
Struts Introduction
3
Hibernate Introduction
4
Spring Introduction
5
Integration Of SSH
6
System Logging
Process for running JSP
JSP File
Web
Browser
Servlet
Source Code
Compiled
Servlet Class
MVC Introduction
The Model-View-Controller (MVC) is a
commonly used and powerful architecture
for GUIs.
The MVC paradigm is a way of breaking an
application, or even just a piece of an
application's interface, into three parts:
the model, the view, and the controller.
MVC was originally developed to map the
traditional input, processing, output roles
into the GUI realm.
Simple Model
Request
Web Brower
Modify Data
JSP
Response
Database
Read Data
Model 1 Architecture
Web Browser
JSP
Response
Application
Server
(EJB Server)
JavaBeans
Database
Web Server
Application Server & Database
Request
Model 2 Architecture
Web Browser
Servlet (Controller)
Response
JSP
(View)
JavaBeans
(Model)
Web Server
Application
Server
(EJB Server)
Database
Application Server & Database
Request
Struts Introduction
The goal of the Apache Struts project is to
encourage application architectures based
on the "Model 2" approach, a variation of
the classic Model-View-Controller (MVC)
design paradigm. Under Model 2, a servlet
(or equivalent) manages business logic
execution, and presentation logic resides
mainly in server pages.
Struts for MVC Model
web.xml
Web Browser
Action Servlet
(Controller)
Web
Server
Struts-config.xml
Action
(Business Logic)
Action Form
(Java Bean Or EJB)
JSP
(View)
Other Action
Java Bean
EJB
(Model)
Several models in Struts
Model 1
JSP / Servlet
JSP + Java Bean
JSP + Custom Tag
Integrate above 3 models
Model 2
JSP / Servlet + Struts
JSP + Struts + JSTL + JSF
JSP + Struts + Velocity + Sitemesh
Integrate above 3 models
Configuration in web.xml
<servlet>
<servlet-name>action</servlet-name>
<servletclass>org.apache.struts.action.ActionServlet</servlet
-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/config/strutsconfig.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Action Form in Struts
An ActionForm is a JavaBean that extends
org.apache.struts.action.ActionForm.
ActionForm maintains the session state for
web application and the ActionForm object
is automatically populated on the server
side with data entered from a form on the
client side.
Example of Action Form
public class Message {
private String text;
private Message nextMessage;
public String getText() {return text;}
public void setText(String text) {
this.text = text; }
public Message getNextMessage() {
return nextMessage; }
public void setNextMessage(Message
nextMessage) {
this.nextMessage = nextMessage;
}
}
Action in Struts
The Action Class is part of the Model and
is a wrapper around the business logic.
The purpose of Action Class is to translate
the HttpServletRequest to the business
logic.
To use the Action, we need to Subclass
and overwrite the execute() method.
In the Action Class all the
database/business processing are done.
It is advisable to perform all the database
related stuffs in the Action Class.
Action in Struts (Cont.)
The ActionServlet (commad) passes the
parameterized class to Action Form using
the execute() method.
The return type of the execute method is
ActionForward which is used by the Struts
Framework to forward the request to the
file as per the value of the returned
ActionForward object.
Example of Action
public class SymposiumAction extends DispatchAction {
private SymposiumService symposiumService = null;
public void setSymposiumService(SymposiumService
symposiumService) {
this.symposiumService = symposiumService;
}
public ActionForward list(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
request.setAttribute("symposiumList",
symposiumService.findSymposiumList());
return mapping.findForward("list");
}
}
DAO (Data Access Object) in Struts
Access to data varies depending on the
source of the data.
Access to persistent storage, such as to a
database, varies greatly depending on the
type of storage (relational databases,
object-oriented databases, flat files, and
so forth) and the vendor implementation.
We will implement DAO in hibernate
persistence Layer.
Tag Libraries in Struts
HTML Tag Lib
The tags in the Struts HTML library form a bridge
between a JSP view and the other components of a
Web application.
Bean Tag Lib
Contains JSP custom tags useful in defining new
beans (in any desired scope) from a variety of
possible sources, as well as a tag to render a
particular bean (or bean property) to the output
response.
Logic Tag Lib
Contains tags that are useful in managing conditional
generation of output text, looping over object
collections for repetitive generation of output text, and
application flow management
Tag Libraries in Struts (Cont.)
Nested Tag Lib
Nested tags & supporting classes extend the base
struts tags to allow them to relate to each other in a
nested nature.
Tiles Tag Lib
Tiles builds on the "include" feature provided by the
JavaServer Pages specification to provide a fullfeatured, robust framework for assembling
presentation pages from component parts.
Sample of Struts Tag Library
<%@ taglib uri="http://jakarta.apache.org/struts/tagsbean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tagshtml" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tagslogic" prefix="logic" %>
<logic:iterate name="books"
type="org.library.bean.Book" id="book">
<tr>
<%-- book informations --%>
<td> <bean:write name="book" property="author"
/> </td>
</td>
</logic:iterate>
Configuration in struts-config.xml
<struts-config>
<data-sources />
<form-beans/>
<global-exceptions />
<global-forwards/>
<!-- Action Mappings -->
<action-mappings>
<action forward="/WEB-INF/jsp/index.jsp"
path="/default" />
</action-mappings>
<!-- Message Resources Configuration -->
<message-resources
parameter="org.research.struts.ApplicationResources"
/>
</struts-config>
What is Hibernate?
Hibernate is a powerful, high performance
object/relational persistence and query
service.
Hibernate lets you develop persistent
classes following object-oriented idiom including association, inheritance,
polymorphism, composition, and
collections.
What does Hibernate do?
OO based model
Query language similar to SQL
Transparent Write-behind
Advanced Cache Strategy
Optimized SQL
Fine grained object mapping
Vendor Abstraction and Independence
Improves performance by caching, lazy
loading and etc
Hibernate Architecture
Transient
Objects
Business Layer
Persistence
Objects
Persistence Layer
Session Factory
Transaction
Factory
Connection
Provider
JNDI
Hibernate API
Session
JDBC
J2EE API
Transaction
JTA
Mechanism for Hibernate
Read the hbm.xml during the runtime
Read the class which was included in
hbm.xml
Auto Create SQL after read the stuff on
the above
Create proxy class dynamically using the
CGLIB, and then put SQL in it
Mechanism for Hibernate (Cont.)
Call the session object, when user access
persistency objects
save(Object aPersistentObject);
delete(Object aPersistentObject);
update(Object aPersistentObject):
Each call retrieve to SQL via proxy class,
and send to RDB.
A sample for Persistent class
public class Message {
private String text;
private Message nextMessage;
public String getText() {return text;}
public void setText(String text) {
this.text = text; }
public Message getNextMessage() {
return nextMessage; }
public void setNextMessage(Message
nextMessage) {
this.nextMessage = nextMessage;
}
}
A simple Hibernate XML mapping
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping2.0.dtd">
<hibernate-mapping>
<classname="hello.Message"table="MESSAGES">
<property name="text"
column="MESSAGE_TEXT"/>
<many-to-one name="nextMessage"
cascade="all"
column="NEXT_MESSAGE_ID"/>
</class>
</hibernate-mapping>
A sample for Hibernate DAO Interface
package org.research.symposium.dao;
import java.util.List;
import
org.research.symposium.model.ResearchDepartment;
public interface ResearchDepartmentDAO {
public void save(ResearchDepartment
transientInstance);
public void delete(ResearchDepartment
persistentInstance);
public ResearchDepartment
findById(java.lang.Integer id);
public List findByExample(ResearchDepartment
instance);
}
A sample for Hibernate DAO
public void delete(ResearchDepartment
persistentInstance) {
log.debug("deleting ResearchDepartment
instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
The benefits of Spring
Spring can effectively organize your
middle tier objects, whether or not you
choose to use EJB.
Spring can eliminate the proliferation of
Singletons seen on many projects.
Applications built using Spring are very
easy to unit test.
The benefits of Spring (Cont.)
Spring helps you solve many problems
without using EJB.
Spring provides a consistent framework
for data access, whether using JDBC or an
O/R mapping product such as Hibernate or
a JDO implementation.
Summery of Spring Framework
Spring ORM
Spring Web
Hibernate Support
JDO Support
Web Application
Context
Spring
AOP
Aspect Oriented
Programming
Spring DAO
JDBC Support
DAO Support
Spring Context
UI Support
Validation
EJB Support
Spring Core
Supporting Utilities
Bean Container
Spring Web
MVC
Web MVC Framework
Web Views
JSP / Velocity
PDF / Excel
What is IoC?
IoC is short of Inversion of Control.
Lister
Interface
Finder
finder
finderByID
listByID
<implements>
FinderWithConstruct
<<create>>
listObjectName
FinderWithConstruct
finderByID
private Finder finder = new FinderWithConstruct();
What is AOP?
Injector
Check
Point
Separation of Aspect
Security
Logging
Combination of Aspect
Tran Mgmt
Spring Middle Tiles
Web fronted using
Struts or WebWork
Spring WEB
Spring AOP
Spring ORM
Transaction Management
Using Spring decl. trans
Hibernate Mappings
Custom Hibernate DAOs
Spring Core
Spring DAO
SSH Architecture
Spring Container
Servlet Container
Struts Framework
UI (User Interface) Layer
Spring Framework
Business Layer
Hibernate Framework
Persistence Layer
SSH Framework - Struts
Action Servlet
(Controller)
Action
Web
Server
Action Form
(Java Bean Or EJB)
JSP
(View)
S
(Business Logic)
Hibernate
Struts-config.xml
Spring
Web Browser
web.xml
Other Action
S
H
SSH Framework - Spring
Action
ServiceImpl
Web Browser
Struts
Action Form
(Java Bean Or EJB)
AbstractService
Hibernate
(Business Logic)
Other Action
ApplicationContext.xml
S
S
H
SSH Framework - Hibernate
ModelImpl
AbstractDAO
AbstractModel
Spring
Struts
Web Browser
DAOImpl
XXX.hbm.xml
S
S
H
Spring integration with Struts
Use Spring's ActionSupport
Override the RequestProcessor
Delegate action management to Spring
A much better solution is to delegate Struts action
management to the Spring framework.
You can do this by registering a proxy in the strutsconfig action mapping.
The proxy is responsible for looking up the Struts
action in the Spring context.
Because the action is under Spring's control, it
populates the action's JavaBean properties and
leaves the door open to applying features such as
Spring's AOP interceptors.
Spring integration with Struts (Cont.)
Delegation method of Spring integration
<action path="/searchSubmit"
type="org.springframework.web.struts.DelegatingActi
onProxy"
input="/searchEntry.do"
name="searchForm">
</action>
<plug-in
className="org.springframework.web.struts.Context
LoaderPlugIn"> <set-property
property="contextConfigLocation" value="/WEBINF/beans.xml"/>
</plug-in>
Spring integration with Struts (Cont.)
Register a Struts action in the Spring
context
…
<beans>
<bean name="/searchSubmit"
class="ca.nexcel.books.actions.SearchSubmit">
<property name="bookService">
<ref bean="bookService"/>
</property>
</bean>
</beans>
…
Spring integration with Hibernate
Two approaches
Inversion of Control with a HibernateTemplate
and Callback.
Extending HibernateDaoSupport and Applying
an AOP Interceptor.
• Configure the Hibernate SessionFactory
• Extend your DAO Implementation from
HibernateDaoSupport
• Wire in Transaction Support with AOP
Spring integration with Hibernate (Cont.)
Configuring the Hibernate SessionFactory in
Spring
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSes
sionFactoryBean">
<property name="mappingResources">
<list>
<value>com/zabada/springrecipes/base/Widget.hbm.
xml
</value>
</list>
</property>
</bean>
Spring integration with Hibernate (Cont.)
Extending HibernateDaoSupport for the
Actual DAO Implementation.
public class WidgetDAOHibernateImpl
extends HibernateDaoSupport
implements WidgetDAO {
public Collection getWidgets()
{
return getHibernateTemplate().loadAll(Widget.class);
}
public Widget saveWidget(Widget widget)
{
getHibernateTemplate().saveOrUpdate(widget);
return widget;
}
}
Spring integration with Hibernate (Cont.)
Using AOP to Wire Up the DAO and
Transaction Management
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate3.HibernateIn
terceptor">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="widgetDaoTarget"
class="com.zabada.springrecipes.hibernate.WidgetDA
OHibernateImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property></bean>
Spring integration with Hibernate (Cont.)
<bean id="widgetDAO"
class="org.springframework.aop.framework.ProxyFacto
ryBean">
<property name="proxyInterfaces">
<value>com.zabada.springrecipes.base.WidgetDAO</v
alue>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
<value>widgetDaoTarget</value>
</list>
</property>
</bean>
The Packaging for the Whole Project
WebRoot
WEB-INF
•
•
•
•
•
•
Classes
Config (applicationContext.xml, struts-config.xml)
Jsp (JSP Files…)
Lib (Struts, Spring, Hibernate Libs)
Tld (Tlds for JSTL or struts)
Validator (validator-rules.xml validation.xml)
META-INF
• MANIFEST.MF
Issues of System.out.println ()
Output format for logging info isn’t so
flexible.
Programmer has to recompile the
source if there has some changes.
The efficiency of application can be
decrease if there are so many
println();
Logger’s Level
Off : The OFF Level has the highest possible rank and is
intended to turn off logging.
Fatal : The FATAL level designates very severe error events
that will presumably lead the application to abort.
Error : The ERROR level designates error events that might
still allow the application to continue running.
Warn : The WARN level designates potentially harmful
situations.
Info : The INFO level designates informational messages
that highlight the progress of the application at coarsegrained level.
Debug : The DEBUG Level designates fine-grained
informational events that are most useful to debug an
application.
All : The ALL Level has the lowest possible rank and is
intended to turn on all logging.
Solutions for managing Log4j
User can enable, disable and switch the
logging level between (INFO, WARN,
DEBUG, ERROR, FATAL, OFF, ALL) the
various levels at run-time in my web
application.
Log4j.xml Configuration
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p - %C{1}.%M(%L)
| %m%n"/>
</layout>
</appender>
<logger name="org.springframework">
<level value="WARN"/>
</logger>
<logger name="com.nanumsem">
<level value="DEBUG"/>
</logger>
<root>
<level value="WARN"/>
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>
Log4j DEMO
protected final Log logger = LogFactory.getLog(getClass());
if (logger.isDebugEnabled())
{
logger.debug("entering 'list' method...");
}
P6SPY
P6Log intercepts and logs the database
statements of any application that uses
JDBC.
This application is particularly useful for
developers to monitor the SQL statements
produced by EJB servers Or ORM.
P6Spy is designed to be installed in
minutes and requires no code changes.
Integrate P6SPY With Hibernate
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.p6spy.engine.spy.P6SpyDriver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/</value>
</property>
<property name="username">
<value>username</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
SQL Profiler DEMO
Reference
http://www.java.com
http://struts.apache.org/
http://sourceforge.net/
http://db.apache.org/ojb/
http://logging.apache.org/log4j/
http://jakarta.apache.org/velocity/
http://jakarta.apache.org/commons/
http://www.hibernate.org/
http://www.springframework.org/
http://www.p6spy.com/