View
199
Download
3
Category
Preview:
Citation preview
Java EE Web project
Contents
● Java EE project setup● Introduction to Maven● Web application structure● JSF basics● CDI basics● GIT overview (optional)
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
Java EE project setup in Netbeans
Prerequisites:● Netbeans 7 or 8● Glassfish application server● Download prepared sources from GitHub
DEMO
Presentation of the demo application in IDE and running on app server
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
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
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)
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
DEMO
Example of Maven pom.xml.Example of running maven in IDE.
Multitiered application model
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.
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
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
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
JSF Lifecycle
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
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
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
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
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; } }
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
Basic components - layout
● h:panelGroup - container - <span> or <div>● h:panelGrid - grid layout container (table)
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
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
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
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
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
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
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
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
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
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
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();
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
Producers - example
class MyVarProducer {@Produces @RequestScopedpublic Var createVar(@New Var var) {
var.setState("NEW");return var;
}}
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
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
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
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
Recommended