zoukankan      html  css  js  c++  java
  • Java框架spring Boot学习笔记(十):传递数据到html页面的例子

    新建一个templates文件夹和index.html

     1 <!DOCTYPE html>
     2 <html>
     3 <head lang="en">
     4     <meta charset="UTF-8" />
     5     <title></title>
     6 </head>
     7 <body>
     8 <h1 th:text="${host}">Hello World</h1>
     9 </body>
    10 </html>

    这个文件直接用浏览器打开时显示Hello World

     编写HelloController.java

    package com.springboot.test;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController {
    
        @RequestMapping("/")
        public String index(ModelMap map) {
            // 加入一个属性,用来在模板中读取
            map.addAttribute("host", "Spring test");
            // return模板文件的名称,对应src/main/resources/templates/index.html
            return "index";
        }
    }

    运行,浏览器访问127.0.0.1:8080。

     这样就做到了不破坏HTML自身内容的数据逻辑分离。

  • 相关阅读:
    hiho150周
    hdu1011
    hiho1055/hdu1561
    bat脚本启动exe并打开文件后退出 + 中文乱码
    hiho1080
    hiho1079
    java异常处理——基础篇
    找不到要编译的文件——path环境变量配置
    MVC——studying
    轻松搞定EasyUI
  • 原文地址:https://www.cnblogs.com/zylq-blog/p/7911400.html
Copyright © 2011-2022 走看看