SpringBoot (15) Let's start (4/10). Defining a CRUD form from scratch with one simple table/class.

 

0. Introduction

In a previous post, we could see how to define CRUD form in the YAML file src/main/resources/config/menus/menus.yml

Let's define a CRUD form that counts from one number to another and displays the counting process to the form.

These steps should be followed:

  1. Define the menu item in the YAML file and also update the field idFills from the parent menu item
  2. As no special form is needed the field url should be assigned "/crud"
  3. Create a Java class PRGrade in the package ximo.xotherapps.model.proves that extends AuditIdLong class and implements Base interface
  4. Create the database and schema
  5. Create i18n elements


1. Updating the menus.yml file

Here is the menus.yml file. Notice the yellow elements.


1
2
3
4
5
6
7
menus:
  items:
    - { id: 8, name: Informatica,    idFills: [8-1, 8-2, 8-3, 8-4] } 
    - { id: 8-4, name: Creating Grades, url: '/crud',
         modelClassName: PRGrade, pkg: ximo.xotherapps.model.proves, 
         action: c 
      }

In this menu item, we are going to CREATE new "grades" as action="c"

2. Creating the Entity class 

Let's create the package ximo.xotherapps.model.proves (it is recommended to be in ximo.xotherapps.model package)

Create the PRGrade class in this package

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package ximo.xotherapps.model.proves;

import org.hibernate.envers.Audited;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;

import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import ximo.annotations.HideInGridEdu;
import ximo.xotherapps.model.base.AuditIdLong;
import ximo.xotherapps.model.base.Base;

@Entity

@Table(name = "prGrade", schema = "prova" //, 
       //uniqueConstraints = @UniqueConstraint(columnNames =  { "programa", "usuari", "entityadm", }),
       //indexes = {@Index (name = "idx_usuari_entityadm", columnList = "usuari, entityAdm")}
)
@Audited
//@ToString 
@NoArgsConstructor
public class PRGrade  extends AuditIdLong implements Base {

	@HideInGridEdu private static final long serialVersionUID = 1L;
	/*****************************************************************
	 * 1. Field definition
	 *****************************************************************/
	@Getter @Setter @NotNull
	@Size(max = 256)
	@Column(unique=true, length=256)
	private String description="fist level"; 
	
	@Getter @Setter @NotNull
	private int level=1; 
	
	@Getter @Setter @NotNull
	@Size(max = 256)
	@Column(length=256)
	private String remarks=""; 
	
	@Getter @Setter
	@Transient
	int idioma=0; //0=Valencia 1=castellà 
	
	@Override  //For defining i18n  
	public String getI18nprefix() {
		return "proves";
	}
		
		
	/*****************************************************************
	 * 1. @Override of Base methods
	 *****************************************************************/
	//@Override public String getCRUDFormName() {return "RHPlazaPersona_page";}
	//@Override public default String getProcessClassAfterSave() {	return "";}	
	//@Override public default String getTitleAfterSave() {	return "after.title";}
	//@Override public default String getSubtitleAfterSave() {	return "after.subtitle";}
	//@Override public default String getPageAfterSave() {	return "aftersave_page";}
	//@Override public List<FieldEdu> getProcessParamsAfterSave() {

	//@Override public int validate(Locale locale) {
	//@Override public Map<String,String> ajaxButton(String buttonValue) {
	//@Override @HideInGridEdu public Object getSharedInfo() {
	
	//@Override public void getInitializedBase(Map<String, Object> mp) {
	//@Override public void addDefaultFormValors(Map<String, Object> mpObject) {
		
	//@PrePersist 	private void updateFieldPersist(){
	//@PreUpdate 	private void updateFieldUpdate(){
		
		
	/*******************************************************************
	 * 8. Other
	 ******************************************************************/
	
	public boolean otherStuff() {
		return true;
	}
}

There are only a few class attributes. There are a lot of commented lines that let us figure out some of the possibilities. 

3. Creating the Database and schema

Use pgadmin to create the database and the schema

Define the database connection in the application.yml configuration file. It is described in this post.

4. Creating the i18n elements

The properties files are in the src/main/resources/i18n/messages_XX_XX.properties

Each name of the fields of the class is reflected in these files. Note that the prefix proves has been added.

Also the input abm.prgrade must be inserted in this file that consists of the prefix "abm." and the name of hte class in lower case letters.


Note that the suffix shown in the method "public String getI18nprefix() "for defining th ei18n keys (proves.description, ...)

If you want to internationalize errors, use the files 

 src/main/resources/i18n/errors_XX_XX.properties



5. Execution

In the menu


And our form is









Comentarios

Entradas populares de este blog

SpringBoot (6) Spring Data JPA (1)

SpringBoot (10) Authentication (1). Server certificate, application.yml and Application.java

Spring Boot (2) Websockets. Subscriptions of only one member