Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

spring boot web mvc application example

pom.xml

4.0.0org.springframework.boot
		spring-boot-starter-parent
		2.2.4.RELEASEcom.candidjava
	Spring-Boot-Web-MVC
	war0.0.1-SNAPSHOTSpring-Boot-Web-MVC Maven Webapphttp://maven.apache.orgorg.springframework.boot
			spring-boot-starter-web
		org.springframework.boot
			spring-boot-starter-thymeleaf
		Spring-Boot-Web-MVCorg.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) {
  List students = service.getAllStudents();
  model.addAttribute("students", students);

  return "student";
 }

}

student.html

No students yet!

Students

ID Name Age

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 List studentList = 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

Share the post

spring boot web mvc application example

×

Subscribe to Core Java,servlet,jsp,struts,hibernate,spring Fram

Get updates delivered right to your inbox!

Thank you for your subscription

×