This application demonstrate how to render excel report in an ActFramework.
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:
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.
@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:
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.
In the app there is a TestDataGenerator class which is used to generate a random list of Employee records.
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:
GET /
requestGET /template
requestThe 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:
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:
/json
- get employee list in JSON data/csv
- get employee list in CSV file/xls
- get employee list in xls file/xlsx
- get employee list in xlsx file/template/xls
- get employee list in xls file - rendered with predefined
template/template/xlsx
- get employee list in xlsx file - rendered with
predefined templateAn 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.
There are two excel templates defined in resources/excel/demo/excel /EmployeeService/
folder:
Both of them are defined using JXLS templating engine:
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
The template rendered excel file
This demo app shows
content_suffix.aware=true
to allow ActFramework overwrite
Accept
header of incoming request