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

}

}

相关推荐
Mr.Java.8 分钟前
Spring Boot MongoDB自定义连接池配置
java·spring boot·后端·mongodb
菠萝崽.37 分钟前
springboot中测试python脚本:ProcessBuilder
java·开发语言·spring boot·python·processbuilder
风象南1 小时前
SpringBoot中3种条件装配技术
java·spring boot·后端
呆萌很1 小时前
基于 Spring Boot 瑞吉外卖系统开发(三)
spring boot
智绘前端2 小时前
Vue3的Composition API与React Hooks有什么异同?
前端·vue.js·react.js·前端框架·vue
.生产的驴8 小时前
SpringBoot 接口限流Lua脚本接合Redis 服务熔断 自定义注解 接口保护
java·大数据·数据库·spring boot·redis·后端·lua
一切皆有迹可循8 小时前
SpringBoot整合MinIO快速入门:实现分布式文件存储与管理
spring boot·分布式·后端
gqkmiss9 小时前
Git Cherry-pick:核心命令、实践详解
前端·git·前端框架·commit·cherry-pick
洛可可白9 小时前
Spring Boot中自定义注解的创建与使用
java·spring boot·后端
Code额10 小时前
Vue 框架组件间通信方式
前端·vue.js·前端框架