Java Hibernate Training Integrating Struts with Hibernate
Page 1Classification: Restricted Agenda • Struts 2 • Struts Action Class • Validation • Control Tags • Data Tags
Page 2Classification: Restricted Struts framework • The struts 2 framework is used to develop MVC-based web application. • The struts framework was initially created by Craig McClanahan and donated to Apache Foundation in May, 2000 and Struts 1.0 was released in June 2001. • Craig R. McClanahan is a programmer and original author of the Apache Struts framework for building web applications. He was part of the expert group that defined the servlet 2.2, 2.3 and JSP 1.1, 1.2 specifications. He is also the architect of Tomcat's servlet container Catalina.
Page 3Classification: Restricted Struts 2 Framework • The Struts 2 framework is used to develop MVC (Model View Controller) based web applications. Struts 2 is the combination of webwork framework of opensymphony and struts 1. struts2 = webwork + struts1
Page 4Classification: Restricted Struts 2 Features • Configurable MVC components • POJO based actions • AJAX support • Integration support – with Hibernate, Spring etc • Various Result Types – JSP, Freemarker, Velocity etc • Various Tag support • Theme and Template support
Page 5Classification: Restricted Web Application Architecture - Evolution • Model 1 Architecture pag e 5 • Navigation control is decentralized • Time Consuming • Hard to extend
Page 6Classification: Restricted Web Application Architecture - Evolution • Model 2 Architecture pag e 6 • Navigation control is centralized Now only controller contains the logic to determine the next page. • Easy to maintain, extend and test • Better separation of concerns • (SOLID principles: OOP: Single Responsibility Principle)
Page 7Classification: Restricted Struts… building your first app • Struts – Configuring in Eclipse demo
Page 8Classification: Restricted Struts demo • Login Page • Validation Example
Page 9Classification: Restricted Struts Action class… A Struts Action class can be • A simple POJO • Implements Action • Extends ActionSupport • Implements Action, Validateable, Serializable etc
Page 10Classification: Restricted Validation in Struts 2 Two ways: • Custom validation (as shown in last class) • Extend ActionSupport class • Override validate() method (Derived from Validateable interface) • addFieldError() (Derived from ValidationAware interface) • Built-in validation • requiredstring validator • stringlength validator • email validator • date validator • int validator • double validator • url validator • regex validator
Page 11Classification: Restricted Built-in validation • Example: <validators> <!-- Field-Validator Syntax --> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>username is required</message> </field-validator> </field> </validators>
Page 12Classification: Restricted Struts 2 tags: Control tags <s:if test="%{false}"> <div>Will Not Be Executed</div> </s:if> <s:elseif test="%{true}"> <div>Will Be Executed</div> </s:elseif> <s:else> <div>Will Not Be Executed</div> </s:else>
Page 13Classification: Restricted Struts 2 tags: Control tags <s:iterator value="days"> <p>day is: <s:property/></p> </s:iterator>
Page 14Classification: Restricted Struts 2 tags: Control tags <s:merge var="myMergedIterator"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:merge> <s:iterator value="%{#myMergedIterator}"> <s:property /> </s:iterator>
Page 15Classification: Restricted Struts 2 tags: Control tags <s:append var="myAppendIterator"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:append> <s:iterator value="%{#myAppendIterator}"> <s:property /> </s:iterator>
Page 16Classification: Restricted Struts 2 – Data tags <-- First Syntax --> <s:include value="myJsp.jsp" /> <-- Second Syntax --> <s:include value="myJsp.jsp"> <s:param name="param1" value="value2" /> <s:param name="param2" value="value2" /> </s:include> <-- Third Syntax --> <s:include value="myJsp.jsp"> <s:param name="param1">value1</s:param> <s:param name="param2">value2</s:param> </s:include>
Page 17Classification: Restricted Struts 2 – Data tags <s:bean name="org.apache.struts2.util.Counter" var="counter"> <s:param name="first" value="20"/> <s:param name="last" value="25" /> </s:bean>
Page 18Classification: Restricted Declarative validation //<ActionClassName>-validation.xml <?xml version="1.0" encoding="UTF-8"?> Use appropriate DTD here!! <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message>Name can't be blank</message> </field-validator> </field> </validators>
Page 19Classification: Restricted Topics to be covered in next session Spring Framework • Core Container • Data Access/Integration • Web Layer • Spring Setup • Key features • Spring Bean • Dependency Injection • Relation between DI and IoC • Spring IoC Containers • Spring DI
Page 20Classification: Restricted Thank you!

Struts 2 - Hibernate Integration

  • 1.
  • 2.
    Page 1Classification: Restricted Agenda •Struts 2 • Struts Action Class • Validation • Control Tags • Data Tags
  • 3.
    Page 2Classification: Restricted Strutsframework • The struts 2 framework is used to develop MVC-based web application. • The struts framework was initially created by Craig McClanahan and donated to Apache Foundation in May, 2000 and Struts 1.0 was released in June 2001. • Craig R. McClanahan is a programmer and original author of the Apache Struts framework for building web applications. He was part of the expert group that defined the servlet 2.2, 2.3 and JSP 1.1, 1.2 specifications. He is also the architect of Tomcat's servlet container Catalina.
  • 4.
    Page 3Classification: Restricted Struts2 Framework • The Struts 2 framework is used to develop MVC (Model View Controller) based web applications. Struts 2 is the combination of webwork framework of opensymphony and struts 1. struts2 = webwork + struts1
  • 5.
    Page 4Classification: Restricted Struts2 Features • Configurable MVC components • POJO based actions • AJAX support • Integration support – with Hibernate, Spring etc • Various Result Types – JSP, Freemarker, Velocity etc • Various Tag support • Theme and Template support
  • 6.
    Page 5Classification: Restricted WebApplication Architecture - Evolution • Model 1 Architecture pag e 5 • Navigation control is decentralized • Time Consuming • Hard to extend
  • 7.
    Page 6Classification: Restricted WebApplication Architecture - Evolution • Model 2 Architecture pag e 6 • Navigation control is centralized Now only controller contains the logic to determine the next page. • Easy to maintain, extend and test • Better separation of concerns • (SOLID principles: OOP: Single Responsibility Principle)
  • 8.
    Page 7Classification: Restricted Struts…building your first app • Struts – Configuring in Eclipse demo
  • 9.
    Page 8Classification: Restricted Strutsdemo • Login Page • Validation Example
  • 10.
    Page 9Classification: Restricted StrutsAction class… A Struts Action class can be • A simple POJO • Implements Action • Extends ActionSupport • Implements Action, Validateable, Serializable etc
  • 11.
    Page 10Classification: Restricted Validationin Struts 2 Two ways: • Custom validation (as shown in last class) • Extend ActionSupport class • Override validate() method (Derived from Validateable interface) • addFieldError() (Derived from ValidationAware interface) • Built-in validation • requiredstring validator • stringlength validator • email validator • date validator • int validator • double validator • url validator • regex validator
  • 12.
    Page 11Classification: Restricted Built-invalidation • Example: <validators> <!-- Field-Validator Syntax --> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>username is required</message> </field-validator> </field> </validators>
  • 13.
    Page 12Classification: Restricted Struts2 tags: Control tags <s:if test="%{false}"> <div>Will Not Be Executed</div> </s:if> <s:elseif test="%{true}"> <div>Will Be Executed</div> </s:elseif> <s:else> <div>Will Not Be Executed</div> </s:else>
  • 14.
    Page 13Classification: Restricted Struts2 tags: Control tags <s:iterator value="days"> <p>day is: <s:property/></p> </s:iterator>
  • 15.
    Page 14Classification: Restricted Struts2 tags: Control tags <s:merge var="myMergedIterator"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:merge> <s:iterator value="%{#myMergedIterator}"> <s:property /> </s:iterator>
  • 16.
    Page 15Classification: Restricted Struts2 tags: Control tags <s:append var="myAppendIterator"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:append> <s:iterator value="%{#myAppendIterator}"> <s:property /> </s:iterator>
  • 17.
    Page 16Classification: Restricted Struts2 – Data tags <-- First Syntax --> <s:include value="myJsp.jsp" /> <-- Second Syntax --> <s:include value="myJsp.jsp"> <s:param name="param1" value="value2" /> <s:param name="param2" value="value2" /> </s:include> <-- Third Syntax --> <s:include value="myJsp.jsp"> <s:param name="param1">value1</s:param> <s:param name="param2">value2</s:param> </s:include>
  • 18.
    Page 17Classification: Restricted Struts2 – Data tags <s:bean name="org.apache.struts2.util.Counter" var="counter"> <s:param name="first" value="20"/> <s:param name="last" value="25" /> </s:bean>
  • 19.
    Page 18Classification: Restricted Declarativevalidation //<ActionClassName>-validation.xml <?xml version="1.0" encoding="UTF-8"?> Use appropriate DTD here!! <!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <validators> <field name="username"> <field-validator type="requiredstring"> <message>Name can't be blank</message> </field-validator> </field> </validators>
  • 20.
    Page 19Classification: Restricted Topicsto be covered in next session Spring Framework • Core Container • Data Access/Integration • Web Layer • Spring Setup • Key features • Spring Bean • Dependency Injection • Relation between DI and IoC • Spring IoC Containers • Spring DI
  • 21.