Spring Boot and JSP Tutorial

2018.05.03 15:54

졸리운_곰 조회 수:409

 

Spring Boot and JSP Tutorial

 
11 Shares
 
 

1- Objective of Post 

 
Spring is a famous framework because it supports a lot of technologies for View layer. The technologies supported for the  View layer by the Spring are JSP, Thymeleaf, Freemarker, ...
Because of the simplicity of Thymeleaf, it is considered as the default technology used for the View layer, and is automatically configured by the  Spring Boot . Therefore, if you choose the JSP for the View layer, you need to configure it.
In this post, I will show you how to create a Web application with the Spring Boot and use the  JSP to display data. The contents will be mentioned in this post:
  1. Configure to use the JSP for the View Layer
  2. Explain the operating principle of Controller & JSP.

2- Create a Spring Boot project

 
On the  Eclipse, create a  Spring Boot ​​​​​​​project.
Enter:
  • Name: SpringBootJSP
  • Group: org.o7planning.org
  • Description: Spring Boot + JSP
  • Package: org.o7planning.sbjsp
Select the technologies and libraries to be used:
  • Web
     
OK, the Project has been created.
SpringBootJspApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
package org.o7planning.sbjsp;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SpringBootJspApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringBootJspApplication.class, args);
    }
}

3- Configure pom.xml

 
Configure the libraries necessary for JSP/Servlet in the  pom.xml file:
1
2
3
4
5
6
7
8
9
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
 
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>
The full contents of pom.xml file:
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.o7planning</groupId>
    <artifactId>SpringBootJSP</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>SpringBootJSP</name>
    <description>Spring Boot + JSP</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
 
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

4- Configure JSP View

 
In the  src/main directory, create a  webapp/WEB-INF/jsp subdirectory, your JSP files will be placed into this directory.
In the next step, you need to configure to tell the  Spring Boot the place where you will put JSP files. OK, Open the application.properties file and add the following  properties  :
application.properties
1
2
3
4
5
6
7
# =============================================
# VIEW RESOLVER
# =============================================
 
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

5- Controller & JSP

 
The relationship between the  Controller and the  View is explained in the figure:
Person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.o7planning.sbjsp.model;
 
public class Person {
 
    private String firstName;
    private String lastName;
 
    public Person() {
 
    }
 
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
}
MainController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.o7planning.sbjsp.controller;
 
import java.util.ArrayList;
import java.util.List;
 
import org.o7planning.sbjsp.model.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
@Controller
public class MainController {
 
    private static List<Person> persons = new ArrayList<Person>();
 
    static {
        persons.add(new Person("Bill", "Gates"));
        persons.add(new Person("Steve", "Jobs"));
    }
 
    @RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET)
    public String index(Model model) {
 
        String message = "Hello Spring Boot + JSP";
 
        model.addAttribute("message", message);
 
        return "index";
    }
 
    @RequestMapping(value = { "/personList" }, method = RequestMethod.GET)
    public String viewPersonList(Model model) {
 
        model.addAttribute("persons", persons);
 
        return "personList";
    }
 
}
index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE HTML>
<html>
   <head>
      <meta charset="UTF-8" />
      <title>Welcome</title>
      <link rel="stylesheet" type="text/css"
                href="${pageContext.request.contextPath}/css/style.css"/>
   </head>
   <body>
      <h1>Welcome</h1>
      <h2>${message}</h2>
       
     
         
      <a href="${pageContext.request.contextPath}/personList">Person List</a
       
   </body>
   
</html>
personList.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Person List</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css"/>
  </head>
  <body>
    <h1>Person List</h1>
    
    <br/><br/>
    <div>
      <table border="1">
        <tr>
          <th>First Name</th>
          <th>Last Name</th>
        </tr>
        <c:forEach  items="${persons}" var ="person">
        <tr>
          <td>${person.firstName}</td>
          <td>${person.lastName}</td>
        </tr>
        </c:forEach>
      </table>
    </div>
  </body>
  
</html>

6- Run the application

 
On the Eclipse, right click on the project, select:
  • Run As/Spring Boot App
 
 

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
126 Creating a CRUD REST API/Service with Spring Boot, JPA and Hibernate file 졸리운_곰 2018.08.17 131
125 Spring boot에서 REST API 개발 시작해보기 file 졸리운_곰 2018.08.17 328
124 [JSP] 네이버 스마트 에디터 연동 + 이미지파일 업로드 기능 추가 file 졸리운_곰 2018.07.28 1088
123 Use React and Spring Boot to Build a Simple CRUD App file 졸리운_곰 2018.07.28 366
122 JSP CRUD Example jsp 기본예제 file 졸리운_곰 2018.07.28 141
121 스웨거 2.0으로 스프링 부트 어플리케이션 API 문서화하기 file 졸리운_곰 2018.05.24 111
120 Spring REST API 문서를 Swagger로 만들자 file 졸리운_곰 2018.05.22 390
119 Spring REST API에 Swagger 2 설정하기 file 졸리운_곰 2018.05.22 311
118 Spring Boot와 AngualrJS를 조합한 코드 자동 생성 도구(scaffolding) file 졸리운_곰 2018.05.17 117
117 Spring java Code 기반 설정 Configuration 어노테이션과 Bean 어노테이션 졸리운_곰 2018.05.14 61
116 java spring redis 세션 공유 : Spring Session을 이용한 세션 클러스터링 졸리운_곰 2018.05.06 1005
115 Spring MVC hello world example file 졸리운_곰 2018.05.04 204
114 Create a Web Application With Spring Boot file 졸리운_곰 2018.05.03 115
» Spring Boot and JSP Tutorial file 졸리운_곰 2018.05.03 409
112 Spring Boot Hello World Example – JSP file 졸리운_곰 2018.05.03 96
111 Spring Boot – JSP View Example file 졸리운_곰 2018.05.03 119
110 SpringMVC-JSP 프로젝트를 Spring Boot로 옮기기 file 졸리운_곰 2018.05.03 291
109 SPRING BOOT에서 JSP 사용하기 졸리운_곰 2018.05.03 43
108 MYBATIS 기본 - SELECTLIST file 졸리운_곰 2018.03.26 66
107 Spring MVC 에 MyBatis 적용해 보기. file 졸리운_곰 2018.03.26 69
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED