View on GitHub

reads

https://yazansneneh.github.io/reads/

Read: 11 - Spring

creating and downloading project.
  1. In website mentioned I should navigate to it then.
  2. choose java as language.
  3. choose gradle build.
  4. add dependencies Spring Web, Thymeleaf, and Spring Boot DevTools.
  5. and generate.

Create a Web Controller

<!DOCTYPE HTML>

<head> 
    <title>card</title> 
</head>
<body>
    <p th:text="'Age: ' + ${age}" />
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

Spring Boot Devtools

SO what it does :

Run the Application


### Build an executable JAR
* to run app from terminal I can use gradle or maven gradle.
* use **gradle** to run the app:
  * `./gradlew bootRun`
  

### Test the Application
* when i run `./gradlew bootRun` can open it in browser.
* to do so I can find it on this url http://localhost:8080/greeting
* If i want to change name i can change parameter value to do so I can hit this url :
  * http://localhost:8080/greeting?name=Yazan

##### Add a Home Page
* I can add Static resources, including HTML and JavaScript and CSS in resources file that in src folder.
* By default, Spring Boot serves static resources from resources in the classpath at `/static` or `/public`

## Spring MVC and Thymeleaf: how to access data from templates
* the spring app contain model and controller.
  * controller prepare the model with data then select a viewer to render it.
  * Model map make the process abstract.
  [resource](https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html)

1. **Spring model attributes**
* during the execution the Spring get the data that found and inject it to the **thymeleaf or any rendering engine**.
  * there is variable should be in the view engine with **key** specified and connected with model in order for proper injection.
* That's wht it's called **model mapping**.
* **Ways of injecting data in thymeleaf:**
  * in any way i am injecting to thymeleaf I will add the variable in model and it should be available in thymeleaf or any template engine.
  1. **ddAttribute()** method.

@RequestMapping(value = “message”, method = RequestMethod.GET) public String messages(Model model) { model.addAttribute(“messages”, messageRepository.findAll()); return “message/list”; }

[resource](https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html)

2. `ModelAndView class`

@RequestMapping(value = “message”, method = RequestMethod.GET) public ModelAndView messages() { ModelAndView mav = new ModelAndView(“message/list”); mav.addObject(“messages”, messageRepository.findAll()); return mav; }

 
[resource](https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html).

* now for data to be accessed in thymeleaf I must specify attribute  it should be between `${attributeVariable}` exactly like **Template literals** in javaScript.
example : 
1 Title ... Text ...
[resource](https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html).

2. **Request parameters**
   * there parameters are coming from client (URL Query parameters).
   * in order to access these parameters i must type `<p th:text="${param.query}"></p>`.
   * if i have multi values in parameter I will be working with them like i am working with list.
     * `<p th:text="${param.query[0]}">`
3. **Session attributes**
   * the session attribute can be accessed like params.
   * in controller: `session.setAttribute("mySessionAttribute", "someValue");`
   * in thymeleaf to access it : `<p th:text="${session.mySessionAttribute}" th:unless="${session == null}">[...]</p>`
4. **ServletContext attributes**
 * since i dont know what exactly is this yet: [resource](https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html)
* The ServletContext attributes are shared between requests and sessions. In order to access ServletContext attributes in Thymeleaf you can use the #servletContext. prefix: 
* Example:
My context attribute 42
javax.servlet.context.tempdir /tmp

```