0% found this document useful (0 votes)
2 views6 pages

SpringBootdevelopmentGuidelines GEMS25

This document provides detailed notes on creating a Spring Boot project, including setting up dependencies, creating controllers, and returning JSP pages. It covers a 'Hello World' demo, returning JSP pages with model attributes, and implementing multi-page navigation with form handling. Key configurations and code snippets are included to guide the user through the development process.

Uploaded by

hardenilam77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

SpringBootdevelopmentGuidelines GEMS25

This document provides detailed notes on creating a Spring Boot project, including setting up dependencies, creating controllers, and returning JSP pages. It covers a 'Hello World' demo, returning JSP pages with model attributes, and implementing multi-page navigation with form handling. Key configurations and code snippets are included to guide the user through the development process.

Uploaded by

hardenilam77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Spring Boot Project notes:

SPRING-BOOT PROJECT CREATION

● Add plugin for jsp – help-eclipse market place – search for jsp – Eclipse
enterprise java web developer toolkit…
● Create spring boot web application on url https://start.spring.io/
● Select
o Maven, Java (17), Spring boot version > 3+ (At present 3.3.0)
o proj metadata - group = com.springboot, artifact = demoproj01
o It will form package and name (do not change it)
o Packaging = jar, Java version = Java17
o Add required dependencies [Spring Web, Thymeleaf, Lombok,
Spring data JPA, Validation
o Click Generate.
● This generates the project zip file. Extract it and paste it in workspace.
● Load this project from file import->“open project from file system”
● Open pom.xml and add following dependencies (without this it will
download application)
<!--
https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--
https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api -->
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>

o Details: go to maven repo – mvnrepository.com and search “tomcat jasper”


o Go to maven dependencies of project and see tomcat version. Pickup same version for tomcat
jasper. Copy the code and paste in maven dependencies.

SPRING-BOOT PROJECT – “Hello World level” DEMO

● Create Spring MVC Controller


o Create A “controller” package in src/main below application
package (“com.springboot”)
o Create Controller class with @Controller annotation
o Add service method named home() with @RequestMapping
(“home”)
@RestController
@RequestMapping("/")
public class HelloController {

@GetMapping("/home")
public ResponseEntity<List<String>> home() {
List<String> messages = new ArrayList<String>();
messages.add("Good morning");
messages.add("Hello");
try {
return ResponseEntity.status(HttpStatus.CREATED).body(messages);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}
}

o Right click on the project (file with main(…))->run as “Spring


Boot App”. In built Tomcat Server will start and indicate in
Terminal
o Open the browser and type http://localhost:8080/home
o You should see the collection of two strings.

SPRING-BOOT PROJECT – Return home.jsp page

o Make sure following dependencies are included in pom file [Read


carefully about these dependencies at
https://stackoverflow.com/questions/4928271/how-to-install-jstl-
it-fails-with-the-absolute-uri-cannot-be-resolved-or-una
o ]
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

● SPRING-BOOT PROJECT – Return home.jsp page



o Add following properties to application.properties file.
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
server.port = 8080

o Properties are provided in property files.


o Create controller which returns template of jsp page name as a
string (“home” for home.jsp) and Service class to provide sample
list (separation of concerns).

@Controller
@RequestMapping("/")
public class HelloController {
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("messages", HomeService.messages());
return "home";
}
}

public class HomeService {


public static List<String> messages(){
List<String> messages = new ArrayList<>();
messages.add("Hi");
messages.add("Hello");
messages.add("Good Morning");
messages.add("Good Afternoon");
messages.add("Will meet again");
return messages;
}
}

o Create folders webapp/WEB-INF/jsp under src/main


o Add home.jsp inside above jsp folder. Jsp page can access the
parameters passed from controller method.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Messages</title>
</head>
<body>
<h1>ALL MESSAGES HERE</h1>
<c:forEach items="${messages}" var = "messages">
<tr>
<td>${messages}</td>
</tr>
</c:forEach>
${Temp}
${Force}
</body>
</html>

o Run the application.


o Check on browser http://localhost:8080/home.
o We should see the jsp page output.

● SPRING-BOOT PROJECT – Return home.jsp page with access to


request objects (Request object usage Not recommended)
● We can access HttpServletRequest object in controller as follows. But
its not best practice. Instead we use Model interface.
● The parameters passed will be available on jsp page.
@Controller
@RequestMapping("/")
public class HelloController {
@GetMapping("/home")
public String home(HttpServletRequest request, Model model) {
//Accessing HttpServletRequest Object
System.out.println(request.getServerName());
System.out.println(request.getServerPort());

// Adding single parameter


model.addAttribute("messages", HomeService.messages());
model.addAttribute("messages", HomeService.messages());

//adding map as a parameter


Map<String, Integer> c = new HashMap<>();
c.put("Temp", 35);
c.put("Volume", 120);
c.put("Force", 10);
model.addAllAttributes(c);
return "home";
}
}

SPRING-BOOT PROJECT – Return home.jsp page using ModelAndView


encapsulation.

● In the above controller we will make changes:


● Observe we are using Controller for typical web application.
@Controller
@RequestMapping("/")
public class HelloController {

@GetMapping("/home")
public ModelAndView home(ModelAndView mav) {

// Adding single parameter


mav.addObject("messages", HomeService.messages());

//adding map as a parameter


Map<String, Integer> c = new HashMap<>();
c.put("Protein", 35);
c.put("Carbs", 120);
c.put("Fats", 10);
mav.addAllObjects(c);
mav.setViewName("home");
return mav;
}

SPRING-BOOT PROJECT – Multipage navigation.

● Create new Spring-boot application: add minimum required


dependency of “Spring web”
● Open pom file and add mandatory dependencies for jsp pages.
<!--
https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--
https://mvnrepository.com/artifact/jakarta.servlet.jsp.jstl/jakarta.servlet.jsp.jstl-api -->
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>

● Create a controller in controller package.

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

@GetMapping ("/welcome")
public ModelAndView welcome() {
System.out.println("Insode welcome");
ModelAndView mav = new ModelAndView();
mav.addObject("welcome_message", "Welcome to the Kailash Tours");
mav.setViewName("ask_name");
return mav;
}

@PostMapping ("/display")
public ModelAndView displayName(@RequestParam(name = "user_name") String user)
{
System.out.println("In display: " + user);
ModelAndView mav = new ModelAndView();
mav.addObject("greetings", "Hi " + user);
mav.setViewName("greetings");
return mav;
}
}

● Create jsp pages (ask_name.jsp, greetings.jsp) under


main/webapp/WEB-INF/views.

<%@ page language="java" contentType="text/html; charset=UTF-8"


pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>WELCOME PAGE</h1>
<form action = "display" method ="post">
<table>
<tr>
<td><h1>${welcome_message}</h1></td>
</tr>
<tr>
<td><input type="text" name="user_name" /></td>
</tr>
<tr>
<td><input type="submit" name="submit_name" /></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"


pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "display" method ="post">
<table>
<tr>
<td><h1>${greetings} Thanks for joining</h1></td>
</tr>
</table>
</form>
</body>
</html>

● Add mapping of jsp pages path to application.properties in resources


spring.application.name=multipage
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
server.port = 8080

● Run the application and test on browser with localhost:8080/welcome


● Observe how the code control passes through controller methods.
● Control passes through jsp pages.

You might also like