Primefaces Spring Boot Hello World Example

Primefaces Spring Boot Hello World Example – Primefaces Tutorial

Hello and welcome again to elgarnaoui.com, in this tutorials we will create a Hello world using spring boot and JSF primefaces framework.

So let’s start :

Tools used in this tutorial :

  • Eclipse 2020-03 or IntelliJ IDEA Community Edition 2020.2
  • Spring boot 2.3.2.RELEASE
  • Java 8
  • Primefaces 5.1
  • Myfaces-impl 2.2.6

Let’s create our project :

In eclipse click new > other > Spring Starter Project and click next then choose the name of your project, group, artifact and version :

Spring boot and Jsf primefaces tutoriels

Next shoose the spring boot version and other dependencies like devtools :

Spring boot and jsf tuto

Click Finish, then the project is created now.

Project Structure :

This is the web project structure build using Maven.

Spring boot primefaces tuto project structure

Maven dependencies :

In pom.xml file of your maven project, add the dependencies below.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.sample</groupId>
	<artifactId>sample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>sample</name>
	<description>Demo project for Spring Boot</description>
	<packaging>jar</packaging>

	<properties>
		<java.version>8</java.version>
	</properties>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-impl</artifactId>
            <version>2.2.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-api</artifactId>
            <version>2.2.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
			<groupId>org.primefaces.extensions</groupId>
			<artifactId>all-themes</artifactId>
			<version>1.0.8</version>
		</dependency>
        <dependency>
			<groupId>org.ocpsoft.rewrite</groupId>
			<artifactId>rewrite-servlet</artifactId>
			<version>2.0.12.Final</version>
		</dependency>
		
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Model class :

In package model create a managed bean java class HelloModel.java with these annotations from JSF to make this class a ManagedBean

@ManagedBean(name = "model", eager = true)
@SessionScoped

HelloModel.java as below :

package com.sample.demo.model;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 * Created by elgarnaoui.com.
 */

@ManagedBean(name = "model", eager = true)
@SessionScoped
public class HelloModel {
	
	private String hello = "Hello /";
	private String world = "World/";
	
	
	public String getHello() {
		return hello;
	}
	public void setHello(String hello) {
		this.hello = hello;
	}
	public String getWorld() {
		return world;
	}
	public void setWorld(String world) {
		this.world = world;
	}
}

View Class :

In view package create a HelloView.java class extends from the ManagedBean java class HelloWorld.java as below :

package com.sample.demo.view;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import com.sample.demo.model.HelloModel;

/**
 * Created by elgarnaoui.com.
 */

@ManagedBean(name = "hello", eager = true)
@RequestScoped
public class HelloView extends HelloModel{

	public HelloView() {
		super();
		// TODO Auto-generated constructor stub
	}
}

Demo package :

In this package create FacesRewriteConfigurationProvider.java and Initializer.java and update your SampleAppliction.java as below :

SampleAppliction.java

package com.sample.demo;

import java.util.EnumSet;

import javax.faces.webapp.FacesServlet;
import javax.servlet.DispatcherType;

import org.ocpsoft.rewrite.servlet.RewriteFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by elgarnaoui.com.
 */

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com.sample.demo"})
public class SampleApplication extends SpringBootServletInitializer{

	public static void main(String[] args) {
		SpringApplication.run(SampleApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SampleApplication.class, Initializer.class);
	}

	@SuppressWarnings("rawtypes")
	@Bean
	public ServletRegistrationBean servletRegistrationBean() {
		FacesServlet servlet = new FacesServlet();
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf");
		return servletRegistrationBean;
	}

	@SuppressWarnings("unchecked")
	@Bean
	public FilterRegistrationBean rewriteFilter() {
		FilterRegistrationBean rwFilter = new FilterRegistrationBean(new RewriteFilter());
		rwFilter.setDispatcherTypes(EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST,
				DispatcherType.ASYNC, DispatcherType.ERROR));
		rwFilter.addUrlPatterns("/*");
		return rwFilter;
	}

}

FacesRewriteConfigurationProvider.java

package com.sample.demo;

import javax.servlet.ServletContext;

import org.ocpsoft.rewrite.annotation.RewriteConfiguration;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
import org.ocpsoft.rewrite.servlet.config.rule.Join;

/**
 * Created by elgarnaoui.com.
 */
@RewriteConfiguration
public class FacesRewriteConfigurationProvider extends HttpConfigurationProvider {

	   @Override
	   public int priority()
	   {
	     return 10;
	   }

	   @Override
	   public Configuration getConfiguration(final ServletContext context)
	   {
	     return ConfigurationBuilder.begin()
	       .addRule(Join.path("/sample/").to("/index.jsf"))
	       .addRule(Join.path("/sample/hello/").to("/index.jsf"));
	    }
}

Initializer.java

package com.sample.demo;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 * Created by elgarnaoui.com.
 */

@Configuration
public class Initializer implements org.springframework.boot.web.servlet.ServletContextInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.err.println("------------------------------------");
        servletContext.setInitParameter("primefaces.CLIENT_SIDE_VALIDATION", "true");
        servletContext.setInitParameter("primefaces.THEME", "bootstrap");
    }

}

Webapp folder :

In this folder create 2 files.xhtml with taglibs from JSF, JSF core, Facelets and PrimeFaces:

layout.xhtml as below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
<f:view>
    <h:head>
        <title><ui:insert name="title">Default title</ui:insert></title>
    </h:head>
    <h:body>
        <div class="ui-grid ui-grid-responsive">
            <div class="ui-grid-row">
                <div class="ui-grid-col-3"></div>
                <div class="ui-grid-col-6"></div>
                <div class="ui-grid-col-3"></div>
            </div>
            <div class="ui-grid-row">
                <div class="ui-grid-col-3"></div>
                <div class="ui-grid-col-6">
                    <p:growl for="errors" showDetail="true" autoUpdate="true"/>
                    <ui:insert name="content">Default content</ui:insert>
                </div>
                <div class="ui-grid-col-3"></div>
            </div>
        </div>
    </h:body>
</f:view>
</html>

index.xhtml as below :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui"  >
    <ui:composition template="layout.xhtml">

        <ui:define name="content">
            <p:panelGrid columns="2">
                <h:outputText value="#{hello.hello}"/>
                <h:outputText value="#{hello.world}"/>
            </p:panelGrid>
        </ui:define>

    </ui:composition>
</html>

Run Application :

Now go to SampleApplication.java > right click >Run As > Java Application and when the tomcat running go to your browser and type http://localhost:8080/sample/hello/ or http://localhost:8080/sample/ as your config in FacesRewriteConfigurationProvider.java and you will get a hello world as :

hello world spring boot and primefaces

Thank you for reading this post, to support us share it on social media, and if you have any issues, request or question let me know in the comment section 🙂

Code source :

Similar Posts

57 Comments

  1. you’re in reality a excellent webmaster. The web site loading velocity is incredible.
    It kind of feels that you’re doing any distinctive trick.

    Also, The contents are masterwork. you have performed a fantastic process on this matter!

  2. Awesome blog! Is your theme custom made or did you download it
    from somewhere? A theme like yours with a few simple tweeks would really make my blog jump out.
    Please let me know where you got your theme. Bless you

  3. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four
    e-mails with the same comment. Is there any way you can remove people from that service?
    Many thanks!

  4. I got this website from my friend who informed me regarding this web site and at the moment this time I am browsing
    this web page and reading very informative content
    at this time.

  5. naturally like your web-site but you need to check the spelling on quite a few of your posts.
    Several of them are rife with spelling problems
    and I find it very bothersome to tell the truth however I’ll definitely come back again.

  6. I have been surfing on-line greater than 3 hours nowadays,
    but I never discovered any fascinating article like yours.
    It’s beautiful worth sufficient for me. Personally, if all site owners and bloggers made just right content material
    as you probably did, the net might be a lot more useful than ever before.

  7. Pretty element of content. I simply stumbled upon your blog and in accession capital to
    say that I acquire in fact enjoyed account your blog posts.

    Anyway I’ll be subscribing to your augment or even I fulfillment you get right
    of entry to constantly rapidly.

  8. What’s up i am kavin, its my first time to commenting anywhere, when i read this post i thought i could also create comment due
    to this good piece of writing.

  9. When someone writes an post he/she maintains the idea of a user in his/her brain that how a
    user can be aware of it. Thus that’s why this article is perfect.

    Thanks!

  10. This design is steller! You obviously know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start
    my own blog (well, almost…HaHa!) Wonderful job. I really loved what you had to say,
    and more than that, how you presented it. Too cool!

  11. Have you ever thought about adding a little bit more than just your articles?
    I mean, what you say is valuable and all. However think of if you added some great photos or videos to give your posts more, “pop”!
    Your content is excellent but with pics and clips, this blog could definitely be
    one of the best in its field. Very good blog!

  12. I was curious if you ever thought of changing the page layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or 2 images.
    Maybe you could space it out better?

  13. My spouse and I stumbled over here coming from a different website and thought I might check things out.
    I like what I see so i am just following you. Look forward to looking at your
    web page repeatedly.

  14. Does your site have a contact page? I’m having trouble locating it but, I’d
    like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested
    in hearing. Either way, great website and I look
    forward to seeing it develop over time.

  15. We are a group of volunteers and starting a new scheme in our community.
    Your web site offered us with valuable information to work on. You’ve done a formidable job and our whole community will be thankful
    to you.

  16. Neat blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your theme. Many thanks

  17. Simply desire to say your article is as surprising.
    The clarity in your post is simply excellent and i could assume you are an expert on this subject.
    Well with your permission allow me to grab your
    RSS feed to keep up to date with forthcoming post. Thanks a million and
    please carry on the rewarding work.

  18. What you composed was actually very logical.
    However, what about this? suppose you were to write a awesome headline?

    I ain’t saying your information isn’t good, but what if you added a post title to possibly grab a
    person’s attention? I mean Primefaces Spring
    Boot Hello World Example – Primefaces Tutorial is kinda vanilla.
    You ought to glance at Yahoo’s front page and watch how they create post headlines to grab
    viewers interested. You might try adding a video or a related picture or two to get readers excited about what you’ve written. In my opinion,
    it could bring your posts a little bit more interesting.

  19. Hey! I know this is kind of off-topic however I needed to ask.

    Does managing a well-established website like yours take a massive
    amount work? I’m completely new to operating a blog but I do
    write in my journal daily. I’d like to start a blog so I can share my personal experience and thoughts online.
    Please let me know if you have any kind of recommendations or tips
    for new aspiring bloggers. Appreciate it!

  20. Definitely believe that which you said. Your favorite justification appeared to be on the web
    the easiest thing to be aware of. I say to you, I definitely get annoyed while people think about
    worries that they plainly do not know about. You managed to hit the nail upon the top and also defined out
    the whole thing without having side-effects , people
    can take a signal. Will probably be back to get more.

    Thanks

  21. Good day! I know this is somewhat off topic but I was wondering which blog platform
    are you using for this site? I’m getting sick and tired of WordPress
    because I’ve had issues with hackers and I’m looking at alternatives for another platform.

    I would be fantastic if you could point me in the direction of a good platform.

  22. When someone writes an piece of writing he/she maintains
    the thought of a user in his/her mind that how a user can know it.

    So that’s why this piece of writing is perfect.
    Thanks!

  23. Howdy this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you
    have to manually code with HTML. I’m starting
    a blog soon but have no coding skills so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  24. Hello there I am so grateful I found your blog, I really found you by mistake, while I was browsing on Digg for something else,
    Regardless I am here now and would just like to say many thanks for
    a remarkable post and a all round enjoyable blog (I also love the theme/design), I don’t have time to go through it all at the moment but I have book-marked
    it and also added in your RSS feeds, so when I have time I will
    be back to read much more, Please do keep up the superb work.

  25. certainly like your web-site however you need to
    take a look at the spelling on quite a few of your posts.
    Many of them are rife with spelling issues and I find
    it very bothersome to inform the truth on the other
    hand I will definitely come back again.

  26. Do you have a spam problem on this website; I also am a blogger, and I was wanting to
    know your situation; we have created some nice procedures and we are looking to
    exchange methods with others, be sure to shoot me an e-mail if interested.

  27. Hello there! This is my first comment here so I
    just wanted to give a quick shout out and say I really enjoy reading through your articles.

    Can you suggest any other blogs/websites/forums that cover the same subjects?
    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *