├── .travis.yml ├── LICENSE ├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── company │ └── app │ ├── Application.java │ ├── controller │ ├── BookController.java │ └── HomeController.java │ ├── model │ └── Book.java │ └── repository │ └── BookRepository.java ├── resources └── application.yml └── webapp ├── static └── js │ └── main.js └── view ├── book.jsp ├── books.jsp └── home.jsp /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | install: true 3 | script: 4 | - mvn -B verify && cd .. 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 github.com/egnaf 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/egnaf/spring-jsp-example.svg)](https://travis-ci.org/egnaf/spring-jsp-example) 2 | 3 | # spring-jsp-example 4 | 5 | ## Stack 6 | - Spring Boot 2.2.0.RELEASE 7 | - Servlet API 2.5 8 | - JSP, HTML, JS, CSS 9 | - Tomcat 9 10 | 11 | ## Install 12 | 1. To clean and build maven project, use: 13 | ```bash 14 | mvn clean install 15 | ``` 16 | 2. To create and run the Spring boot application, run the following code: 17 | ```bash 18 | $ mvn spring-boot:run 19 | ``` 20 | 21 | ## Contribute 22 | For any problems, comments, or feedback please create an issue 23 | [here](https://github.com/egnaf/spring-web-jsp-example/issues). 24 |
25 | 26 | ## License 27 | Project is released under the [MIT](https://en.wikipedia.org/wiki/MIT_License). 28 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.company 8 | app 9 | 1.0 10 | war 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 1.8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 2.2.0.RELEASE 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | org.apache.tomcat.embed 31 | tomcat-embed-jasper 32 | provided 33 | 34 | 35 | javax.servlet 36 | servlet-api 37 | 2.5 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-data-jpa 42 | 43 | 44 | mysql 45 | mysql-connector-java 46 | 8.0.28 47 | 48 | 49 | org.projectlombok 50 | lombok 51 | 1.18.10 52 | 53 | 54 | javax.servlet 55 | jstl 56 | 1.2 57 | 58 | 59 | 60 | 61 | 62 | ${project.artifactId} 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /src/main/java/com/company/app/Application.java: -------------------------------------------------------------------------------- 1 | package com.company.app; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/company/app/controller/BookController.java: -------------------------------------------------------------------------------- 1 | package com.company.app.controller; 2 | 3 | import com.company.app.model.Book; 4 | import com.company.app.repository.BookRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.ui.Model; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | 11 | import java.util.List; 12 | import java.util.Optional; 13 | 14 | @Controller 15 | public class BookController { 16 | 17 | private final BookRepository bookRepository; 18 | 19 | @Autowired 20 | public BookController(BookRepository bookRepository) { 21 | this.bookRepository = bookRepository; 22 | } 23 | 24 | @RequestMapping("/books") 25 | public String getBooks(Model model) { 26 | List books = bookRepository.findAll(); 27 | model.addAttribute("books", books); 28 | return "books"; 29 | } 30 | 31 | @RequestMapping("/books/{id}") 32 | public String getBookById(Model model, @PathVariable int id) { 33 | Optional book = bookRepository.findById(id); 34 | book.ifPresent(value -> model.addAttribute("book", value)); 35 | return "book"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/company/app/controller/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.company.app.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | 6 | @Controller 7 | public class HomeController { 8 | 9 | @RequestMapping({"/", "/home"}) 10 | public String home() { 11 | return "home"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/company/app/model/Book.java: -------------------------------------------------------------------------------- 1 | package com.company.app.model; 2 | 3 | import lombok.Data; 4 | 5 | import javax.persistence.*; 6 | 7 | @Entity 8 | @Table(name = "books") 9 | @Data 10 | public class Book { 11 | 12 | @Id 13 | @GeneratedValue(strategy = GenerationType.IDENTITY) 14 | private int id; 15 | 16 | @Column(name = "name") 17 | private String name; 18 | 19 | @Column(name = "author") 20 | private String author; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/company/app/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.company.app.repository; 2 | 3 | import com.company.app.model.Book; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface BookRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | 4 | spring: 5 | mvc: 6 | view: 7 | prefix: /view/ 8 | suffix: .jsp 9 | profiles: 10 | active: dev 11 | main: 12 | banner-mode: off 13 | datasource: 14 | driver-class-name: com.mysql.cj.jdbc.Driver 15 | url: jdbc:mysql://localhost:3306/books?serverTimezone=UTC 16 | username: root 17 | password: rootPassword 18 | jpa: 19 | hibernate: 20 | ddl-auto: update 21 | show-sql: false 22 | database: mysql 23 | database-platform: org.hibernate.dialect.MySQL5Dialect 24 | open-in-view: false 25 | generate-ddl: false 26 | -------------------------------------------------------------------------------- /src/main/webapp/static/js/main.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mamadaliev/spring-boot-jsp-postgres/d84cbb7c8262c9cc5a0822ee434835bd2a42ffae/src/main/webapp/static/js/main.js -------------------------------------------------------------------------------- /src/main/webapp/view/book.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | 3 | 4 | Book 5 | 6 | 7 | 8 |
9 | Book id: ${book.id}
10 | Book name: ${book.name}
11 | Book author: ${book.author}
12 |
13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/webapp/view/books.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 | 4 | 5 | Books 6 | 7 | 8 |
9 | 10 |
${book.id}
11 | ${book.name} 12 |
${book.author}
13 |
14 |
15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /src/main/webapp/view/home.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8"%> 2 | 3 | 4 | Home page 5 | 6 | 7 |
8 | Welcome to Home page! 9 |
10 | 11 | 12 | --------------------------------------------------------------------------------