-
Notifications
You must be signed in to change notification settings - Fork 1
Spring
... with cool dependencies.
(Spring works like Gradle to set up a project)
- Choose Gradle
- Choose Java
- Choose Default spring boot
- Project metadata - name it with your domain name
- The group is a reversed domain name; It is unique to every Java package ever created.
- It should be a domain name you own. Make one up that you think you could own. Like,
com.sharinastubbs. - The artifact is the specific project (name and package), so call it spring demo or whatever the project is called.
- Skip the options
- Dependencies - JPA (databases), Actuator (creates monitoring routes for the site), Web (for creating servers), Devtools (lets you not have to start and restart your server, etc). Choose web, and devtools at least. Plus, choose thymeleaf! It's a template engine (like EJS).
- Hit the big green button
- Open the zip file and put contents into desired location
- Use intelliJ to open the file.
When the file is run, go to the port it specifies to see the whitelabel error.
- New file - Java Class "HomeController.java"
- Use annotations to set this controller up
- Add annotation called @Controller <---- tells spring to look here for routes ---> and write a method
@Controller
public class HomeController {
@GetMapping("/") <------- PostMapping, etc are other options (can have different Mapping point to "/")
public String getHomeRoute() {
return "home";
}
}
- then, restart the server. Note that if you hit the play button, it will give you an error. First, hit the build button on the top (a hammer). Then refresh website.
- You'll see a 500 error now because it's looking for a template called home.
- Create a template file... put it in resources folder, within templates folder. The name of the template HAS TO MATCH the "home" that is returned up in HomeController ----> home.html
- Type some stuff into the HTML file
- Update the build and refresh the website and you'll see a website without errors!
Thyme leaf attributes start with th:text
<h3 th:text="${}"></h3>
Link to CSS in the home.html
<title>
<link href="/styles.css" rel="stylesheet" />
And put a file called styles.css in static folder.
Open up VS Code for help with CSS (Spring wants you to pay them).
To create an error page, simply create a new page in templates and type in the error you want. Title it error.html, I think, and write what you want. Can write error-404.html to get it even more easy.
- Disconnect leaves your server running in the background forever. Choose terminate!!!! It will stop the server.
- If disconnect chosen, go to terminal and type in lsof -i:8080 to find the individual process to kill.
- used to map web requests onto particular handler classes and/or handler methods
- Produces and Consumes attributes are available - they are new, so beware of old documentation and blog posts. * * Used to map web requests to Spring Controller Methods, ie, mapping an HTTP request to a method.
- @PathVariable - bind parts of the mapping URI to variables. Can use with Regex.
- Can extract the value of the id parameter using @RequestParam("id")annotation
- Can define the parameters to narrow the request mapping
- Usually use just one @RequestMapping for a single controller method
- Creates automatic repository implementations at runtime
Find the port that is listening (look for LISTEN)
$ lsof -i:8080
Kill the port PID, for example:
$ kill 20220
https://spring.io/guides/gs/serving-web-content/
https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html https://www.thymeleaf.org/index.html
- Class demo: https://github.com/codefellows/seattle-java-401d6/tree/master/class-12
- My class notes: https://github.com/SharinaS/java-fundamentals/wiki/Databases-and-Spring
https://www.baeldung.com/spring-requestmapping
https://devcenter.heroku.com/articles/deploying-spring-boot-apps-to-heroku