V-Client and Server Side Frameworks
V-Client and Server Side Frameworks
Node JS
Struts
Angular
JS
Outlin
eMVC architecture,
Angular JS : Overview,
• Syntax
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
</script>
AngularJS
Introduction
• AngularJS Extends HTML
• AngularJS extends HTML with ng-directives.
• The ng-app directive defines an AngularJS application.
• The ng-model directive binds the value of HTML controls
(input, select, textarea) to application data.
• The ng-bind directive binds application data to the HTML
view.
Outlin
eMVC architecture,
Angular JS : Overview,
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
</body>
</html>
How AngularJS integrates
with HTML
• ng-app directive indicates the start of AngularJS application.
• ng-model directive then creates a model variable named
"name" which can be used with the html page and within the
div having ng-app directive.
• ng-bind then uses the name model to be displayed in the
html span tag whenever user input something in the text box.
• Closing</div> tag indicates the end of AngularJS application.
AngularJS -
Directives
• AngularJS directives are used to extend HTML. These are special
attributes starting with ng- prefix.
1. ng-app − This directive starts an AngularJS Application. It is also
used to load various AngularJS modules in AngularJS
Application.
• <div ng-app = ""> ... </div>
2. ng-init − This directive initializes application data. It is used to
put values to the variables to be used in the application.
• <div ng-app="" ng-init="firstName='John'">
3. ng-model − This directive binds the values of AngularJS
application data to HTML input controls.
• <p>Enter your Name: <input type = "text" ng-model =
"name"></p>
4. ng-repeat − This directive repeats html elements for each item
in a collection.
• <ol>
• <li ng-repeat = "country in countries"> {{ country.name}} </li>
AngularJS Directives-
Example
• AngularJS directives are HTML attributes with an ng prefix.
• The ng-init directive initializes AngularJS application variables.
• AngularJS Example
• <div ng-app="" ng-init="firstName='John'">
<p>The name is <span ng-bind="firstName"></span></p>
</div>
OUTPUT
AngularJS -
Expressions
• Expressions are used to bind application data to html.
• Expressions are written inside double braces like
• {{ expression}}.
• Expressions behaves in same way as ng-bind directives.
• Using numbers
• <p>Expense on Books : {{cost * quantity}} Rs</p>
• Using strings
• <p>Hello {{fname+ “ “ +lname }}</p>
• Using object
• <p>Roll No: {{student.rollno}}</p>
• Using array
• <p>Marks(Math): {{marks[3]}}</p>
AngularJS Expressions -
Example
• AngularJS expressions are written inside double braces: {{ expression }}.
• AngularJS will "output" data exactly where the expression is written:
• AngularJS Example
• <html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.j
s"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
AngularJS -
Controllers
• AngularJS application mainly relies on controllers to control
the flow of data in the application.
• A controller is defined using ng-controller directive.
• Each controller accepts $scope as a parameter which refers to
the application/module that controller is to control.
• <script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
AngularJS
Controllers-
Example Explained
• AngularJS modules define applications:
• var app = angular.module('myApp', []);
• Example
• Enter first name:<input type = "text" ng-model = "firstName">
• Name in Upper Case: {{firstName | uppercase}}
AngularJS -
Tables
• Table data is normally repeatable by nature. ng-repeat directive can be
used to draw table easily.
• The ng-repeat directive repeats a set of HTML, a given number of
times.
• The set of HTML will be repeated once per item in a collection.
• The collection must be an array or an object.
• Following example states the use of ng-repeat directive to draw a table.
• <table >
• <tr>
• <th> Name </th>
• <th> City </th>
• </tr>
• <tr ng-repeat="entry in collection">
• <td> {{entry.name}}</td>
• <td> {{entry.city}} </td>
• </tr>
• </table>
AngularJS – Tables
Example
• AngularFormTable.html
<form ng-submit="addEntry()">
<html ng-app = "simpleApp"> Name:<input type="text" ng-model="newData.name" >
<head>
city:<input type="text" ng-model="newData.city" >
<script src =
"https://ajax.googleapis.com/ajax/libs/angul <input type="submit" value="Add Entry">
a rjs/1.3.14/angular.min.js"></script> </form>
<script src = "index.js"></script>
</div>
</head>
<body> </body>
<div ng-controller = "simpleController"> </html>
<table border= 1>
<tr>
<th> No </th>
<th> Name </th>
<th> City </th>
</tr>
app.controller("simpleController",function($scope)
{
$scope.collection=[
{name:"Amit",city:"Nashik"},
{name:"Neha",city:"Nashik"}];
$scope.addEntry=function()
{
$scope.collection.push($scope.newData);
$scope.newData='';
};
});
AngularJS – Tables Example
O/P
Outlin
eMVC architecture,
Angular JS : Overview,
• Events
• AngularJS provides multiple events which can be associated
with the
HTML controls.
• ng-click
• ng-dbl-click
• ng-mousedown
• ng-mouseup
• ng-mouseover
• ng-keydown
• ng-keyup
• ng-keypress
AngularJS -
Forms
• Forms in AngularJS provides data-binding and validation of
input controls.
• Input Controls-Input controls are the HTML input
elements:
• input elements
• select elements
• button elements
• textarea elements
• Validate data-Following can be used to track error.
• $dirty − states that value has been changed.
• $invalid − states that value entered is invalid.
• $error − states the exact error.
AngularJS – Forms:
Textbox Example
• Data-Binding
• Input controls provides data-binding by using the ng-model
directive.
• <input type="text" ng-model="firstname">
• The application does now have a property named firstname.
• The ng-model directive binds the input controller to the rest
of your application.
• Example
• <form>
First Name: <input type="text" ng-model="firstname">
</form>
<h1>You entered: {{firstname}}</h1>
AngularJS – Forms:
Checkbox Example
• A checkbox has the value true or false.
• Apply the ng-model directive to a checkbox, and use its value in your application.
• <html>
• <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">
• </script>
• <body>
• <div ng-app="">
• <form>
• Check to show a header:
• <input type="checkbox" ng-model="myVar">
• </form>
• <h1 ng-show="myVar">My Header</h1>
• </div>
• <p>The header's ng-show attribute is set to true when the checkbox is checked.</p>
• </body>
• </html>
AngularJS – Forms:
Checkbox Example O/P
Outlin
eMVC architecture,
Angular JS : Overview,
• Example
<body ng-app="">
<div ng-include="'myFile.htm'"></div>
</body>
AngularJS – Includes:
Example
• main.htm
• tryAngularJS.htm
• <body>
Enter first name • <div ng-app = "mainApp" ng-controller="studentController">
<input type = "text" ng-model = • <div ng-include = “main.htm'"></div>
"student.firstName” > • <script>
<br> • var mainApp = angular.module("mainApp", []);
Enter last name mainApp.controller('studentController', function($scope) {
<input type = "text" ng-model • $scope.student = {
= • firstName: "Mahesh",
"student.lastName"> • lastName: "Parashar",
<br> • fullName: function() {
• var studentObject;
{{student.fullName()}}
• studentObject = $scope.student;
• return studentObject.firstName + "
" + studentObject.lastName;
• } }; });
• </script> </body>
AngularJS –
Includes: Example
O/P
Outlin
eMVC architecture,
Angular JS : Overview,
mainApp.controller('AddC', function($scope)
when('/add1',
{
{ templateUrl:
$scope.message = "This page will be used to
'add', controller: display add student form";
'AddC' });
}).
when('/view1', mainApp.controller('ViewC', function($scope) {
{ templateUrl: $scope.message = "This page will be used to
'view', controller: display view students form";
'ViewC' });
});
}]); </script>
</body>
</html>
AngularJS – Views:
Example O/P
Outlin
eMVC architecture,
Angular JS : Overview,
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller";
$scope.type = "Shape";
});
</script>
AngularJS -
Scopes
• Following are the important points to be considered in above
example.
• $scope is passed as first argument to controller during its
constructor definition.
• $scope.message and $scope.type are the models which are to
be used in the HTML page.
• We've set values to models which will be reflected in the
application module whose controller is shapeController.
• We can define functions as well in $scope.
AngularJS – Scopes:
Example
(scope Inheritance)
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller"; });
mainApp.controller("circleController", function($scope) {
$scope.message = "In circle controller"; });
mainApp.controller("squareController", function($scope) {
$scope.message = "In square controller"; });
</script>
Outlin
eMVC architecture,
Angular JS : Overview,
//define a module
var mainApp = angular.module("mainApp", []);
//create a value object as "defaultInput" and pass it a data.
mainApp.value("defaultInput", 5);
//inject the value in the controller using its name "defaultInput"
mainApp.controller('CalcController', function($scope, defaultInput) {
$scope.number = defaultInput; });
Outlin
eMVC architecture,
Angular JS : Overview,
mainApp.directive('student', function() {
var directive = {};
directive.restrict = 'E';
directive.template = "Student: {{student.name}} , Roll No: {{student.rollno}}";
directive.scope = {
student : "=name"
}
directive.compile = function(element, attributes) {
var linkFunction = function($scope, element, attributes) {
element.html("Student: "+$scope.student.name +" Roll No: "+$scope.student.rollno+">");
} return linkFunction;
} return directive;
});
AngularJS - Custom
Directives: Example
• mainApp.controller('StudentController', function($scope) {
• $scope.Mahesh = {};
• $scope.Mahesh.name = "Mahesh Parashar";
• $scope.Mahesh.rollno = 1;
• $scope.Piyush = {};
• $scope.Piyush.name = "Piyush Parashar";
• $scope.Piyush.rollno = 2;
• });
•
• </script>
•
• </body>
• </html>
AngularJS - Custom
Directives: Example O/P
Outlin
eMVC architecture,
Angular JS : Overview,
• <script src =
"https://code.angularjs.org/1.2.5/i18n/angular-
locale_da-dk.js"></script>
AngularJS –
Internationalization: Example
<html>
<body>
<div ng-app = "mainApp" ng-controller = "StudentController">
{{fees | currency }} <br>
{{admissiondate | date }} <br>
</div>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('StudentController', function($scope) {
$scope.fees = 100;
$scope.admissiondate = new Date();
});
</script>
</body>
</html>
AngularJS –
Internationalization: Example
O/P
Outlin
e
Angular JS
Node JS
Struts
Node
JS
Introduction to Node
JS
• What is Node.js?
• Node.js is an open source server framework
• Node.js is free
• Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
• Node.js uses JavaScript on the server
• What Can Node.js Do?
• Node.js can generate dynamic page content
• Node.js can create, open, read, write, delete, and close files on the server
• Node.js can collect form data
• Node.js can add, delete, modify data in your database
• What is a Node.js File?
• Node.js files contain tasks that will be executed on certain events
• A typical event is someone trying to access a port on the server
• Node.js files must be initiated on the server before having any effect
• Node.js files have extension ".js"
Introduction to Node
JS
A common task for a web server can be to open a file on the server and
return the content to the client.
How PHP or ASP handles a file request: How Node.js handles a file request:
1. Sends the task to the computer's file 1. Sends the task to the computer's file
system. system.
2. Waits while the file system opens 2. Ready to handle the next request.
and reads the file. 3. When the file system has opened
3. Returns the content to the client. and read the file, the server returns
4. Ready to handle the next the content to the client.
request.
• Node.js eliminates the waiting, and simply continues with the next
request.
• Node.js runs single-threaded, non-blocking, asynchronously
programming, which is very memory efficient.
Outlin
e
Angular JS
Node JS
Struts
Strut
s
Outlin
eStruts: Overview,
architecture,
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts
Overview
• Apache Struts 2 is an elegant, extensible framework for
creating enterprise-ready Java web applications.
• This framework is designed to streamline the
full development cycle from building, to deploying
and maintaining applications over time.
• Struts 2 Features
• Configurable MVC components
• POJO based actions
• AJAX support
• Integration support
• Various Result Types
• Various Tag support
• Theme and Template support
Struts
Overview :Features
• 1) Configurable MVC components
• In struts 2 framework, we provide all the components (view components and action) information in
struts.xml file. If we need to change any information, we can simply change it in the xml file.
• 2) POJO based actions
• In struts 2, action class is POJO (Plain Old Java Object) i.e. a simple java class. Here, you are not forced to
implement any interface or inherit any class.
• 3) AJAX support
• Struts 2 provides support to ajax technology. It is used to make asynchronous request i.e. it doesn't block
the user. It sends only required field data to the server side not all. So it makes the performance fast.
• 4) Integration Support
• We can simply integrate the struts 2 application with hibernate, spring, tiles etc. frameworks.
• 5) Various Result Types
• We can use JSP, freemarker, velocity etc. technologies as the result in struts 2.
• 6) Various Tag support
• Struts 2 provides various types of tags such as UI tags, Data tags, control tags etc to ease the
development of struts 2 application.
• 7) Theme and Template support
• Struts 2 provides three types of theme support: xhtml, simple and css_xhtml. The xhtml is default theme
of struts 2. Themes and templates can be used for common look and feel.
Outlin
eStruts: Overview,
architecture,
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts
Architecture
• The struts 2 framework is used to develop MVC-based web
application.
• Model View Controller or MVC is made up of the following
three parts −
1. Model The model represents the state (data) and business logic
of the application.
2. View The view module is responsible to display data i.e. it
represents the presentation.
3. Controller The controller module acts as an interface between
view and model. It intercepts all the requests i.e. receives input
and commands to Model / View to change accordingly.
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts
Configuration
Basic configuration which is required for a Struts 2
application.
• web.xml,
• struts.xml,
• struts-config.xml,
• struts.properties
Struts
Configuration
• The web.xml File
• The web.xml configuration file is a J2EE configuration file that
determines how elements of the HTTP request are processed
by the servlet container.
• It is not strictly a Struts2 configuration file, but it is a file that
needs to be configured for Struts2 to work.
• As discussed earlier, this file provides an entry point for any
web application
• The web.xml file needs to be created under the folder
WebContent/WEB-INF.
Struts
Configuration
• The Struts.xml File
• The struts.xml file contains the configuration information that
you will be modifying as actions are developed.
• This file can be used to override default settings for an
application, for example struts.devMode = false and other
settings which are defined in property file.
• This file can be created under the folder WEB-INF/classes.
• The first thing to note is the DOCTYPE.
• <struts> is the root tag element, under which we declare
different packages using <package> tags.
Struts
Configuration
• The Struts-config.xml File
• The struts-config.xml configuration file is a link between the
View and Model components in the Web Client
• The configuration file basically contains following main
elements −
• struts-config- This is the root node of the configuration
file.
• form-beans-This is where you map your ActionForm subclass to a
name.
• action-mappings-This is where you declare form handlers
• Controller-This section configures Struts internals
• plug-in-This section tells Struts where to find your properties
files, which contain prompts and error messages
Struts
Configuration
• The Struts.properties File
• This configuration file provides a mechanism to change the
default behavior of the framework.
• you can create this file under the folder WEB-INF/classes.
• The values configured in this file will override the default
values configured in default.properties which is contained in
the struts2-core-x.y.z.jar distribution.
Outlin
eStruts: Overview,
architecture,
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts 2 -
Actions
• Actions are the core of the Struts2 framework, as they are for
any MVC (Model View Controller) framework.
• Each URL is mapped to a specific action, which provides the
processing logic which is necessary to service the request from
the user.
• But the action also serves in two other important capacities.
• Firstly, the action plays an important role in the transfer of
data from the request through to the view, whether its a JSP
or other type of result.
• Secondly, the action must assist the framework in determining
which result should render the view that will be returned in
the response to the request.
Struts 2 -
Actions
• Create Action • Action method in the Hello World
• The only requirement for actions in
example −
Struts2 is that there must be one
noargument .
• If the no-argument method is not
specified, the default is to use the
execute() method.
• Extend the ActionSupport class
which implements six interfaces
including Action interface.
• The Action interface is as follows −
Outlin
eStruts: Overview,
architecture,
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts 2 -
Interceptors
• 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.
• Many of the features provided in the Struts2 framework are
implemented using interceptors;
• Examples include exception handling, file uploading, lifecycle
callbacks, etc. In fact, as Struts2 emphasizes much of its
functionality on interceptors
Struts 2 -
Interceptors
Few of the important interceptors are listed below −
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts 2 - Results & Result
Types
• <results> tag plays the role of a view in the Struts2 MVC
framework.
• The action is responsible for executing the business
logic.
• The next step after executing the business logic is to display
the view using the <results> tag.
• For example, if the action method is to authenticate a user,
there are three possible outcomes.
• Successful Login
• Unsuccessful Login - Incorrect username or password
• Account Locked
Struts 2 - Results & Result
Types
• Struts comes with a number of predefined result types
1. Dispatcher
2. Velocity,
3. Freemaker,
4. Redirect
5. XSLT and Tiles.
Struts 2 - Results & Result
Types
• The Dispatcher Result Type
• The dispatcher result type is the default type, and is used if no other result type
is specified. It's used to forward to a servlet, JSP, HTML page, and so on, on the
server. It uses the RequestDispatcher.forward() method.
• <result name = "success" type = "dispatcher">
• /HelloWorld.jsp
• </result>
• The FreeMaker Result Type
• Freemaker is a popular templating engine that is used to generate output
using predefined templates.
• Let us now create a Freemaker template file called hello.fm with the following
contents −
• Hello World ${name}
• The above file is a template where name is a parameter which will be passed
from outside using the defined action.
Struts 2 - Results & Result
Types
• The Redirect Result Type
• The redirect result type calls the standard
response.sendRedirect() method, causing the browser to
create a new request to the given location.
• We can provide the location either in the body of the
<result...> element or as a <param name = "location">
element. Redirect also supports the parse parameter. Here's
an example configured using XML −
• <result name = "success" type = "redirect">
• <param name = "location">
• /NewWorld.jsp
• </param >
• </result>
Outlin
eStruts: Overview,
architecture,
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts 2 - Validations
Framework
• At the Struts core, we have the validation framework that assists
the application to run the rules to perform validation before the
action method is executed.
• we will take an example of an Employee whose age ,we will put
validation to make sure that the user always enters a age in a
range between 28 and 65.
Struts 2 - Validations
Framework
• Create Main Page- JSP file index.jsp, which will be used to collect
Employee related information mentioned above.
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception handling,
annotations.
Struts2 -
Localization
• Internationalization (i18n) is the process of planning and
implementing products and services so that they can easily be
adapted to specific local languages and cultures, a process called
localization.
• The internationalization process is called translation or localization
enablement.
• Internationalization is abbreviated i18n because the word starts
with the letter “i” and ends with “n”, and there are 18 characters
between the first i and the last n.
• Struts2 provides localization, i.e., internationalization (i18n) support
through resource bundles, interceptors and tag libraries in the
following places −
• The UI Tags
• Messages and Errors.
• Within action classes.
Struts2 –
Localization
Resource
Bundles
• Struts2 uses resource bundles to provide multiple language and locale
options to the users of the web application.
• You don't need to worry about writing pages in different languages.
• All you have to do is to create a resource bundle for each language that you
want.
• The resource bundles will contain titles, messages, and other text in the
language of your user.
• Resource bundles are the file that contains the key/value pairs for the
default language of your application.
• The simplest naming format for a resource file is −
• bundlename_language_country.properties
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception
handling,
annotations.
Struts 2 - Exception
Handling
• Struts provides an easier way to handle uncaught exception and
redirect users to a dedicated error page. You can easily configure
Struts to have different error pages for different exceptions.
• Struts makes the exception handling easy by the use of the
"exception" interceptor.
• Create a file called Error.jsp
• <html>
• <body>
• This is my custom error page
• </body>
• </html>
• Let us now configure Struts to use this this error page in case of
an exception.
• Let us modify the struts.xml by adding following line−
• <result name = "error">/Error.jsp</result>
Outlin
eStruts: Overview,
architecture,
configuration,
actions,
interceptors,
result types,
validations,
localization,
exception
handling,
annotations.
Struts 2 -
Annotations
• As mentioned previously, Struts provides two forms of configuration. The
traditional way is to use the struts.xml file for all the configurations. The other
way of configuring Struts is by using the Java 5 Annotations feature. Using the
struts annotations, we can achieve Zero Configuration.
• To start using annotations in your project, make sure you have included the
following jar files in your WebContent/WEB-INF/lib folder −
• struts2-convention-plugin-x.y.z.jar
• asm-x.y.jar
• antlr-x.y.z.jar
• commons-fileupload-x.y.z.jar
• commons-io-x.y.z.jar
• commons-lang-x.y.jar
• commons-logging-x.y.z.jar
• commons-logging-api-x.y.jar
• freemarker-x.y.z.jar
• javassist-.xy.z.GA
• ognl-x.y.z.jar
• struts2-core-x.y.z.jar
• xwork-core.x.y.z.jar
Struts 2 – Annotations
Types Annotations Types
Namespace Annotation (Action Annotation)
Result Annotation - (Action Annotation)
Results Annotation - (Action Annotation)
The @Results annotation defines a set of results for an Action.
After Annotation - (Interceptor Annotation)
The @After annotation marks a action method that needs to be called after the
main action method and the result was executed. Return value is ignored.
Before Annotation - (Interceptor Annotation)
The @Before annotation marks a action method that needs to be called before
the main action method and the result was executed. Return value is ignored.
EmailValidator Annotation - (Validation Annotation)
IntRangeFieldValidator Annotation - (Validation Annotation)
RequiredFieldValidator Annotation - (Validation Annotation)
Struts 2 – Annotations
Example
Validation on Name field as required and Age as range between 28 and 65.
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.validator.annotations.*;
@IntRangeFieldValidator(message = "Age
must be in between 28 and 65", min = "28",
max = "65")