1.方法一
调试时只能获取eclipse 项目未编译前的路径 不太好用
/* private static Logger logger = Logger.getLogger(BookController.class); */ @RequestMapping("/index") public String bookHandle(HttpServletRequest servlet) { JSONObject json = JsonResourceUtils.getJsonObjFromResource ("static/json/book_nav.json",servlet.getServletContext().getRealPath("/")); return "book"; }
2.方法 二
获取项目运行时的真实类路径
/* private static Logger logger = Logger.getLogger(BookController.class); */ @RequestMapping("/index") public String bookHandle(HttpServletRequest servlet) { JSONObject json = JsonResourceUtils.getJsonObjFromResource /* 这里直接获取到了文件路径 */ (BookController.class.getClassLoader().getResource("static/json/book_nav.json").getPath()); return "book"; }
原文地址:https://www.cnblogs.com/jiangfeilong/p/11106129.html
时间: 2024-09-30 15:42:33