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);

}

}

相关推荐
摸鱼仙人~17 分钟前
深入理解 classnames:React 动态类名管理的最佳实践
前端·react.js·前端框架
RainbowSea1 小时前
跨域问题(Allow CORS)解决(3 种方法)
java·spring boot·后端
RainbowSea1 小时前
问题 1:MyBatis-plus-3.5.9 的分页功能修复
java·spring boot·mybatis
sniper_fandc3 小时前
SpringBoot系列—入门
java·spring boot·后端
Albert Edison8 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
三水气象台11 小时前
用户中心Vue3网页开发(1.0版)
javascript·css·vue.js·typescript·前端框架·html·anti-design-vue
六毛的毛12 小时前
Springboot开发常见注解一览
java·spring boot·后端
开开心心就好13 小时前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑
猴哥源码13 小时前
基于Java+SpringBoot的农事管理系统
java·spring boot
光军oi15 小时前
java微服务(Springboot篇)——————IDEA搭建第一个Springboot入门项目
java·spring boot·微服务