博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Tutorial: Build a Spring WebMVC App with Primefaces
阅读量:6967 次
发布时间:2019-06-27

本文共 13011 字,大约阅读时间需要 43 分钟。

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 .

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 . 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:

转载地址:http://yoisl.baihongyu.com/

你可能感兴趣的文章
【Erlang新手成长日记】JSON编码与解码
查看>>
Linux下安装tomcat和jdk
查看>>
实验四+065+方绎杰
查看>>
js中函数作用域,作用域链,变量提升
查看>>
c/c++ sizeof运算符详解以及对象大小
查看>>
HDU2046 骨牌铺方格【递推】
查看>>
第二个spring,第一天
查看>>
问题集录--从初级java程序员到架构师,从小工到专家
查看>>
CSS3自定义滚动条样式 -webkit-scrollbar(转)
查看>>
VC下动态数据交换技术
查看>>
docker容器的分层思想
查看>>
决策树
查看>>
【转】app瘦身
查看>>
拓扑排序
查看>>
【转】获取Windows系统明文密码神器
查看>>
Rhel6-keepalived+lvs配置文档
查看>>
Paint.FontMetrics
查看>>
笔记本分类大全
查看>>
C/S 登录跳转/系统升级
查看>>
git的使用
查看>>