thymeleaf和vue快速入门

简介: thymeleaf和vue快速入门

时间是个常数,但也是个变数。勤奋的人无穷多,懒惰的人无穷少。——字严

今天中午午休抽时间写了个thymeleafvue使用elementUI简单入门Demo

仓库地址:https://gitee.com/VampireAchao/simple-thymeleaf-html.git

前端代码如下:

<!doctype html>
<html>
<head>
    <title>给胖哥的html页面</title>
    <!-- 引入样式,从这来的 https://element.eleme.cn/#/zh-CN/component/installation#cdn -->
    <link href="https://unpkg.com/element-ui/lib/theme-chalk/index.css" rel="stylesheet">
</head>
<!-- 下面的代码来源 -->
<!-- https://element.eleme.cn/#/zh-CN/component/installation#hello-world -->
<body>
<!-- 下面我们Vue实例绑定了 #app,因此在该节点下的内容会被vue加强 -->
<div id="app">
    <!-- el-button 是 elementUI 按钮组件:https://element.eleme.cn/#/zh-CN/component/button -->
    <!-- @click 是 vue 语法:https://cn.vuejs.org/v2/guide/syntax.html#v-on-%E7%BC%A9%E5%86%99 -->
    <!-- "双大括号"写法(“Mustache”语法)文档:https://cn.vuejs.org/v2/guide/syntax.html#%E6%96%87%E6%9C%AC 简单来说就是引用下面 vue 实例data中的属性值 -->
    <el-button @click="openDialog">{{message}}</el-button>
    <!-- el-dialog 是 elementUI 对话框组件:https://element.eleme.cn/#/zh-CN/component/dialog -->
    <el-dialog :visible.sync="visible" title="弹框标题">
        <!-- thymeleaf 基本语法:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects -->
        <!-- 注意thymeleaf渲染优先级比html高,此处从request中获取对应的attribute -->
        <p>[[${paramFromServer}]]</p>
    </el-dialog>
    <!-- 之后要啥组件,去文档里找,然后cv下来就行了 -->
    <!-- https://element.eleme.cn/#/zh-CN/component/installation -->
</div>
</body>
<!-- 从这里复制过来的:https://cn.vuejs.org/v2/guide/#%E8%B5%B7%E6%AD%A5  -->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!-- 注意顺序,elementUI依赖vue,因此需要先引入vue,此处src来源:https://element.eleme.cn/#/zh-CN/component/installation#cdn 注意有版本号区别 -->
<script src="https://unpkg.com/element-ui@2.15.8/lib/index.js"></script>
<script>
    /* 注意此处new出来的vue实例,能实现双向绑定:https://cn.vuejs.org/v2/guide/instance.html */
    new Vue({
        /* 这里是id选择器,所以要加#号 */
        el: '#app',
        /* data里放双向绑定的数据:https://cn.vuejs.org/v2/guide/instance.html#%E6%95%B0%E6%8D%AE%E4%B8%8E%E6%96%B9%E6%B3%95 */
        data() {
            return {
                visible: false,
                /* 在js中获取,也可以用中括号,但需要用''引起来避免编译报错 */
                message: '[[${message}]]'
            }
        },
        /* methods中定义各种方法 */
        methods: {
            openDialog() {
                // 注意此处的this,此处的this是vue实例,而不是el-button
                // 此处修改visible的值,会触发vue实例的视图更新
                this.visible = true;
            }
        }
    })
</script>
</html>


启动类:

package com.ruben.simplethymeleafhtml;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
@SpringBootApplication
public class SimpleThymeleafHtmlApplication {
    @Resource
    private HttpServletRequest request;
    public static void main(String[] args) {
        SpringApplication.run(SimpleThymeleafHtmlApplication.class, args);
    }
    @GetMapping("/")
    public String index() {
        request.setAttribute("paramFromServer", "我是controller中返回的值");
        request.setAttribute("message", "我是后端返回的值");
        return "index";
    }
}

GAV

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ruben</groupId>
    <artifactId>simple-thymeleaf-html</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>simple-thymeleaf-html</name>
    <description>simple-thymeleaf-html</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.ruben.simplethymeleafhtml.SimpleThymeleafHtmlApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
相关文章
|
2月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
232 2
|
11天前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
221 137
|
5月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
663 0
|
5月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
4月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
165 0
|
4月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
323 1
|
5月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
418 17
|
5月前
|
JavaScript 前端开发 UED
Vue 表情包输入组件实现代码及详细开发流程解析
这是一篇关于 Vue 表情包输入组件的使用方法与封装指南的文章。通过安装依赖、全局注册和局部使用,可以快速集成表情包功能到 Vue 项目中。文章还详细介绍了组件的封装实现、高级配置(如自定义表情列表、主题定制、动画效果和懒加载)以及完整集成示例。开发者可根据需求扩展功能,例如 GIF 搜索或自定义表情上传,提升用户体验。资源链接提供进一步学习材料。
242 1
|
5月前
|
存储 JavaScript 前端开发
如何高效实现 vue 文件批量下载及相关操作技巧
在Vue项目中,实现文件批量下载是常见需求。例如文档管理系统或图片库应用中,用户可能需要一次性下载多个文件。本文介绍了三种技术方案:1) 使用`file-saver`和`jszip`插件在前端打包文件为ZIP并下载;2) 借助后端接口完成文件压缩与传输;3) 使用`StreamSaver`解决大文件下载问题。同时,通过在线教育平台的实例详细说明了前后端的具体实现步骤,帮助开发者根据项目需求选择合适方案。
425 0
|
5月前
|
JavaScript 前端开发 UED
Vue 项目中如何自定义实用的进度条组件
本文介绍了如何使用Vue.js创建一个灵活多样的自定义进度条组件。该组件可接受进度段数据数组作为输入,动态渲染进度段,支持动画效果和内容展示。当进度超出总长时,超出部分将以红色填充。文章详细描述了组件的设计目标、实现步骤(包括props定义、宽度计算、模板渲染、动画处理及超出部分的显示),并提供了使用示例。通过此组件,开发者可根据项目需求灵活展示进度情况,优化用户体验。资源地址:[https://pan.quark.cn/s/35324205c62b](https://pan.quark.cn/s/35324205c62b)。
181 0
下一篇
开通oss服务