pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent2.2.4.RELEASE com.candidjava Spring-Boot-Web-MVCwar 0.0.1-SNAPSHOT Spring-Boot-Web-MVC Maven Webapp http://maven.apache.org org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-thymeleafSpring-Boot-Web-MVC org.springframework.boot spring-boot-maven-plugin
application.yml
server: port: ${PORT:8081} spring: thymeleaf: cache: false check-template: true check-template-location: true content-type: text/html enabled: true encoding: UTF-8
StudentController.java
package com.candidjava.springboot.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.candidjava.springboot.models.Student; import com.candidjava.springboot.service.StudentService; @Controller @RequestMapping(value = "/student") public class StudentController { @Autowired StudentService service; @GetMapping("/getAll") public String get(Model model) { Liststudents = service.getAllStudents(); model.addAttribute("students", students); return "student"; } }
student.html
No students yet!
Students
ID Name Age Related Articles
StudentService.java
package com.candidjava.springboot.service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Service; import com.candidjava.springboot.models.Student; import com.candidjava.springboot.service.StudentService; @Service public class StudentService { private ListstudentList = new ArrayList (Arrays.asList( new Student("1", "ram", "20"), new Student("2", "arun", "21"), new Student("3", "karthick", "22") )); public List getAllStudents() { return studentList; } }
Student.java
package com.candidjava.springboot.models; public class Student { private String id; private String name; private String age; public Student(String id, String name, String age) { this.id = id; this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
SpringWebMVCApplication.java
package com.candidjava.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringWebMVCApplication { public static void main(String[] args) { SpringApplication.run(SpringWebMVCApplication.class, args); } }
This post first appeared on Core Java,Servlet,Jsp,Struts,Hibernate,Spring Fram, please read the originial post: here