###### tags: `Java` `CheatSheet` `Starter` # Java Starter notes ## MAC if Spring boot will not open after it has in past - Run the following in the terminal (location of terminal does not mater) - `codesign --force --deep --sign - /Applications/SpringToolSuite4.app` 1. Chose the location to open spring boot in.. if via the launch screen you may not have other projects to see inside spring boot 2. Right or click file > new > new Spring Starter Project ![](https://i.imgur.com/qipIwYt.png) ![](https://i.imgur.com/KcyQ4dD.png) Install Emmet Help > Install New Software > Add > location = http://download.emmet.io/eclipse/updates/ ## Mac - Import missing dependencies - CMD + Shift + o - Run Spring Boot Application - Click on Project Main Folder - Option + Shift + x (menu pops up in bottom right of window) - B (selects spring boot from menu) ## Windows - Import missing dependencies - CTRL + Shift + o - Run Spring Boot Application - Click on Project Main Folder - Alt + Shift + x (menu pops up in bottom right of window) - B (selects spring boot from menu) Generate Getters and Setters Right click class flile > Chose Source >. Genertate Getters and Setters click all applicable fields and then fininto # Quick Block Comments Attributes Label ``` // ========================== // ATTRIBUTES // ========================== ``` Constructor Label ``` // ========================== // CONSTRUCTOR // ========================== ``` Getter and Setter Label ``` // ========================== // GETTERS / SETTERS // ========================== ``` Method Label ``` // ========================== // METHODS // ========================== ``` Interface Label ``` // ========================== // INTERFACE // ========================== ``` Relationship Label ``` // ========================== // RELATIONSHIPS // ========================== ``` # Formating print statements https://alvinalexander.com/programming/printf-format-cheat-sheet/ ![](https://i.imgur.com/abwaLu1.png) ![](https://i.imgur.com/MUk9BID.png) ## pom file dependencies 1. Add the following code to pom.xml (NOTE: be careful not toput a dependency into another dependency) 2. parent should be 2.7.10, .9, or .8 ``` <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.8</version> <relativePath/> <!-- lookup parent from repository --> </parent> Below will over write/ replace everything between the <dependencies></dependencies> tags <!-- 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>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <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.30</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.6.0</version> </dependency> ``` 2. New folder created in [src > main > webapp] named WEB-INF ![](https://i.imgur.com/TvzlqsD.png) 3. src/main/resources select application.properties add ``` spring.mvc.view.prefix=/WEB-INF/ spring.datasource.url=jdbc:mysql://localhost:3306/<<YOUR_SCHEMA>> spring.datasource.username=<<dbuser>> spring.datasource.password=<<dbpassword>> spring.jpa.hibernate.ddl-auto=update spring.mvc.hiddenmethod.filter.enabled=true ``` 4. In all JSP files start them with the following code ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!-- for forms --> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %> <!-- for validation --> <%@ page isErrorPage="true" %> <!DOCTYPE html> <html> <head> <!-- for Bootstrap CSS --> <link rel="stylesheet" href="/webjars/bootstrap/css/bootstrap.min.css" /> <!-- YOUR own local CSS --> <link rel="stylesheet" type="text/css" href="/css/style.css"> <!-- For any Bootstrap that uses JS --> <script src="/webjars/bootstrap/js/bootstrap.min.js"></script> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <header> <h1>Hello</h1> </header> <main> </main> <footer> </footer> </body> </html> ``` # Adding Controllers 1. right click the main src/main/java folder and add new package 2. add controllers to the end of what is prepopulated ![](https://i.imgur.com/mGs4IrB.png) All controller files will now go into this folder ## Renaming files/packages(folders) right click item to rename refactor > rename