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

}

}

相关推荐
醉方休14 分钟前
React中使用DDD(领域驱动设计)
前端·react.js·前端框架
学习3人组23 分钟前
React 样式隔离核心方法和最佳实践
前端·react.js·前端框架
LiRuiJie30 分钟前
深入剖析Spring Boot / Spring 应用中可自定义的扩展点
java·spring boot·spring
尚学教辅学习资料3 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
晚安里3 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
上官浩仁4 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring
叫我阿柒啊4 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
中国胖子风清扬5 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
Rhys..8 小时前
python + Flask模块学习 1 基础用法
python·学习·前端框架·flask
JosieBook9 小时前
【SpringBoot】21-Spring Boot中Web页面抽取公共页面的完整实践
前端·spring boot·python