zoukankan      html  css  js  c++  java
  • 笔记61 Spring Boot快速入门(一)

    IDEA+Spring Boot快速搭建

    一、IDEA创建项目

    项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服务端口。

    application.properties

    1 spring.datasource.url = jdbc:mysql://localhost:3306/sh
    2 spring.datasource.username = root
    3 spring.datasource.password =  123456
    4 spring.datasource.driverClassName = com.mysql.jdbc.Driver
    5 #页面热加载
    6 spring.thymeleaf.cache = false
    7 #端口
    8 server.port=8888

    二、新建控制器

    com.example.demo下创建包Controller用来存放控制器

    IndexController.java  (显示当前时间)

     1 package com.example.demo.Controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.ui.Model;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 
     7 import java.text.DateFormat;
     8 import java.util.Date;
     9 
    10 @Controller
    11 public class IndexController {
    12     @RequestMapping("/index")
    13     public String index(Model model){
    14         model.addAttribute("time",DateFormat.getDateTimeInstance().format(new Date()));
    15         return "hello";
    16     }
    17 }

    三、新建视图html

    1.在resources下新建static包,用来存放一些静态资源:css、js、img等。

    test.css

    1 body{
    2     color: red;
    3 }

    2.在resources下新建templates包,用来存放视图。

      在这里使用thymeleaf来显示后台传过来的数据。thymeleaf 跟 JSP 一样,就是运行之后,就得到纯 HTML了。 区别在与,不运行之前, thymeleaf 也是 纯 html ...所以 thymeleaf 不需要 服务端的支持,就能够被以 html 的方式打开,这样就方便前端人员独立设计与调试, jsp 就不行了, 不启动服务器 jsp 都没法运行出结果来。

    <1>声明当前文件是 thymeleaf, 里面可以用th开头的属性

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

    <2>把 time 的值显示在当前 h2里,用的是th开头的属性: th:text, 而取值用的是 "${time}" 这种写法叫做 ognl,就是跟EL表达式一样吧。 这样取出来放进h2里,从而替换到原来h2标签里的 4个字符 "name" .

    1  <h2 th:text="'Time:'+${time}">time</h2>

    hello.html

     1 <!DOCTYPE html>
     2 <html xmlns:th="http://www.thymeleaf.org">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6     <link rel="stylesheet" href="test.css" type="text/css"/>
     7 </head>
     8 <body>
     9     <h1>Hello Spring Boot</h1>
    10     <h2 th:text="'Time:'+${time}">time</h2>
    11 </body>
    12 </html>

    四、运行结果

  • 相关阅读:
    linux系统——机制与策略(三)
    linux系统——机制与策略(二)
    Linux系统——机制策略(一)
    RTSP会话基本流程
    linux编程学习
    编码风格——linux内核开发的coding style
    编程风格——整洁代码的4个提示
    编程风格——五种应该避免的代码注释
    十条不错的编程观点
    代码优化概要
  • 原文地址:https://www.cnblogs.com/lyj-gyq/p/9262612.html
Copyright © 2011-2022 走看看