Review
In the previous section, we declared our configuration using JavaConfig and compared it side-by-side with an XML-based configuration. In this section, we will discuss the remaining layers of our application.
Table of Contents
Click on a link to jump to that section:
- Functional Specs
- Creating the View
- HTML Mockup
- Thymeleaf Integration
- JavaConfig
- ApplicationContext.java
- SpringDataConfig.java
- ThymeleafConfig.java
- ApplicationInitializer.java
- Layers
- Domain
- Service
- Controller
- Running the application
- Clone from GitHub
- Create the Database
- Run with Maven and Tomcat 7
- Run with Maven and Jetty 8
- Import to Eclipse
- Validate with W3C
Layers
Here we'll discuss the Domain, Repository, Service and Controller layers.
Domain
Our domain layer consists of two simple classes: User.java and Role.java. If you'd been following my previous tutorials, you will notice that these are the same domain classes we'd been using before. Both classes had been annotated with @Entity which means these are JPA entities and will be persisted to a database.These classes represent a user with the following properties: first name, last name, username, role, and password (we're not actively using the password field).
For role, we only have two values: an admin or a regular user.
User.java
Role.java
Controller
Our controller is a standard controller providing CRUD requests. The most important lines here are the following:// Create
model.addAttribute("users", UserMapper.map(users));
model.addAttribute("commanduser", new UserDto());
model.addAttribute("usertype", "new");
model.addAttribute("users", UserMapper.map(users));
model.addAttribute("commanduser", new UserDto());
model.addAttribute("usertype", "new");
// Update
model.addAttribute("users", UserMapper.map(users));
model.addAttribute("commanduser", UserMapper.map(repository.findOne(id)));
model.addAttribute("usertype", "update");
model.addAttribute("users", UserMapper.map(users));
model.addAttribute("commanduser", UserMapper.map(repository.findOne(id)));
model.addAttribute("usertype", "update");
These lines adds three attributes to the model:
- users - contains all users
- commanduser - the form backing object or the command object of the form
- usertype - an attribute to determine if the request is a new user or existing user
0 komentar:
Post a Comment