Skip to main content

JSF-PrimeFaces

JSF basics

Web pages can be created in facelets using the standard JSTL
We 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
Concept of template...
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 container
Use @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 

  • added applicationContext.xml file in WEB-INF location(for spring beans)
This is a regular spring bean config file which i also made


--------------

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

Comments

Popular posts from this blog

Hive

Hive What is Transactional Processing? * Small data * recent data * updates data * real time * narrow scope - single data source What is Analytical Processing? * Mostly batch * Mostly old data * Mostly long running * wide scope multiple data source to give a big picture ----------- RDBMS (mostly for transactional) * Single m/c with backup, structured data, usually one source Its hard to do analytics on RDBMS Big Data (mostly for analytical using data warehouse) * distributed data, semi/unstructured, auto replication with fault tolerance, usually different source and different format Not always... * hbase is special db that can be configured for transactional processing even if its on big data * qlikview can be used for analyitial processing even if its running on a single m/c --------- Whats a Data Warehouse? Technology that aggregates data  from one or more sources for analytical processing used by long running jobs, lagged data, large data, multi s...

Simple tutorial to create RESTful web services using SPRING, GRADLE & ECLIPSE

How to create RESTful web services using SPRING, GRADLE & ECLIPSE * First install Eclipse  i n your machine by referring to the official wiki  (I have installed an eclipse version called Kepler in my machine) * After installing Eclipse, open it and go to "eclipse market place" to add Gradle as seen in below screenshots: * Now create a new blank Gradle project in Eclipse *Now lets build the blank project using Gradle to ensure that everything is fine so far * Now change the build.gradle file to below as seen in http://spring.io/guides/gs/rest-service/ buildscript { repositories { maven { url "http://repo.spring.io/libs-release" } mavenLocal () mavenCentral () } dependencies { classpath ( "org.springframework.boot:spring-boot-gradle-plugin:1.1.4.RELEASE" ) } } apply plugin : 'java' apply plugin : 'eclipse' apply plugin : 'idea...

Simple FitNesse tutorial

(Time spent for writing - 1:30 min) In simple words, FitNesse is a testing framework that can be used to develop automated test cases. I had searched many posts but could not find a single tutorial that could help me get started with FitNesse. So writing it now... BTW I am a java developer, so this tutorial is in pure java! I use windows XP and Java version is 1.5.0 To get started first download FitNesse from http://fitnesse.org/ I didnt like its 2 minute example, because that just showed what it does, doesnt really help me to start with development. They should have a real getting started tutorial there, I think. I had downloaded fitnesse20070619.zip from downloads section After downloading, install it by unzipping the all the files to a some location like c:\fitnesse c:\fitnesse will then act as the root folder for you and all the classpaths will be relative to root folder By default FitNesse will run on port 80. If you want another port you can edit the run.bat file in the root fol...