40
Java EE Web project

Java EE web project introduction

Embed Size (px)

Citation preview

Page 1: Java EE web project introduction

Java EE Web project

Page 2: Java EE web project introduction

Contents

● Java EE project setup● Introduction to Maven● Web application structure● JSF basics● CDI basics● GIT overview (optional)

Page 3: Java EE web project introduction

Java EE project setup in Eclipse

Prerequisites:● Eclipse Luna● WildFly application server● Maven support in Eclipse - m2eclipse● Git support in Eclipse eGIT● JBoss Tools for Eclipse● Download prepared sources from GitHub

Page 4: Java EE web project introduction

Java EE project setup in Netbeans

Prerequisites:● Netbeans 7 or 8● Glassfish application server● Download prepared sources from GitHub

Page 5: Java EE web project introduction

DEMO

Presentation of the demo application in IDE and running on app server

Page 6: Java EE web project introduction

Introduction to Maven

● project build and configuration tool● it can:

o execute build tasks using command lineo automatically download necessary libriaries

(dependencies)o configure project independently from IDE

Page 7: Java EE web project introduction

Introduction to Maven - artifacts

Artifacts are files produced by a maven build.

They are specified by:● groupId● artifactId● version● packaging - JAR, WAR, EJB, EAR, POM, ...An artifact can be dependency of another module

Page 8: Java EE web project introduction

Maven - goals and phases

To execute maven build: > mvn goalGoal can be a name of phase that we want to complete. All phases before that phase are completed in order.

The order of basic maven phases:

compile, test, package, integration-test, installSteps to complete each phase may differ between module types (packging)

Page 9: Java EE web project introduction

Maven - dependencies

Specified by: groupId, artifactId, version, type (jar is default type)

Scope of a dependency:

● compile (default)

● test - only used when running unit tests

● provided - not included in artifact

Page 10: Java EE web project introduction

DEMO

Example of Maven pom.xml.Example of running maven in IDE.

Page 11: Java EE web project introduction

Multitiered application model

Page 12: Java EE web project introduction

Java EE Web application

In simplest for a single WAR file.● WEB-INF folder

o web.xmlo faces-config.xmlo optionally beans.xml for CDIo classes folder with compiled java classes

● web resources in root foldero JSF facelets, css, images, etc.

Page 13: Java EE web project introduction

WAR Maven module

Single module of type WAR● pom.xml specifies:

o packaging WARo dependency on javaee-api (scope=provided)

● src/main/java - java sources● src/main/webapp - Web resources● src/main/resources - classpath resources

Page 14: Java EE web project introduction

JSF overview

● run by FacesServlet mapped in web.xml● configured by faces-config.xml and contex

parameters in web.xml● FacesServlet executes several phases in

MVC styleo Request -> Execute code in Java Bean -> Create

view from Facelet -> Response

Page 15: Java EE web project introduction

JSF lifecycle

● Restore view phase● Apply request values phase; process events● Process validations phase; process events● Update model values phase; process events● Invoke application phase; process events● Render response phase

Page 16: Java EE web project introduction

JSF Lifecycle

Page 17: Java EE web project introduction

JSF component tree

● logical tree of UI components● created from a definition in facelet● encoded to xhtml at the end of the request

o to bring new state of the view to browser (new page)● recreated at the begining of the request

o to represent the current state of the view before request is processed

Page 18: Java EE web project introduction

JSF Facelets

● XHTML based templates for a web page● define components in a component tree● specify binding to Java code

o datao conditional renderingo controller methods / listeners

Page 19: Java EE web project introduction

JSF Managed Beans

● provide controller methods and data model for facelets

● bound to component properties using Expression Language

● identified in facelets by textual name● marked by @Named CDI qualifier

Page 20: Java EE web project introduction

Expression language

EL is a script used in facelets to bind values to component properties● Results in a value - primitive values, object,

even a method pointer● Fields are converted to getters and setters● Never throws a Null Pointer -> blank value

instead

Page 21: Java EE web project introduction

Usage of Expression language

● enclosed in #{ ... }● do not mix with ${ … } used in JSP

o JSP and JSTL tags should not be mixed with JSF

● in facelet: rendered="#{ bookView.displayed }

● in BookView bean:@Named class BookView {

public boolean isDisplayed() { return true; } }

Page 22: Java EE web project introduction

Basic components - display

● h:outputText - textual output in <span>● h:outputLabel - <label>● h:outputLink - <a href…>● h:messages - display of JSF messages● h:graphicImage - <img>● h:dataTable - table for collection of rows

Page 23: Java EE web project introduction

Basic components - layout

● h:panelGroup - container - <span> or <div>● h:panelGrid - grid layout container (table)

Page 24: Java EE web project introduction

Basic components - Forms

● h:inputText● h:inputTextarea● h:selectManyListbox, h:selectOneMenu, etc.

- selection components, listboxes, checkboxes

● h:form - all input components must be in some form to function

Page 25: Java EE web project introduction

Basic components - actions

● All action components submit form and call action on web server via javascript

● They accept link to a listener method

● h:commandButton● h:commandLink

Page 26: Java EE web project introduction

Contexts & Dependency Injection

● Injection: instead of myVar = Factory.getVar() or myVar = new Var() declare that I need Var

● @Inject Var myVar● instance of Var is created by the container● works only in container managed beans

o JSF beans, Servlets, EJBs, all injected beans - generally Java objects created by the container

Page 27: Java EE web project introduction

Dependency injection rules

● never use “new” on a bean with a CDI dependency

● all injected beans must have constructor without parameters

● do not put initialization to constructor but to method marked with @PostConstruct

Page 28: Java EE web project introduction

Lifecycle of injected beans

● when bean createdo constructor is executed firsto then are dependencies injectedo method marked with @PostConstruct is called

● during injection, beans are either created or reusedo it depends on current context of execution and

scope of injected bean

Page 29: Java EE web project introduction

Basic scopes

● Dependent (default)o a new bean is always created for injectiono similar to calling constructor directly

● Application scopeo single bean is created for whole applicationo the same bean is reused afterwards, retaining its

complete stateo singleton pattern

Page 30: Java EE web project introduction

Basic Java EE scopes

Most scopes are bound to a lifecycle of a specific action● Request scope

o bound to lifecycle of HTTP requesto within HTTP request, only single instance of request

scoped bean is created and reused● Session scope

o bound to lifecycle of HTTP session

Page 31: Java EE web project introduction

Java EE scope annotations

package javax.enterprise.context:

● Dependent (optional)

● ApplicationScoped

● RequestScoped

● SessionScoped

Only single instance of Java object with defined scope is created and reused for single application, request, session

Page 32: Java EE web project introduction

Dependency declaration

● inject by classo inject instance of the class or its subclass

● inject by interfaceo inject instance of java class with given interface

● inject by qualifiero inject instance with given qualifiero qualifier is a java annotation marked with @Qualifier

For one injection point, one class should match

Page 33: Java EE web project introduction

Runtime / conditional injection

@Inject Instance<Var> myVarInjector;…myVar = myVarInjector.get();

● matching bean is resolved when necessary● can be injected directly when needed● resolution errors can be treted at runtime

Page 34: Java EE web project introduction

Creating new CDI beans

● possible to create a completely new CDI bean regardless of context

● @New annotation overrides bean scope● can be used instead of new keyword:@Inject @New Instance<Var> varInjector;...Var myVar1 = varInjector.get();...Var myVar2 = varInjector.get();

Page 35: Java EE web project introduction

Producers

● CDI usually creates new beans via constructor without parameters

● Producers allow to customize creation● instead of a class, a method of a producer is

marked with annotations and scopeo this method returns created bean of matching typeo it is called when bean with the same definition is

requested

Page 36: Java EE web project introduction

Producers - example

class MyVarProducer {@Produces @RequestScopedpublic Var createVar(@New Var var) {

var.setState("NEW");return var;

}}

Page 37: Java EE web project introduction

Introduction to GIT

GIT is a source code version control system.

It is a distributed version control system.● multiple repositories can be interconnected

and synchronized

Page 38: Java EE web project introduction

GIT repository

● single instance of storage of versioned files● stores latest version of files, history of

changes, metadata (commit messages), and connection to other upstream repositories

● repositories are organized in a treeo from upstream repositories to downstream

Page 39: Java EE web project introduction

Simplest GIT topology

● Central GIT repository - accesible via public linko local GIT repositoryo local GIT repositoryo local GIT repository

● Local repositories exist only on local computerso they are created from central using clone command

Page 40: Java EE web project introduction

2-step synchronisation● clone - downloads new copy of repository● edit files

o add (add new files if created)o edited and removed files are already handled

● commit - save changes locally (checkpoint)● pull - download changes from central repo

o merge and resolve conflicting changeso commit if files changed

● push - upload locally commited changes