创建一个测试类,添加
@ControllerAdvie
和@ModelAttribute
注解
package com.model.test;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import java.util.ArrayList;
import java.util.List;
/**
* 全局数据对象访问test
*/
@ControllerAdvice
public class TestDataController {
@ModelAttribute(value = "test")
public List<String> dataTest() {
List<String> list = new ArrayList<>(0);
list.add("test1");
list.add("test2");
list.add("test3");
return list;
}
}
获取定义全局对象数据值
package com.model.controller;
import com.its.dangerouschemical.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@Api(tags = "全局对象测试")
@RestController
@RequestMapping("/testData)
public class TestDataController {
/**
* test
*/
@ApiOperation("test")
@GetMapping("getTestData")
public Result<List<String>> getTestData(@RequestParam int mode, Model model) {
System.out.println(mode);
Map<String, Object> map = model.asMap();
List<String> test = (List<String>) map.get("test");
return Result.ok(test);
}
}
结果显示:
标签:web,SpringBoot,List,springframework,访问,org,test,import,全局 From: https://www.cnblogs.com/2393920029-qq/p/16820488.html