美文网首页spring
使用 Spring MVC 提供 Web 内容

使用 Spring MVC 提供 Web 内容

作者: ADADACHAN | 来源:发表于2021-09-09 11:07 被阅读0次

    使用 Spring MVC 提供 Web 内容

    你将建造什么

    您将构建一个具有静态主页的应用程序,该应用程序还将接受位于 http://localhost:8080/greeting 的 HTTP GET 请求。

    它将响应一个显示 HTML 的网页。 HTML 的正文将包含一句问候语:“Hello, World!”

    您可以使用查询字符串中的可选名称参数自定义问候语。该 URL 可能是 http://localhost:8080/greeting?name=User。

    name 参数值覆盖 World 的默认值,并通过内容更改为“Hello, User!”反映在响应中。

    build.gradle

    plugins{

      id'org.springframework.boot' version'2.5.2'

      id'io.spring.dependency-management' version'1.0.11.RELEASE'

      id'java'

    }

    group ='com.example'

    version ='0.0.1-SNAPSHOT'

    sourceCompatibility ='1.8'

    repositories{

      mavenCentral()

    }

    dependencies{

      implementation'org.springframework.boot:spring-boot-starter-thymeleaf'

      implementation'org.springframework.boot:spring-boot-starter-web'

      developmentOnly'org.springframework.boot:spring-boot-devtools'

      testImplementation'org.springframework.boot:spring-boot-starter-test'

    }

    test{

      useJUnitPlatform()

    }

    Spring Appaction的入口主函数

    package com.example.servingwebcontent;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication

    public class ServingWebContentApplication {

    public static void main(String[] args) {

    SpringApplication.run(ServingWebContentApplication.class, args);

        }

    }

    Spring Initializr 为您创建了一个应用程序类。在这种情况下,您不需要进一步修改 Spring Initializr 提供的类。以下清单(来自 src/main/java/com/example/servingwebcontent/ServingWebContentApplication.java)显示了应用程序类:没有什么变法或者通过, https://start.spring.io这个网站初始化一个代码就可以。

    创建 Web 控制器

    package com.example.servingwebcontent;

    import org.springframework.stereotype.Controller;

    import org.springframework.ui.Model;

    import org.springframework.web.bind.annotation.*;

    @Controller

    public class GreetingController {

    @GetMapping("/greeting")

    public Stringgreeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {

    model.addAttribute("name", name);

          return "greeting";

      }

    @RequestMapping(value={"/greeting/{someID}"}, method= RequestMethod.GET)

    public Stringadd(@PathVariable(value="someID")final String id,

                    @RequestParam  String name,

                        Model model) {

    System.out.println("id="+id);

          model.addAttribute("name", name);

          model.addAttribute("id", id);

          return "greeting";

      }

    }

    简单加入一个/add/{id}?name=XXXX

    获取id和name的值。

    @GetMapping 注释确保对 /greeting 的 HTTP GET 请求映射到 greeting() 方法。

    @RequestParam 将查询字符串参数 name 的值绑定到 greeting() 方法的 name 参数中。此查询字符串参数不是必需的。如果请求中不存在,则使用 World 的 defaultValue。 name 参数的值被添加到 Model 对象中,最终使其可被视图模板访问。

    通过@PathVariable 来获取RequestMap的地址 获取{someID},绑定到id参数中,设置必须传入的参数。

    结果显示为

    页面需要修改一下:

    <!DOCTYPE HTML>

    <html xmlns:th="http://www.thymeleaf.org">

        <title>Getting Started: Serving Web Content

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <p th:text="'Hello, ' + ${name} + '!'" />

        <p th:text="'Hello, ' + ${id} + '!'+ ${name}" />

    显示${id}需要添加进去

    编译

    如果您使用 Gradle,则可以使用 ./gradlew bootRun 运行该应用程序。

    或者,您可以使用 ./gradlew build 构建 JAR 文件,然后运行 JAR 文件,如下所示:

    或者通过idea的界面进行点击,比较方便

    编译后的目录到这里寻找

    相关文章

      网友评论

        本文标题:使用 Spring MVC 提供 Web 内容

        本文链接:https://www.haomeiwen.com/subject/igriwltx.html