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

}

}

相关推荐
sclibingqing37 分钟前
SpringBoot项目接口集中测试方法及实现
java·spring boot·后端
KK溜了溜了3 小时前
JAVA-springboot log日志
java·spring boot·logback
我命由我123454 小时前
Spring Boot 项目集成 Redis 问题:RedisTemplate 多余空格问题
java·开发语言·spring boot·redis·后端·java-ee·intellij-idea
面朝大海,春不暖,花不开4 小时前
Spring Boot消息系统开发指南
java·spring boot·后端
hshpy4 小时前
setting up Activiti BPMN Workflow Engine with Spring Boot
数据库·spring boot·后端
jay神4 小时前
基于Springboot的宠物领养系统
java·spring boot·后端·宠物·软件设计与开发
不知几秋5 小时前
Spring Boot
java·前端·spring boot
我命由我123455 小时前
VSCode - VSCode 放大与缩小代码
前端·ide·windows·vscode·前端框架·编辑器·软件工具
howard20056 小时前
5.4.2 Spring Boot整合Redis
spring boot·整合redis
Nejosi_念旧6 小时前
Vite 双引擎架构 —— Esbuild 概念篇
架构·前端框架