{"id":20957020,"url":"https://github.com/spilth/annotated-spring-episode-002","last_synced_at":"2025-08-21T01:10:59.189Z","repository":{"id":136684008,"uuid":"41522355","full_name":"spilth/annotated-spring-episode-002","owner":"spilth","description":"Spring Boot Web Application QuickStart ","archived":false,"fork":false,"pushed_at":"2016-12-17T15:57:05.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-13T06:12:54.820Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.annotatedspring.com/episodes/2/spring-boot-web-application-quickstart","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spilth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-28T02:27:40.000Z","updated_at":"2024-04-02T17:42:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"d8424c30-38c0-4d50-86f8-bad630847bff","html_url":"https://github.com/spilth/annotated-spring-episode-002","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/spilth/annotated-spring-episode-002","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spilth%2Fannotated-spring-episode-002","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spilth%2Fannotated-spring-episode-002/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spilth%2Fannotated-spring-episode-002/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spilth%2Fannotated-spring-episode-002/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spilth","download_url":"https://codeload.github.com/spilth/annotated-spring-episode-002/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spilth%2Fannotated-spring-episode-002/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271411294,"owners_count":24754901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T01:29:16.748Z","updated_at":"2025-08-21T01:10:59.158Z","avatar_url":"https://github.com/spilth.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Spring Boot Web Application QuickStart\n\nNow that I have the Spring Boot CLI installed, I'd like to create a simple web application.  To keep it super simple, I'm going to create a page that just displays the current time on the web server. Each time I reload the page I should see the date and time update.\n\n## Initializing the Project\n\nSince the Spring Boot CLI creates a command-line application by default, I'll need a way to specify that I want a web application instead.\n\nLet's take a look at what options we have for the `init` command.\n\n    $ spring help init\n\nSo the `init` command can take a list of Spring Boot dependencies that you'd like to use in your project.  You can see the available list by passing `—list` to the init command.\n\n    $ spring init —list\n\nSince we want to build a web app we'll pull in the `web` starter. We also want to be able to serve dynamic pages with some kind of templating system. Freemarker seems like a good candidate for that.\n\n    $ spring init hello-web -d=web,freemarker\n    $ cd hello-web\n\nLet's see what we get out of the box. We’ll use the `spring-boot:run` Maven goal to start up our app.\n\n    $ mvn spring-boot:run\n\nThen navigate your browser to `http://localhost:8080/`\n\nAnd we’re getting an error page. Why is that?\n\n## Simple Controller \u0026 Template\n\nIn order to respond to a request we need to set up a Controller. A controller lets you control what happens when a particular path is requested in your application.\n\nLets create a Controller named `HomepageController` with a method called `index` that renders a simple template.\n\nFirst we make the class. Then we add the @Controller annotation to the class which lets Spring know that it should load this class and treat it as a controller.\n\nTo respond to requests for the root path we’ll add a method that is mapped to forward slash. We do this by annotating a method with @RequestMapping and a value of just forward slash.\n\nAt a minimum this method needs to return the path of a template to render, so we'll set it’s return type as String and return the string of “index”.\n\n    package demo;\n    \n    import org.springframework.stereotype.Controller;\n    import org.springframework.ui.Model;\n    import org.springframework.web.bind.annotation.RequestMapping;\n    \n    import java.util.Date;\n    \n    @Controller\n    public class DemoController {\n    \n        @RequestMapping(\"/\")\n        public String index() {\n            return \"index\";\n        }\n    }\n\nNext we create a Freemarker template under `src/main/resources/templates` named `index.ftl`.  For now it can just contain some simple markup.\n\n    \u003chtml\u003e\n        \u003chead\u003e\n            \u003ctitle\u003eHello, web!\u003c/title\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003ch1\u003eHello, web!\u003c/h1\u003e\n        \u003c/body\u003e\n    \u003c/html\u003e\n\nLet’s restart our server and see if things are working now.\n\n    $ mvn clean spring-boot:run\n\nGreat, we’re serving up what basically amounts to a static page.\n\n## Dynamic Content\n\nLet’s add the date and time to the page now. Back in the Controller we’ll add a Model to the signature of our method. Spring will now pass in a model that we can add attributes to and those attributes are then available in our template.  Let’s add the current date.\n\n    package demo;\n    \n    import org.springframework.stereotype.Controller;\n    import org.springframework.ui.Model;\n    import org.springframework.web.bind.annotation.RequestMapping;\n    \n    import java.util.Date;\n    \n    @Controller\n    public class DemoController {\n    \n        @RequestMapping(\"/\")\n        public String index(Model model) {\n            model.addAttribute(\"now\", new Date());\n    \n            return \"index\";\n        }\n    }\n\nThen we update the template to render the attribute we added.\n\n    \u003chtml\u003e\n        \u003chead\u003e\n            \u003ctitle\u003eHello, web!\u003c/title\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003ch1\u003eHello, web!\u003c/h1\u003e\n    \n            \u003cp\u003eRight now it is ${now}\u003c/p\u003e\n        \u003c/body\u003e\n    \u003c/html\u003e\n\nNow kill your server with Control-C and then restart it.\n\nNow it’s complaining about not knowing how to render the date.  Let’s fix that by using some Freemarker functions to make it render correctly and nicely.\n\n    \u003chtml\u003e\n        \u003chead\u003e\n            \u003ctitle\u003eHello, web!\u003c/title\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003ch1\u003eHello, web!\u003c/h1\u003e\n    \n            \u003cp\u003eRight now it is ${now?datetime?string.full}\u003c/p\u003e\n        \u003c/body\u003e\n    \u003c/html\u003e\n\nNow we know the basics of creating a Controller, assign a method to a path and providing data to our templates.\n\nNext: [Web Application Layouts with Freemarker, WebJars \u0026 Bootstrap](https://github.com/spilth/annotated-spring-episode-003)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspilth%2Fannotated-spring-episode-002","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspilth%2Fannotated-spring-episode-002","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspilth%2Fannotated-spring-episode-002/lists"}