Java配置过滤器配置路径出错_java springboot 入门二 (读取配置文件/过滤器解决跨域问题)...

1.读取配置文件

2.处理跨域问题(过滤器/@CrossOrigin注解)

一 读取配置文件

1.1@Value注解 默认读取application.properties,中文会乱码

1.2 @ConfigurationProperties @PropertySource

application.properties

server.port=8084student.sno=007student.sname=邦德

student.age=40

my.properties

student2.sno=007-2student2.sname=邦德-2student2.age=40-2

packagecom.ligy.controller;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;

@Controllerpublic classStudent {

@Value("${student.sno}")privateString sno;

@Value("${student.sname}")privateString sname;

@Value("${student.age}")privateString age;

@RequestMapping("/test")

@ResponseBodypublicString Test() {

String result= "hello,world";

String config= "sno=" + sno + "|sname=" + sname + "|age=" +age;

result+=config;returnresult;

}

}

packagecom.ligy.controller;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;

@Controller

@ConfigurationProperties(prefix= "student2")

@PropertySource(value= "classpath:my.properties", encoding = "UTF-8")

@Componentpublic classStudent2 {

@Value("${student.sno}")privateString sno;

@Value("${student.sname}")privateString sname;

@Value("${student.age}")privateString age;publicString getSno() {returnsno;

}public voidsetSno(String sno) {this.sno =sno;

}publicString getSname() {returnsname;

}public voidsetSname(String sname) {this.sname =sname;

}publicString getAge() {returnage;

}public voidsetAge(String age) {this.age =age;

}

@RequestMapping("/test2")

@ResponseBodypublicString Test2() {

String result= "hello,world";

String config= "sno=" + sno + "|sname=" + sname + "|age=" +age;

result+=config;returnresult;

}

}

bfc3d591642239b1433927431ea6ab22.png

817636f0b0f23dc18d28db6cabb6d59e.png

二 处理跨域问题(过滤器、@CrossOrigin注解)

2.1html调用接口,报跨域问题

2.2解决跨域问题

菜鸟教程(runoob.com)

$("button").click(function(){

$("#div1").load("http://localhost:8084/test2");

});

});

使用 jQuery AJAX 修改文本内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值