一、设计思路
将菜单写在文件menu.json里,后台读取该文件,并将菜单输出到页面上。
二、技术点
1、读取JSON文件
2、序列化成实体数组
3、thymeleaf循环输出
三、具体描述
1、menu.json
[{
"id": 1,
"name": "home",
"title": "首页",
"url": "home/"
},{
"id": 2,
"name": "hyjj",
"title": "知识经济",
"url": "hyjj/"
},{
"id": 3,
"name": "fzjz",
"title": "防灾减灾",
"url": "fzjz/"
},{
"id": 4,
"name": "cgzh",
"title": "成果转化",
"url": ""
}]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2、菜单项实体类
public class Menu {
private int id;
private String name;
private String title;
private String url;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
3、读取菜单
1)读取json文件静态类JsonUtils
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class JsonUtils {
public static <T> T readSingle(String path, Type type) throws IOException {
ClassPathResource resource = new ClassPathResource(path);
if (resource.exists()) {
return JSON.parseObject(resource.getInputStream(), StandardCharsets.UTF_8, type,
// 自动关闭流
Feature.AutoCloseSource,
// 允许注释
Feature.AllowComment,
// 允许单引号
Feature.AllowSingleQuotes,
// 使用 Big decimal
Feature.UseBigDecimal);
} else {
throw new IOException();
}
}
public static <T> List<T> readArray(String path, Class<T> t) throws IOException {
ClassPathResource resource = new ClassPathResource(path);
if (resource.exists()) {
InputStream stream = resource.getInputStream();
byte[] bytes = new byte[stream.available()];
stream.read(bytes);
return JSON.parseArray(new String(bytes), t);
} else {
throw new IOException();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
2)调用JsonUtils
public List<Menu> getMenus() {
try {
return JsonUtils.readArray("static/data/menu.json", Menu.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4、输出给前端
@Controller("homeIndexController")
@RequestMapping(value = "/home")
public class IndexController extends BaseController {
@RequestMapping(value = "/")
public String Index(Model model){
model.addAttribute("menus",getMenus());//getMenus见上面
return "home/index";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
5、前端展示
<div th:each="item:${menus}">
<span th:text="${item.title}"></span>
</div>
- 1
- 2
- 3
四、总结
重点在于读取JSON文件并序列化成实体数组这里。
我用到了fastjson,虽然支持泛型,但没有彻底,分为2种情况:一种就是输入内容为JSON对象({“id”:1,…}),一种是JSON数组([{“id”:1,…},{“id”:2,…},…])。在本例中,很显然是个数组。刚开始用JSON.parseObject,会报错。
感觉fastjson这个类有点怪,JSON.parseObject支持多种格式,编码,而JSON.parseArray只重载了几种情况。其次是调用JSON.parseArray如何传参遇到了点麻烦,从网上找例子,都不完整,颇感郁闷。所以我现在给出完整的例子。