JSF basics
Web pages can be created in facelets using the standard JSTLWe can use templates
We can manage resource files like stylesheets, javascripts etc using resource libraries and resource library contracts
Facelets is based on xhtml
To use JSF with tomcat check the following page:
http://stackoverflow.com/documentation/jsf/916/getting-started-with-jsf#t=201704050012362807453
I will use MyFaces here.
Will also add maven tomcat plugin (http://tomcat.apache.org/maven-plugin-trunk/tomcat7-maven-plugin/usage.html) to run tomcat from maven.
Here is the sample project https://github.com/bhajanpreets/JSFBase/tree/v1-jsf-basic/jsf-basic
To run I use "clean install tomcat:run" target
We use the elements from specific xml namespace with h prefix as seen below...
Usually in JSP we declare tag libraries, in JSF we use xml namespaces as done above
These are the basic tag libraries in JSF
HTML UI components library for various basic html components (as seen above)
xmlns:h="http://xmlns.jcp.org/jsf/html"
Facelets tag library used for templating
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
Core tag library for lots of things like input listeners, validators, converters,view parameters, ajax etc
xmlns:f="http://xmlns.jcp.org/jsf/core"
Composite Component tag library for creating composite components using special templating tags
xmlns:cc="http://xmlns.jcp.org/jsf/composite"
Passthrough Attributes for support for HTML5 (use pure html components instead of jsf)
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
Passthrough Elements for support for HTML5 (use pure html components instead of jsf)
xmlns:jsf="http://xmlns.jcp.org/jsf"
Demo of how to add template using facelets tag library
https://github.com/bhajanpreets/JSFBase/tree/v2-jsf-template-basic/jsf-facelets-01
What we want...
How we got it...
We can use ui:decorators to decorate pages partially
For example the images below can be template using decorator
We can have a template definition
https://github.com/bhajanpreets/JSFBase/blob/v3-jsf-template-decorate/jsf-facelets-01/src/main/webapp/templates/image-summary.xhtml
And a template usage
https://github.com/bhajanpreets/JSFBase/blob/v3-jsf-template-decorate/jsf-facelets-01/src/main/webapp/index.xhtml
Resources
Right way to use resources in JSF is to use the resource library. First of all resources should be in the right directory src/main/webapp/resources (not like maven where we have src/main/resources)Then we use it using special tags. Here are examples of how to use this for stylesheets and images...
https://github.com/bhajanpreets/JSFBase/blob/v4-resource-library/jsf-facelets-01/src/main/webapp/product-details.xhtml
https://github.com/bhajanpreets/JSFBase/blob/v4-resource-library/jsf-facelets-01/src/main/webapp/templates/page-template.xhtml
Resources can also be packaged separately from the war file so that they can be reused in different projects
To do that...
Resource library contracts
can be used to switch resource libraries dynamically. For example when we want to change the complete layout of pages (make them darker/lighter or make them compliant with mobile/desktop etc)Adding business logic to our JSF application using Managed Beans
Managed bean lifecycle is handled by containerUse @Inject that's a part of CDI (Standard J2EE API for Contexts and Dependency Injection)
Requirements of CDI
* Java bean should have no-arg constructor or constructor with @Inject annotation
* Managed beans are injected into each other using @Inject
* Scope of a bean is specified using scope annotation (could be standard CDI scope or JSF special scope or custom scope)
* @Named can be used to specify name to the managed bean to access it in facelet pages
Scopes
Standard CDI scopes annotations are specified in javax.enterprise.context
We have @RequestScoped (over HTTP request - example while persisting data), @SessionScoped (over session - example user login), @ApplicationScoped (over app contextshared by all users), @ConversationScoped (multiple requests in a session - defined by programmer - example some wizard in the webpage)
JSF Scopes
@ViewScoped (all requests while in the same page)
@FlowScope (to do with faces flows)
Managed beans with Session, View , Conversation scope must be serializable (java.io.Serializable) so that app server can save data to disk while session is on. When we have serializable class in java all fields in that class should also be serializable (or should be transient) else we will get not serializable exception
This makes things complex. For example if we have an application scoped non serialized bean injected in view scoped bean(that is serialized) the app server will handle this by itself by injecting a proxy of application scoped bean. The proxy is serializable. Don't make the application bean injected above transient, else it wont be saved and cause NPE.
---------------------------------------------------
Primefaces
Tools
Eclipse
Tomcat7
Maven
Import basic project
Convert to dynamic web project in eclipse from properties -> project facets
Update maven to use for tomcat start stop
Start Tomcat and check hello world
-------------------------------------------------------------------------------
How to use spring hibernate with jsf
TODO list the main steps in details, in general I had separate JSF project and separate Spring Hibernate project. In order to merge the two
- mainly updated web.xml ( in web.xml i had context-param tag that had param-name as contextConfigLocation and param-value as classpath:/jpaContext.xml. I added space and /WEB-INF/applicationContext.xml in existing param-value.
- faces-config.xml (added SpringBeanFacesELResolver). In faces-config.xml I had
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
I think due to some error i started using
org.springframework.web.jsf.el.SpringBeanFacesELResolver
it worked
This is a regular spring bean config file which i also made org.springframework.web.jsf.el.SpringBeanFacesELResolver
it worked
- added applicationContext.xml file in WEB-INF location(for spring beans)
--------------
How to integrate spring security?
checked spring security tutorial and integrated with coreservlets spring....
one frequent error i get is below (when i start tomcat server)
Apr 20, 2017 11:00:59 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter] while setting constructor argument with key [1]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallableProcessingInterceptor
CallableProcessingInterceptor is in spring-web
added this artifact from maven and running the code again
have to add spring core now
added this artifact from maven and running the code again
have to add spring core now
Comments