Table of Contents
Javaee tutorial 4: Generate CRUD with Forge
Preparation
- Install jboss development studio from marketplace
- Windows → Show View → Others… → Forge Console
Install plugin
forge find-plugin angularjs forge install-plugin angularjs
Getting Start with Forge
forge help project cd Project_name
When you cd into a project with Forge, it inspects the project, and detects what technologies you are using in the project. Let’s see this in action:
project list-facets
Generating the CRUD UI
Forge Scripts
Forge supports the execution of scripts. The generation of the CRUD UI is provided as a Forge script in TicketMonster, so you don’t need to type the commands everytime you want to regenerate the Admin UI. The script will also prompt you to apply all changes to the generated CRUD UI that listed later in this chapter. This would relieve us of the need to manually type in the changes.
To run the script:
run admin_layer.fsh
The Script in detail:
Generate the REST resources from the JPA entities
@/* Create REST resources from entities */; rest endpoint-from-entity --contentType application/json de.dailab.socialcloud.auctionservice.webapp.AuctionServiceWebapp.model.* --strategy ROOT_AND_NESTED_DTO;
to instruct Forge to generate JAX-RS resources for all the JPA entities in the project. The resources would be represented in JSON to enable the AngularJS-based front-end to communicate with the backend services. Each resource representation is structured to contain the representation of the corresponding JPA entity (the root) and any associated entities (that are represneted as nested objects). Note
The ROOTANDNESTED_DTO resource representation enables Forge to create REST resources for complex object graphs without adding Jackson annotations to avoid cycles in the graph. Without this constrained representation, one would have to add annotations like @JsonIgnore (to ignore certain undesirable object properties), or @JsonIdentity (to represent cycles in JSON without succumbing to StackOverflowErrors or similar such errors/exceptions).
Update the project
@/* Enable scaffolding from entities */; scaffold-x setup --scaffoldType angularjs --targetDir auction_service;
to instruct Forge to generate the css, images and JavaScript libraries used by the scaffold.
Scaffold the AngularJS UI from the JPA entities
@/* Scaffold CRUD views for the entities that an admin would start drilling down into the data model from */; scaffold-x from src/main/java/de/dailab/socialcloud/auctionservice/webapp/AuctionServiceWebapp/model/* --targetDir auction_service --overwrite;
You can either scaffold the entities one-by-one, which allows you to control which UIs are generated, or you can generate a CRUD UI for all the entities. We’ll do the latter.
Forge will prompt you for additional information when creating the scaffold. For example, you may be prompted to provide information about how different objects should be displayed in the UI. You may also be prompted to specify a different URL for the REST resources used by the AngularJS-based UI. The defaults are sufficient since this is a convenience offered by Forge to provide a different value. Forge inspects the project for existing REST resources conforming to a convention and uses it as the default. Likewise, Forge inspects JPA entities and chooses the first displayable value as the default.