Spring Introduction
Spring Framework and part of it is Spring Boot, that we will be using.
Spring boot --> will give the dependencies as well as it will take care of the configuration that's the best part of using spring boot
Embedded Tomcat: so here our jar file is having an embedded tomcat which means it can be directly run on any server without much configuration
Like: having a Linux server --> then we have to install tomcat --> then deploy the WAR or JAR --> spring boot saves us from this
Spring-boot-starter web Spring boot-starter-jdbc
So we have the project out of the box.
Application.properties file is used for all kinds of properties
Dependency Injection :
- Let's say one class wants an object of another class in that sense we will use the dependency injection, to to create an object for us. So that we don’t want to create the object by ourself
- In older projects, we can define in the XML for the object creation
- In Spring boot we can use Autowire which will take care of object creation for us.
- @Component, this will lose the compelling . And improve the code.
Spring Container :
The below mentioned line is the starting position of the project and it will basically create the Spring container.
Here is the main method and, the main method is the starting position and the run command will basically create the spring container.
1 public static void main(String[] args) {
2 //this line will basically create the spring container
3 SpringApplication.run(DashboardpracticeApplication.class, args);
4 }
@Component
Spring will understand that it will create the object of the classes where we have the @component mentioned at top of the classes.
Spring framework will use the design pattern of the singleton pattern
How to Create First Controller ?
1. Create a class
2. Add the controller at the top
3. Create a method
4. Add the request mapping
5. Retuirn the .jsp file
6. Create the jsp file in the webapp.
Spring Boot Tutorials | Full Course
How to Add properties in the application.properties :
We can do some configuration in the application.properties like , we wants to mention the folder from which we should have all the pages stored like html, jsp or any other We can mention the extension of the file.
1 #path of page is defined by prefix
2
3 spring.mvc.view.prefix=/pages/
4 # Extension of the page is defined by suffix
5 spring.mvc.view.suffix=.jsp
Controller where we are returing the page
1 @Controller
2 public class HomeController {
3
4 @RequestMapping("/")
5 public String home() {
6 System.out.println("inside home ");
7 return "Home";
8 }
9
10 }
What is JSTL and EL format ?
Here we are printing the name , and this name is set by the controlller using the ModelAndView add object and hence we can directly use it in the jsp file.
<html>
<head>
<meta charset="ISO-8859-1">
<title>About</title>
</head>
<body>
<h4>This page is about</h4>
${name}
</body>
</html>
-
How ModelAndView Works ?
1 @RequestMapping("about")
2 public ModelAndView about(@RequestParam("name") String name) {
3
4 System.out.println("inside about ");
5 System.out.println("Parameter reeceived is "+name);
6
7 ModelAndView mv=new ModelAndView();
8
9 //now adding the object that is passed.
10 mv.addObject("name",name);
11 //here we have set the view name
12 mv.setViewName("About");
13 return mv;
14 }
How to receive request param as a object and use that in the jsp file ?
Here we are sending the multiple parameter in the request and storing it in the object and showing the information in the jsp page
Here alien is a class and then adding it in the mv object . And then we can use that in the jsp file.
1 @RequestMapping("alien")
2 public ModelAndView alientech(Alien al) {
3
4 ModelAndView mv=new ModelAndView();
5 mv.addObject("alien",al);
6 mv.setViewName("Alien");
7
8 return mv;
9 }
JSP file
1 <h3> Alien Id is : ${alien.aid } and name is ${alien.aname}</h3>
What is Spring Data JPA and H2 ?
H2 is a in memory database. Read more on this JPA and H2 with spring boot .
https://start.spring.io/
Above mentioned url can be used to initialize the new spring projects.