Skip to content

Latest commit

 

History

History
190 lines (141 loc) · 5.13 KB

spring-data-with-apache-causeway-get-started.adoc

File metadata and controls

190 lines (141 loc) · 5.13 KB

Sample Application with Spring Boot and Apache Causeway

Back to Main Page

Overview

Apache Causeway is a Java framework for creating web applications using the Naked Objects pattern.

In this tutorial, we’ll explore how to use Apache Causeway to generate UI on a Spring Boot based backend. For an introduction to Apache Causeway refer to the documents at the official site https://causeway.apache.org.

Note
We use lombok (https://projectlombok.org/) annotations through out this tutorial. If you are not familiar with these yet, just ignore them as these are optional anyway.

Setup

Let’s start by adding Maven dependencies similar to a standard Spring Boot application:

<parent>
    <groupId>org.apache.causeway.app</groupId>
    <artifactId>causeway-app-starter-parent</artifactId>
    <version>2.0.0-xxx</version>
    <relativePath/>
</parent>
Note
Lookup the latest available release version from Maven Central

Backend Service

We’ll use an Employee entity with firstName and lastName properties to perform CRUD operations on it:

@Entity
public class Employee {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName;
    private String lastName;
}

Here’s the simple, corresponding Spring Data repository – to manage the CRUD operations:

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

}

Let’s pre-populate the DB with a few sample Employees:

@Bean
public CommandLineRunner loadData(EmployeeRepository repository) {
    return (args) -> {
        repository.save(new Employee("Bill", "Gates"));
        repository.save(new Employee("Mark", "Zuckerberg"));
        repository.save(new Employee("Sundar", "Pichai"));
        repository.save(new Employee("Jeff", "Bezos"));
    };
}

Apache Causeway UI

View Model to manage Employee instances

The EmployeeManager class is meant to represent a web page, to be rendered by Apache Causeway' UI logic.

We want this page to show a collection of all the Employees that are persisted in our EmployeeRepository.

public class EmployeeManager {

    @Inject private EmployeeRepository employeeRepo;

    public List<Employee> getAllEmployees(){
        return employeeRepo.findAll();
    }
}

Mixin to add new Employees

We can now extend our EmployeeManager above with new functionality. Let’s add business logic that creates a new Employee.

We do this by introducing a new class, a so called Mixin. This involves some naming convention, for both the class and its main method act.

@RequiredArgsConstructor
public class EmployeeManager_newEmployee {

    @Inject private EmployeeRepository employeeRepo;

    private final EmployeeManager holder;

    public EmployeeManager act(String firstName, String lastName) {
        Employee newEmployee = new Employee(firstName, lastName);
        employeeRepo.save(newEmployee);
        return holder;
    }
}

Menu

We need one more class to tell the Apache Causeway UI logic which Actions we want it to render in the top menu. With this tutorial we only have one. As we’ll see later, we map the employeeManager() method to a menu entry named Employee Manager.

@RequiredArgsConstructor(onConstructor_ = { @Inject })
public class EmployeeMenu {

    final FactoryService factoryService;

    public EmployeeManager employeeManager(){
        return factoryService.viewModel(EmployeeManager.class);
    }
}

Wiring the Components Together

@SpringBootApplication
@Import({
    CausewayModuleCoreRuntimeServices.class, // Apache Causeway Runtime
    CausewayModuleJpaEclipselink.class, // EclipseLink as JPA provider for Spring Data
    CausewayModuleExtModelAnnotation.class, // @Model support
    CausewayModuleViewerWicketViewer.class, // UI (Wicket Viewer)
    CausewayModuleSecurityBypass.class // Security (Bypass, grants all access)
})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

    @Bean
    public CommandLineRunner loadData(EmployeeRepository repository) { // (1)
        return (args) -> {
            repository.save(new Employee("Bill", "Gates"));
            repository.save(new Employee("Mark", "Zuckerberg"));
            repository.save(new Employee("Sundar", "Pichai"));
            repository.save(new Employee("Jeff", "Bezos"));
        };
    }
}
  1. Initializes the repository (database) with some values on startup.

Warning

some TODOs here

more details on pom.xml
application.yml
we left out Apache Causeway specific annotations above, but need to add and explain these here

Conclusion

In this article, we wrote a CRUD UI application using Spring Data JPA for persistence and Apache Causeway for presentation.

The code is available on GitHub.