Skip to content
José Vieira Neto edited this page Dec 15, 2017 · 8 revisions

Commands

Scaffold

There are several commands the main one is the scaffold where it generates for you the model, repository, service, controler and the view with thymeleaf.

When running spring scaffold -n "User" -p "name: String email: String"

you will see.

created src/main/java/com/example/model/UserModel.java
created src/main/java/com/example/repository/UserRepository.java
created src/main/java/com/example/service/UserService.java
created src/main/java/com/example/controller/UserController.java
created src/main/resources/layout.html
created src/main/resources/templates/user/index.html
created src/main/resources/templates/user/form.html
created src/main/resources/templates/user/show.html

with this simple command you already have a complete crud (create, read, update, delete).

it is also possible to generate a scaffold for api rest:

spring scaffold -n "User" -p "name: String email: String" -r "true"

Controller

Another very common command is the spring controller -n "Home"

Output:

created src/main/java/com/example/controller/HomeController.java
created src/main/resources//templates/home/index.html

Files generated:

HomeController.java

package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class HomeController {

	@GetMapping
	public String index() {
	    return "home/index";
	}

}

home.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
	  layout:decorator="layout">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="" />
    <meta name="author" content="" />
</head>
<body>

	<div layout:fragment="content">
	    <h1>Home</h1>
	    <hr />
	</div>
</body>
</html>

Model

command spring model -n "Admin" -p "name:String email:String"

Output:

created src/main/java/com/example/model/AdminModel.java

@Entity
@Table(name = "admins")
public class AdminModel implements Serializable {

	 private static final long serialVersionUID = 1L;
	
	 @Id @GeneratedValue(strategy = GenerationType.AUTO)
	 private Integer id;

	 @Column(name="name")
	 private String name;

	 @Column(name="email")
	 private String email;

	 public void setId(Integer id) { 
	 	this.id = id; 
 	 }
	 
	 public Integer getId() { 
	 	return id; 
 	 }

	 public void setName(String name) {
		 this.name = name;
	 }

	 public String getName() {
		 return this.name;
	 }

	 public void setEmail(String email) {
		 this.email = email;
	 }

	 public String getEmail() {
		 return this.email;
	 }
}
Clone this wiki locally