spring-boot(thymeleaf前端框架,简单了解)、( 跨域请求)

Thymeleaf模板引擎

Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎 ,类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。CMS

FreeMaker 新闻详细 生成.html页面

detail.flt

data

Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用,

SpringBoot推荐的Thymeleaf;

语法更简单,功能更强大;

引入thymeleaf

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Thymeleaf使用

下面是所下载依赖中的一个文件关于thymleaf默认配置

@ConfigurationProperties(prefix = "spring.thymeleaf")

public class ThymeleafProperties {

private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");

private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

public static final String DEFAULT_PREFIX = "classpath:/templates/";

public static final String DEFAULT_SUFFIX = ".html";

//

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

列创建templates/hello.html

导入thymeleaf的命名空间

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

注:th名字可以任意,后面即用该名字的标签引用thymeleaf

创建一个controller

复制代码
package com.zking.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller//跳视图用这个
public class HelloController {
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","hello thymeleaf!");
        return "hello";
    }

}

再在 hello.html中加入

复制代码
<div th:text="${msg}"></div>//然后运行

跨域请求

跨域请求是指从一个域名的网页去请求另一个域名的资源。浏览器出于安全考虑,默认情况下不允许这种跨域请求。但是,通过一些配置可以允许特定的跨域请求。

测试

复制代码
var token = "amdasdfjaskjf12asdf";
var xhr = new XMLHttpRequest();
xhr.open('GET','http://127.0.0.1:8083/hello');
xhr.setRequestHeader("x-access-token",token);
xhr.send(null);
xhr.οnlοad=function(e){
    var xhr=e.target;
    console.log(xhr.responseText);
}

方式一:@CrossOrigin

注释到你要打开跨域请求的方法上

复制代码
@CrossOrigin
@RequestMapping("/hello")
public String hello(Model model){
    model.addAttribute("msg","hello thymeleaf!");
    return "hello";
}

方式二@Configuration注册corsFilter过滤器

把前面的@CrossOrigin注释掉

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import org.springframework.web.filter.CorsFilter;

@Configuration

public class CorsConfig {

private CorsConfiguration buildConfig() {

CorsConfiguration corsConfiguration = new CorsConfiguration();

corsConfiguration.addAllowedOrigin("*"); //允许任何域名

corsConfiguration.addAllowedHeader("*"); //允许任何头

corsConfiguration.addAllowedMethod("*"); //允许任何方法

return corsConfiguration;

}

@Bean

public CorsFilter corsFilter() {

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

source.registerCorsConfiguration("/**", buildConfig()); //注册

return new CorsFilter(source);

}

}

相关推荐
工一木子8 分钟前
【Java项目脚手架系列】第七篇:Spring Boot + Redis项目脚手架
java·spring boot·redis
源码云商3 小时前
【带文档】网上点餐系统 springboot + vue 全栈项目实战(源码+数据库+万字说明文档)
数据库·vue.js·spring boot
zy happy4 小时前
搭建运行若依微服务版本ruoyi-cloud最新教程
java·spring boot·spring cloud·微服务·ruoyi
wowocpp6 小时前
spring boot Controller 和 RestController 的区别
java·spring boot·后端
独泪了无痕7 小时前
MongoTemplate 基础使用帮助手册
spring boot·mongodb
獨枭9 小时前
使用 163 邮箱实现 Spring Boot 邮箱验证码登录
java·spring boot·后端
维基框架9 小时前
Spring Boot 封装 MinIO 工具
java·spring boot·后端
秋野酱9 小时前
基于javaweb的SpringBoot酒店管理系统设计与实现(源码+文档+部署讲解)
java·spring boot·后端
Q_Q196328847510 小时前
python的家教课程管理系统
开发语言·spring boot·python·django·flask·node.js·php
Minyy1110 小时前
Vue3指令(二)--v-text、v-html数据渲染,计算属性
前端·javascript·vue.js·前端框架·vue·html