Skip to content

prominic/grails4-primefaces-demo

Repository files navigation

Summary

Example project for grails4-primefaces plugin.

Dependencies

This plugin was written and tested with Grails 4.0.0.M2 and Java 8.0.202 (AdoptOpenJDK, HotSpot JVM)

Running the Example Interface

Clone the plugin project.

Install the plugin:

cd grails4-primefaces
./gradlew install

Run this application:

cd grails4-primefaces-demo
./grailsw run-app    

Once the application finishes loading, check the interface at: https://localhost:8080/faces/anagraphic/home.xhtml

Building an Example From a New Domain Class

Generate the new domain class

Run this command:

./grailsw create-domain-class demo.Car

Populate the domain class at grails-app/domain/demo/Car.groovy:

package demo
 
class Car {
    String carID
    int year
    String brand
    String color
 
    static constraints = {
    }
}

Generate the service and bean

The primefaces plugin adds a command to generate the managed bean and service for the domain class:

./grailsw run-command pf-generate-all demo.Car

The resulting files are:

  • grails-app/services/demo/CarService.groovy
  • src/main/groovy/demo/beans/CarLazyDataModel.groovy
  • src/main/groovy/demo/beans/CarManagedBean.groovy

Update application.yml

Add the new package "demo.beans" to the grails.plugins.primefaces.beans.packages list in grails-app/conf/application.yml

grails:
    plugins:
        primefaces:
                beans:
                   packages: com.company.demo.beans,demo.beans

Optionally, add this line to the bottom of grails-app/conf/logback.groovy to enable debugging output:

logger('demo', DEBUG)

Add some test data

Add this code to grails-app/init/grails4/primefaces/demo/BootStrap.groovy

...
 
import demo.Car
import java.util.UUID
 
 
class BootStrap {
 
    def init = { servletContext ->

        ... init already has code for Anagraphic.  Add the code below at the end of the block ...
 
        // initialize Car data
        def colors = ['Black', 'White', 'Green', 'Red', 'Blue', 'Orange', 'Silver', 'Yellow', 'Brown', 'Maroon']
        def brands = ['BMW', 'Mercedes', 'Volvo', 'Audi', 'Renault', 'Fiat', 'Volkswagen', 'Honda', 'Jaguar', 'Ford']
        // enumerate all color and brand combinations
        def combinations = [colors, brands].combinations()
        Car.withTransaction {
            combinations.each{ row ->
                String color = row[0]
                String brand = row[1]
                // initialize a car entry
                int newYear = (int) (Math.random() * 50 + 1960)
                String newID = UUID.randomUUID().toString().substring(0, 8);
                Car car = new Car (carID:newID, year:newYear, color:color, brand:brand)
                car.save(flush:true, failOnError:true)
            }
        }
 
        Car.withTransaction() {
            log.info "Generated ${Car.count()} Car entries"
        }

    }
 
    ...
}

This code will run on startup and generate some Car objects to populate the datatables.

Create the XHTML file:

Create src/main/webapp/car/home.xhtml:

<html xmlns="https://www.w3.org/1999/xhtml"
      xmlns:h="https://java.sun.com/jsf/html"
      xmlns:f="https://java.sun.com/jsf/core"
      xmlns:p="https://primefaces.org/ui"
      xmlns:pe="https://primefaces.org/ui/extensions" >
  
<h:head>
    <title>Car Demo</title>
</h:head>
<h:body>
  
    <h:form id="carForm">
  
        <p:dataTable var="car"
                     value="#{carMB.cars}"
                     selection="#{carMB.car}"
                     rowKey="#{car.carID}"
                     rows="10"
                     paginator="true"
                     rowsPerPageTemplate="10,20,50"
                     lazy="true"
                     selectionMode="single"
                     currentPageReportTemplate="#{message.i18n('primefaces.datatable.currentPageReportTemplate.label', '{startRecord}', '{endRecord}', '{totalRecords}')}"
                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                     >
            <p:column headerText="Id">
                <h:outputText value="#{car.carID}" />
            </p:column>
  
            <p:column headerText="Year">
                <h:outputText value="#{car.year}" />
            </p:column>
  
            <p:column headerText="Brand">
                <h:outputText value="#{car.brand}" />
            </p:column>
  
            <p:column headerText="Color">
                <h:outputText value="#{car.color}" />
            </p:column>
        </p:dataTable>
    </h:form>
  
</h:body>
</html>

Run the application

Launch the application with this command:

./grailsw run-app

Open the new interface in a browser: https://localhost:8080/faces/car/home.xhtml

Built With

Authors

License

Moonshine-IDE is licensed under the Apache License 2.0 - see the LICENSE.md file for details

Acknowledgments