Spring boot entry

Let me introduce Spring Boot MVC today. We'll learn how to use Spring Boot to quickly build a simple web application. **Environment Setup** 1. A text editor (like Vim, Emacs, or Sublime Text) or an IDE (such as Eclipse or IntelliJ IDEA). 2. Java environment (JDK 1.7 or higher). 3. Maven 3.0+ (most IDEs include it; if you're using the command line, you’ll need to install it separately). **Creating the Simplest Web Application** Using the Spring Boot framework significantly speeds up web application development. Start by adding the `spring-boot-starter-web` dependency in your Maven project: ```xml 4.0.0 com.tianmaying spring-web-demo 0.0.1-SNAPSHOT jar spring-web-demo Demo project for Spring WebMvc org.springframework.boot spring-boot-starter-parent 1.2.5.RELEASE UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin ``` Next, create a file called `Application.java` under `src/main/java`: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class Application { @RequestMapping("/") public String greeting() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` Run the application using `mvn spring-boot:run` or by running the `main()` method in your IDE. Then, open a browser and go to `http://localhost:8080`. You should see “Hello World!” displayed. With just a few lines of code, we’ve created a fully functional web application. So, what exactly is happening here? The `SpringApplication.run()` method creates a Spring application context and automatically detects dependencies on the classpath. Since we included `spring-boot-starter-web`, Spring Boot knows this is a web application and starts an embedded Tomcat server. The `@RestController` annotation tells Spring that this class is a controller, and its methods return values directly as HTTP response bodies. The `@RequestMapping("/")` maps the root URL (`/`) to the `greeting()` method. If you visit `/hello`, you’ll get a 404 error because no method is mapped to that path. **Implementing URL Routing with @Controller** Modern web applications often have multiple pages, each with its own URL. To handle different URLs, we can define multiple methods: ```java @RestController public class Application { @RequestMapping("/") public String index() { return "Index Page"; } @RequestMapping("/hello") public String hello() { return "Hello World!"; } } ``` You can also apply `@RequestMapping` at the class level to group related endpoints: ```java @RestController @RequestMapping("/classPath") public class Application { @RequestMapping("/methodPath") public String method() { return "mapping url is /classPath/methodPath"; } } ``` **Handling Dynamic URLs with @PathVariable** In real-world applications, URLs are often dynamic. For example, user profiles might look like `http://example.com/users/john`. Instead of writing a separate method for each user, we can use `@PathVariable`: ```java @RequestMapping("/users/{username}") public String userProfile(@PathVariable("username") String username) { return String.format("User %s", username); } @RequestMapping("/posts/{id}") public String post(@PathVariable("id") int id) { return String.format("Post %d", id); } ``` **Supporting HTTP Methods** Different HTTP methods (GET, POST, etc.) are used for different actions. For example, accessing a page is usually a GET request, while submitting a form is a POST request. You can specify which method a controller handles: ```java @RequestMapping(value = "/login", method = RequestMethod.GET) public String loginGet() { return "Login Page"; } @RequestMapping(value = "/login", method = RequestMethod.POST) public String loginPost() { return "Login Post Request"; } ``` **Rendering Templates** So far, we've returned strings directly to the browser. But for richer pages, we need to render HTML templates. To do this, change `@RestController` to `@Controller` and use a template engine like Thymeleaf: ```java import org.springframework.ui.Model; @Controller public class HelloController { @RequestMapping("/hello/{name}") public String hello(@PathVariable("name") String name, Model model) { model.addAttribute("name", name); return "hello"; } } ``` Add the Thymeleaf dependency to your `pom.xml`: ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` Create a template file `src/main/resources/templates/hello.html`: ```html Hello

``` **Serving Static Files** Web applications also need CSS, JavaScript, and images. Place them in the `src/main/resources/static` directory. For example, add `css/style.css` and `js/main.js`. Spring Boot automatically serves these files when accessed via `/css/style.css` or `/js/main.js`. This concludes our introduction to Spring Boot MVC. With this foundation, you can now build more complex web applications and explore advanced features like security, data access, and RESTful APIs.

Laptop Stand Adjustable

China Laptop Stand Adjustable,Adjustable Macbook Stand manufacturer, choose the high quality Ergonomic Adjustable Laptop Stand,Laptop Stand Aluminum Adjustable, etc.

Shenzhen Chengrong Technology Co.ltd is a high-quality enterprise specializing in metal stamping and CNC production for 12 years. The company mainly aims at the R&D, production and sales of Notebook Laptop Stands and Mobile Phone Stands. From the mold design and processing to machining and product surface oxidation, spraying treatment etc ,integration can fully meet the various processing needs of customers. Have a complete and scientific quality management system, strength and product quality are recognized and trusted by the industry, to meet changing economic and social needs .


Slim Laptop Stand

Portable Laptop Stand Adjustable,Best Laptop Stand Adjustable,Aluminum Laptop Stand Adjustable

Shenzhen ChengRong Technology Co.,Ltd. , https://www.laptopstandsupplier.com