Java Hibernate Training
Introducing Struts Framework
Page 1Classification: Restricted
Agenda
• Introduction to Struts Framework
• Features
• Evolution
• Struts Demo
• Declarative validation
• Architecture
• Validators
• Interceptors
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
• JSP: Server Page technology… like ASP, BSP…
• Page is dynamically built on the server…
• Presentation logic goes into the server.
• Limitation:
• As number of clients increases, the server has more work to do. (Controller
logic + Presentation Logic)
• If your web app is responsive/adaptive, it has much more work to do on the
presentation logic.. It has to check the form factor of the device that is
trying to access the web page…
• Solution:
• Move the presentation logic to the client… JS libraries (Angular) + CSS (LESS,
SASS)..
• RESTful architecture.
Page 9Classification: Restricted
Struts demo
• Login Page
• Validation Example
Page 10Classification: 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>
Page 11Classification: Restricted
Struts 2 Architecture
Page 12Classification: Restricted
Struts ready-to-use Validators
• requiredstring validator
• stringlength validator
• email validator
• date validator
• int validator
• double validator
• url validator
• regex validator
Page 13Classification: Restricted
Struts 2 interceptors
• Interceptors are conceptually the same as servlet filters or the JDKs Proxy
class. Interceptors allow for crosscutting functionality to be implemented
separately from the action as well as the framework. You can achieve the
following using interceptors:
• Providing preprocessing logic before the action is called.
• Providing postprocessing logic after the action is called.
• Catching exceptions so that alternate processing can be performed.
Page 14Classification: Restricted
17-08-2018 14
Interceptors
 Can execute code before and after execution
 Are thread-safe
 Can be used for
 Validation
 Pre populating fields
 Double-submit prevention
 Session control
 Authentication
 Type conversion
Page 15Classification: Restricted
Some Struts 2 Interceptors
alias Allows parameters to have different name aliases across requests.
checkbox Assists in managing check boxes by adding a parameter value of
false for check boxes that are not checked.
conversionError Places error information from converting strings to parameter
types into the action's field errors.
createSession Automatically creates an HTTP session if one does not already
exist.
debugging Provides several different debugging screens to the developer.
execAndWait Sends the user to an intermediary waiting page while the action
executes in the background.
exception Maps exceptions that are thrown from an action to a result,
allowing automatic exception handling via redirection.
fileUpload Facilitates easy file uploading.
i18n Keeps track of the selected locale during a user's session.
logger Provides simple logging by outputting the name of the action being
executed.
Page 16Classification: Restricted
Using interceptors
<?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="helloworld" extends="struts-default">
<action name="hello"
class="com.xyz.HelloWorldAction"
method="execute">
<interceptor-ref name="params"/>
<interceptor-ref name="timer" />
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
Page 17Classification: Restricted
Topics to be covered in next session
• Struts 2
• Struts Action Class
• Validation
• Control Tags
• Data Tags
Page 18Classification: Restricted
Thank you!

Struts 2 - Introduction

  • 1.
  • 2.
    Page 1Classification: Restricted Agenda •Introduction to Struts Framework • Features • Evolution • Struts Demo • Declarative validation • Architecture • Validators • Interceptors
  • 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 •JSP: Server Page technology… like ASP, BSP… • Page is dynamically built on the server… • Presentation logic goes into the server. • Limitation: • As number of clients increases, the server has more work to do. (Controller logic + Presentation Logic) • If your web app is responsive/adaptive, it has much more work to do on the presentation logic.. It has to check the form factor of the device that is trying to access the web page… • Solution: • Move the presentation logic to the client… JS libraries (Angular) + CSS (LESS, SASS).. • RESTful architecture.
  • 10.
    Page 9Classification: Restricted Strutsdemo • Login Page • Validation Example
  • 11.
    Page 10Classification: 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>
  • 12.
  • 13.
    Page 12Classification: Restricted Strutsready-to-use Validators • requiredstring validator • stringlength validator • email validator • date validator • int validator • double validator • url validator • regex validator
  • 14.
    Page 13Classification: Restricted Struts2 interceptors • Interceptors are conceptually the same as servlet filters or the JDKs Proxy class. Interceptors allow for crosscutting functionality to be implemented separately from the action as well as the framework. You can achieve the following using interceptors: • Providing preprocessing logic before the action is called. • Providing postprocessing logic after the action is called. • Catching exceptions so that alternate processing can be performed.
  • 15.
    Page 14Classification: Restricted 17-08-201814 Interceptors  Can execute code before and after execution  Are thread-safe  Can be used for  Validation  Pre populating fields  Double-submit prevention  Session control  Authentication  Type conversion
  • 16.
    Page 15Classification: Restricted SomeStruts 2 Interceptors alias Allows parameters to have different name aliases across requests. checkbox Assists in managing check boxes by adding a parameter value of false for check boxes that are not checked. conversionError Places error information from converting strings to parameter types into the action's field errors. createSession Automatically creates an HTTP session if one does not already exist. debugging Provides several different debugging screens to the developer. execAndWait Sends the user to an intermediary waiting page while the action executes in the background. exception Maps exceptions that are thrown from an action to a result, allowing automatic exception handling via redirection. fileUpload Facilitates easy file uploading. i18n Keeps track of the selected locale during a user's session. logger Provides simple logging by outputting the name of the action being executed.
  • 17.
    Page 16Classification: Restricted Usinginterceptors <?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="helloworld" extends="struts-default"> <action name="hello" class="com.xyz.HelloWorldAction" method="execute"> <interceptor-ref name="params"/> <interceptor-ref name="timer" /> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
  • 18.
    Page 17Classification: Restricted Topicsto be covered in next session • Struts 2 • Struts Action Class • Validation • Control Tags • Data Tags
  • 19.