# gs-serving-web-content **Repository Path**: marvelousyang/gs-serving-web-content ## Basic Information - **Project Name**: gs-serving-web-content - **Description**: Serving Web Content with Spring MVC :: Learn how to create a web page with Spring MVC. - **Primary Language**: Shell - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-09-12 - **Last Updated**: 2024-05-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README :spring_version: current :spring_boot_version: 1.5.10.RELEASE :Component: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/stereotype/Component.html :Controller: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/stereotype/Controller.html :DispatcherServlet: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/servlet/DispatcherServlet.html :SpringApplication: http://docs.spring.io/spring-boot/docs/{spring_boot_version}/api/org/springframework/boot/SpringApplication.html :RequestParam: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html :View: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/servlet/View.html :ResponseBody: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html :Model: http://docs.spring.io/spring/docs/{spring_version}/javadoc-api/org/springframework/ui/Model.html :toc: :icons: font :source-highlighter: prettify :project_id: gs-serving-web-content This guide walks you through the process of creating a "hello world" web site with Spring. == What you'll build You'll build an application that has a static home page, and also will accept HTTP GET requests at: http://localhost:8080/greeting and respond with a web page displaying HTML. The body of the HTML contains a greeting: "Hello, World!" You can customize the greeting with an optional `name` parameter in the query string: http://localhost:8080/greeting?name=User The `name` parameter value overrides the default value of "World" and is reflected in the response: "Hello, User!" == What you'll need :java_version: 1.8 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-gradle.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-maven.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/hide-show-sts.adoc[] [[initial]] == Create a web controller In Spring's approach to building web sites, HTTP requests are handled by a controller. You can easily identify these requests by the {Controller}[`@Controller`] annotation. In the following example, the GreetingController handles GET requests for /greeting by returning the name of a {View}[`View`], in this case, "greeting". A `View` is responsible for rendering the HTML content: `src/main/java/hello/GreetingController.java` [source,java] ---- include::complete/src/main/java/hello/GreetingController.java[] ---- This controller is concise and simple, but there's plenty going on. Let's break it down step by step. The `@GetMapping` annotation ensures that HTTP GET requests to `/greeting` are mapped to the `greeting()` method. {RequestParam}[`@RequestParam`] binds the value of the query String parameter `name` into the `name` parameter of the `greeting()` method. This query String parameter is not `required`; if it is absent in the request, the `defaultValue` of "World" is used. The value of the `name` parameter is added to a {Model}[`Model`] object, ultimately making it accessible to the view template. The implementation of the method body relies on a link:/understanding/view-templates[view technology], in this case http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html[Thymeleaf], to perform server-side rendering of the HTML. Thymeleaf parses the `greeting.html` template below and evaluates the `th:text` expression to render the value of the `${name}` parameter that was set in the controller. `src/main/resources/templates/greeting.html` [source,html] ---- include::complete/src/main/resources/templates/greeting.html[] ---- == Developing web apps A common feature of developing web apps is coding a change, restarting your app, and refreshing the browser to view the change. This entire process can eat up a lot of time. To speed up the cycle of things, Spring Boot comes with a handy module known as http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools[spring-boot-devtools]. * Enable http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-hotswapping[hot swapping] * Switches template engines to disable caching * Enables LiveReload to refresh browser automatically * Other reasonable defaults based on development instead of production == Make the application executable Although it is possible to package this service as a traditional link:/understanding/WAR[WAR] file for deployment to an external application server, the simpler approach demonstrated below creates a standalone application. You package everything in a single, executable JAR file, driven by a good old Java `main()` method. Along the way, you use Spring's support for embedding the link:/understanding/Tomcat[Tomcat] servlet container as the HTTP runtime, instead of deploying to an external instance. `src/main/java/hello/Application.java` [source,java] ---- include::complete/src/main/java/hello/Application.java[] ---- include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_subhead.adoc[] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[] Logging output is displayed. The app should be up and running within a few seconds. == Test the App Now that the web site is running, visit http://localhost:8080/greeting, where you see: "Hello, World!" Provide a `name` query string parameter with http://localhost:8080/greeting?name=User. Notice how the message changes from "Hello, World!" to "Hello, User!": "Hello, User!" This change demonstrates that the {RequestParam}[`@RequestParam`] arrangement in `GreetingController` is working as expected. The `name` parameter has been given a default value of "World", but can always be explicitly overridden through the query string. == Add a Home Page Static resources, like HTML or JavaScript or CSS, can easily be served from your Spring Boot application just be dropping them into the right place in the source code. By default Spring Boot serves static content from resources in the classpath at "/static" (or "/public"). The `index.html` resource is special because it is used as a "welcome page" if it exists, which means it will be served up as the root resource, i.e. at `http://localhost:8080/` in our example. So create this file: `src/main/resources/static/index.html` [source,html] ---- include::complete/src/main/resources/static/index.html[] ---- and when you restart the app you will see the HTML at http://localhost:8080/. == Summary Congratulations! You have just developed a web page using Spring. == See Also The following guides may also be helpful: * https://spring.io/guides/gs/spring-boot/[Building an Application with Spring Boot] * https://spring.io/guides/gs/accessing-data-gemfire/[Accessing Data with GemFire] * https://spring.io/guides/gs/accessing-data-jpa/[Accessing Data with JPA] * https://spring.io/guides/gs/accessing-data-mongodb/[Accessing Data with MongoDB] * https://spring.io/guides/gs/accessing-data-mysql/[Accessing data with MySQL] * https://spring.io/guides/gs/testing-web/[Testing the Web Layer] * https://spring.io/guides/gs/rest-service/[Building a RESTful Web Service] include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]