Tutorial: Build a Spring WebMVC App with Primefaces

Primefaces is a JavaServer Faces (JSF) component suite. It extends JSF’s capabilities with rich components, skinning framework, a handy theme collection, built-in Ajax, mobile support, push support, and more. A basic input textbox in the JSF tag library becomes a fully-featured textbox with theming in Primefaces.

Frontend frameworks like AngularJS provide UI components, Ajax capabilities, and HTML5 compliance much like Primefaces does. If you are looking for a lightweight application with quick turnaround time, AngularJS could be your best bet. However, when dealing with an enterprise Java architecture, it is often best to use a mature framework like Primefaces. It is stable and ever-evolving, with the help of an active developer community.

Primefaces also makes a UI developer’s life easier by providing a set of ready-to-use components which, otherwise, would take a considerable amount of time to code – e.g., the dashboard component with drag and drop widgets. Some other examples are slider, autocomplete components, tab views for pages, charts, calendars, etc.

Spring WebMVC and Primefaces

In Spring WebMVC, components are very loosely coupled. It is easy to integrate different libraries to the model layer or the view layer.

In this tutorial, I am going to walk you through using Spring WebMVC and Primefaces to create a basic customer management application with a robust frontend. All the code can be found on Github.

Create a Maven Project

Create a new Maven Project using your favorite IDE. After creating the project, you should see the pom.xml in the project folder. A minimal pom.xml should like this:

 

 

Add Spring Libraries

Next, add the necessary Spring libraries to the dependencies section of the pom.xml.

 

 

Create Your Sample Project with Spring WebMVC

For the customer management application we are going to build, we need to create a mock customer database. It will be a POJO with three attributes. The Customer class would look like this:

 

 

Then we need to create a bean class to manipulate the Customer class:

 

 

Create the Frontend with Primefaces

Since we are going to add Primefaces components to our UI, we will need a UI with JSF capabilities. Add the JSF dependencies to your pom.xml:

 

 

Note: If your target server is a Java EE compliant server like jBoss, the JSF libraries will be provided by the server. In that case, the Maven dependencies can conflict with the server libraries. You can add scope provided to the JSF libraries in the pom.xml to solve this.

 

 

Create a web deployment descriptor – web.xml. The folder structure needs to be as shown below (the other files referenced will be created below):

 

 

web.xml content:

 

 

Create faces-config.xml in the WEB-INF folder:

 

 

Add index.xhtml to the webapp folder.

 

 

Note the XML namespaces for the JSF included in the xhtml. Now we can add the the proper dependencies to pom.xml.

 

 

Finally, add a class implementing WebApplicationInitializer interface. This will be a bootstrap class for Servlet 3.0+ environments, to start the servlet context programmatically, instead of (or in conjunction with) the web.xml approach.

 

 

Configure Primefaces

Now we will modify the index.xhtml file and create a data table to display the customer data. The xml namespace needs to be modified to add Primefaces reference.

 

 

Deploy to Your Application Server (and Test)

Build the project, deploy the war to the application server and check.

Extended Capabilities

Modify the code as shown below to easily produce a sortable data table with filters. Add the following line to CustomerBean.java:

 

 

…and:

 

 

Modify index.xhtml to:

 

 

More on Primefaces

All the UI components available with Primefaces have been showcased at Primefaces Showcase.

Apart from the components extended from the JSF Tag library, Primefaces has a lot of versatile components and plugins known as Primefaces extensions. They are meant to make developers’ lives easier and our web pages more beautiful.

And now, it’s time to add authentication to your Primefaces webapp! Learn more, and take Stormpath for a test drive, with these resources:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值