act-gallery / excel-obsolete-with-response_type-prj

Demonstrate how to generate csv/excel report in ActFramework
Apache License 2.0
1 stars 1 forks source link

Excel Demo App

IMPORTANT: This application is obsolete, it is replaced by response-type.

This application demonstrate how to render excel report in an ActFramework.

Quick start

To start in dev mode:

cd /path/to/a/app
mvn clean compile exec:exec

To start in prod mode:

cd /path/to/a/app
mvn clean package
cd target/dist
unzip *.zip
./start

Once you started you can navigate browser to http://localhost:5460, there you should be able to see the home page of the application:

image

Inside the App

What this app trying to demonstrate is to show how to create an Excel sheet of a list of model entities, specifically in this demo, a list of employees.

The Employee Model

@Data
public class Employee {

    enum Grade {
        E06, E07, E08, E09, E10, E11
    }

    @Label("工号")
    public String id;

    @Label("名")
    public String firstName;

    @Label("姓")
    public String lastName;

    @Label("级别")
    public Grade grade;

}

Note:

  1. There is a @Label annotation on top of properties of Employee, they will be used to render the column header of csv and excel files.

  2. In the app there is a TestDataGenerator class which is used to generate a random list of Employee records.

The Service

public class EmployeeService {

    @LoadCollection(TestDataGenerator.class)
    private List<Employee> employees;

    @GetAction
    @PropertySpec(cli = "id, firstName, lastName, grade")
    public List<Employee> list() {
        return employees;
    }

    @GetAction("template")
    public List<Employee> template() {
        return employees;
    }

}

In this service there is an employee list injected with annotation @LoadCollection, which get fed with random employee list generated by TestDataGenerator class.

There are two request handler methods (endpoints) defined in the service class:

  1. list() - returns the employee list on GET / request
  2. template() - returns the employee list on GET /template request

Views

The HTML template

The HTML template resources/rythm/demo/excel/EmployeeService/list.html is used to render response for request with Accept=text/html header, which is the home page when we navigate browser to http://localhost:5460.

In the page we have 2 major parts:

  1. A group of links
  2. Employee list table

The employee table renders all employees in the employees list injected into EmployeeService instance

The links are for getting employee list in different data presentation:

An immediate question one might ask is there is no request handlers defined for these URLs, how can we get the response sent to these endpoints?

The answer is they all direct to GET / endpoint with different Accept header rewritten to the request.

If we open the /resources/app.properties file we can see the following configuration:

content_suffix.aware=true

This configuration tells ActFramework to treat /{content-suffix} as Accept header written, thus when it received a request to /json, it rewrite the Accept header of incoming request to application/json and route to / endpoint, similar things happen for /csv, /xls and /xlsx URLs.

The excel templates

There are two excel templates defined in resources/excel/demo/excel /EmployeeService/ folder:

Both of them are defined using JXLS templating engine:

image

These templates will be used to service request sent to /template an it could easily see the rendered excel file are different from request sent to / which is rendered directly into excel file without template.

The direct rendered excel file

image

The template rendered excel file

image

Summary

This demo app shows

  1. How to generate excel file directly
  2. How to generate excel file via JXLS template
  3. How to use content_suffix.aware=true to allow ActFramework overwrite Accept header of incoming request