This is the folder Structure for the Java Stack Submissions, This will be updated each week/day:
what I used to create this is tree
try this command in the cmd in the same directory of the folder.
tree /A > tree.txt
.
βββ .gitignore
βββ README.md
βββ here.txt
βββ Java Fundamentals
β βββ Day 48
β β βββ 0-First java program
β β βββ 1-Cafe java
β β βββ 2-Alfred bot
β βββ Day 49
β βββ 0-Cafe Business Logic
β βββ 1-Puzzling
β βββ 2-Map of the Hashmatique
β βββ 3-Lists of Exceptions
βββ Java OOP
β βββ Java OOP
β β βββ 0-Orders & Items
β β βββ 1-Barista's Challenge
β β βββ 2-BankAccount Assignment
β β βββ 3-Coffeedore 64
β βββ Java OOP advanced
β βββ Day 51
β βββ Day 52
β β βββ 0-Zoo Keeper 1
β β βββ 1-CareSoft Interfaces
β βββ Day 53
β βββ 0-Calculator I
β βββ 1-Calculator II
βββ Java Spring
βββ Day 54
β βββ 0-Daikichi Routes
β βββ 1-Hello Human
β βββ 2-Daikichi Path Variables
βββ Day 55
β βββ 0-Hopper's Receipt
β βββ 1-Display Date
β βββ 2-Fruity Loops
β βββ 3-Counter
β βββ Day 55-
β βββ 0-Omikuki Form
β βββ 1-Ninja Gold Game
βββ Day 56
β βββ 0-Omikuki Form
βββ spring MVC
β βββ Day 58
β β βββ Summary.md
β βββ Day 59
β β βββ 0-Books API
β β βββ 1-Rendering Books
β β βββ 2-All Books
βββ spring adding one to many
βββ Day 60
βββ 0-Dojos and ninjas
βββ Full spring
βββ Day 60
β βββ 0-Categories and Products
βββ Day 62
β βββ 0-Login and Registration
β βββ 1-Book Club
βββ Day 63
βββ 0-Book Broker
βββ 1-Project Manager
βββ Day 64
βββ 0-Queries and Joins
This is a small summary of the project documentation for using jsp and java projects in general.
Why spring? Frameworks in general are supposed to make your life 10x times easier as a developer, this is where spring comes into play as explained on there website:
What I currently relate with is having microservices , building web applications, and serverless applications, Although I will be mainly using for building web applications only.
So, After assuming that You have downloaded spring boot suite from their GitHub repo, and it has been installed correctly, weβll move to creating a project.
-
Note :
I have downloaded βdark-darkestβ mode, this is why my dashboard looks like this.
- Choose a new folder directory to place your project in, then
- create a new java project by going to file β new β java project,
- untick the add module thingy.
- add a package to the src , call it βcom.βdomainβ.helloβ or whatever , and add a class ,
- Work on your classes like normal and you should be good.
Steps:
-
Then, You will have a bunch of Generated folders, where one really important file is the βpom.xmlβ which holds all the dependencies
-
- To start using this application we have a few things to discuss:
- src/main/java holds your java packages and files, so when you are creating a new class, always think about how to manage the packages, and make sure to import that class withing the place you are working in.
- the src/main/resourcses holds the static files.
- src/ holds the templates with the β.jspβ file format.
- Actual steps to creating a working app:
- in the main java file, create a new class file, this will be our main controller.
- depending on what type of information you want to return on the web page, you can use
@RestController
to return json or XML data, think of it as if they were Strings for now, or@Controller
to return views, So starting with the restcontroller:
give the main public class the @RestController
annotation, and press ctrl + shift + O
to import the restcontroller package automatically Do not abuse this.
Next, create a path that your website will go to, using the @RequestMapping("/path_here"),for instance:
to run the project, right Click on the project in the folder Explorer, and run it as a spring boot app:
Note: Common error that usually occurs is having the port live somewhere, the fix this is to find where the port is working on your local host and killing it, the best fix i could find is this :
killing port
After that, Yes, the expected output is the string:
The next tricky concept is rendering views! the steps are super tricky and they need to be maintained! Iβll work on top of the file Iβm in:
Letβs work on the folder structure:
- in order to the views to be rendered we need to place a dependency in the pom.xml file for maven to understand what we are talking about,
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
Note : groupId = organization , artfactId: name of the library . docs::: Tom Cat
after placing that, go to the \src\main\webapp and create a new .jsp file
the controller java file should look something like this :
as simple as this :
Note : the ISO-8859-1 is the standard html 4 pages encoding system, changing it to UTF-8 encoding would be a good idea :
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
adding static files:
- Create theΒ
src/main/webapp
Β folder if it does not exist - Create theΒ
src/main/webapp/WEB-INF
Β folder - Edit theΒ
src/main/resources/applications.properties
Β file to contain the following code:
spring.mvc.view.prefix=/WEB-INF/
dependency:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
to use jstl:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- New line below to use the JSP Standard Tag Library -->
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>
to include bootstrap :
in the .jsp file:
<link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css" />
<script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
Dependancy :
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.46</version>
</dependency>
<!-- BOOTSTRAP DEPENDENCIES -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
do not change anything
for static files , link these in the head as well:
<link rel="stylesheet" type="text/css" href="/css/style.css">
<script type="text/javascript" src="/js/app.js"></script>
this is all for now!
You need to install the Spring pack extentions from VScode market Spring for VSCode
From their words in the link:
'To install, launch VS Code and from the Extensions view (Ctrl+Shift+X), search for vscode-spring-initializr.'
'Once you have the extension installed, open the Command Palette (Ctrl+Shift+P) and type Spring Initializr to start generating a Maven or Gradle project and then follow the wizard.'
Follow the normal steps that we took before and you should be good to go, you can start the app from the dashboard.
I also downloaded the spring CLI which feels like fun.
# Where are jsp files? HERE!
spring.mvc.view.prefix=/WEB-INF/
# Data Persistence
spring.datasource.url=jdbc:mysql://localhost:3306/<<YOUR_SCHEMA_NAME>>
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
# For Update and Delete method hidden inputs
spring.mvc.hiddenmethod.filter.enabled=true
<!-- DEPENDENCIES FOR STARTING SPRING PROJECTS-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- DEPENDENCIES FOR DISPLAYING JSPS AND USING JSTL TAGS -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
</dependency>
<!-- DEPENDENCIES FOR INTEGRATING SQL DATABASE AND USING JPA -->
<!-- Note: Project will not run until a schema has been created and the
proper settings in application properties are present for
connecting to a database. -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- DEPENDENCY FOR USING VALIDATION ANNOTATIONS -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- DEPENDENCY FOR USING BCRYPT -->
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>
<!-- DEPENDENCIES FOR BOOTSTRAP -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.46</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- c:out ; c:forEach etc. -->
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!-- Formatting (dates) -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!-- form:form -->
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!-- for rendering errors on PUT routes -->
<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Tacos</title>
<link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/main.css"> <!-- change to match your file/naming structure -->
<script src="/webjars/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/js/app.js"></script><!-- change to match your file/naming structure -->
</head>
<body>
</body>
</html>
Last words : weeeeeee