Spring boot mvc
Spring Boot mvc
Client
(Browser)
WEB
server
dB
html
Client calls a program
server returns the Response
Request and Response
Spring MVC
MVC Architecture
The requesting processing�workflow
@RequestMapping�
First Example
@Controller
@RequestMapping("/welcome")
public class WelcomeController {
@RequestMapping(value = "/greet",method = RequestMethod.GET)
@ResponseBody
public String message() {
return "Spring MVC";
}
}
http://localhost:8080/welcome/greet
Introduction to JSP Technology�
Jsp Scripting
<%
out.println("Hello From Jsp");
%>
Controller
@Controller
public class WelcomeController {
@RequestMapping("/welcome")
public String showLoginPage() {
return "welcome";
}
}
View Resolver
spring.mvc.view.prefix=/WEB-INF/pages/
spring.mvc.view.suffix=.jsp
Welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
<h2>MVC Configured Successfully</h2>
</body>
</html>
Dependency Required
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
ModelAndView object
Controller
@Controller
public class FirstController {
@RequestMapping("/first")
public ModelAndView execute()
{
String message = "Welcome to Spring!";
return new ModelAndView("Success", “msg",message);
}
}
@ModelAttribute
Model
public String init(Model model) {
model.addAttribute("majHeading", "Jeevan Blood Bank");
return "index";
}
Model
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class TripDetail {
private long tripId;
private String destination;
private double amount;
}
Controller
@Controller
public class TripController {
@Autowired
TripDetail detail;
@GetMapping("/")
public String init() {
return "index";
}
Controller
@GetMapping("/addTrip")
public String initForm(Model model) {
model.addAttribute("command",detail);
return "addTripDetails";
}
@PostMapping("/addTrip")
public String onSubmit(@ModelAttribute("data") TripDetail details) {
System.out.println(details);
return "success";
}
}
Using Spring's form tag library�
View
<form:form action="/addTrip" method="post“ modelAttribute=“command”>
<form:form action="/addTrip" method="post">
<form:input path="tripId"/>
<form:input path="destination"/>
<form:input path="amount"/>
<input type="submit" value="Add">
</form:form>
From Spring 5.0
@RequestParam
@RequestMapping ("/find")
public String get ( @RequestParam ("custId") int id, Model model) {
Customer cust =dao.findByCustomerId(id);
model.addAttribute("foundCustomer",cust);
return "Display";
}
Spring Validation
Maven Dependency
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.0.Final</version>
</dependency>
Inbuilt Validation
Inbuilt Validation
Inbuilt Validation
Model
public class BloodDonor {
private int id;
@Length(min = 3,max = 8)
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate dateOfBirth;
private String bloodGroup;
}
@Valid
Binding Result
Controller - @Valid annotation
@RequestMapping(“/donors")
public String initAddDonorForm(Model model) {
model.addAttribute("command",donor);
return "addDonor";
}
@PostMapping(path="/donors")
public String greetingSubmit(@Valid @ModelAttribute("command") BloodDonor donor, BindingResult result) {
if(result.hasErrors()) {
return "addDonor";
} else {
return "result";
}
}
Validation Input Jsp Page
<<form:form method="post" action=" donors" >
ID: <form:input path=“id" />
<form:errors path=“id" />
Name : <form:input path=“name" />
<form:errors path=“name" />
<input type="submit" value="Submit" />