转载请标明出处:https://blog.csdn.net/men_ma/article/details/106847165.
本文出自 不怕报错 就怕不报错的小猿猿 的博客
本篇easyUI前端框架的tree前台展示博客是由 java ——easyUI前端框架tree(树)后台实现.演变渲染出来的,所以得两篇博客一起结合才能完成一个完整的tree树节点结构
本博主自己写的,提供解决方式:链接: 如何解决eclipse中的行注释中乱码问题及eclipse项目页面展示中文乱码问题(字符编码问题)?.
这是博主在做这个案例时,遇到的一些问题,现在可以少掉一点头发了,哈哈哈
如果修改了index1.js文件中的内容,一定要清除浏览器的历史记录,不然有些功能可能显示不出来
清空浏览器历史记录快捷键:shift+Ctrl+delete
在进行数据查询时,我们需要用到自定义mvc框架,把mvc框架打包成一个jar包,拿来使用,还需要框架的工具类
打包好的jar
博主的博课中有框架的源码,自己去打包就好了,现提供:链接: 自定义MVC完整版.
自定义MVC(一).
<?xml version="1.0" encoding="UTF-8"?>
<config>
<action path="/menu" type="com.xiaoqing.web.MenuAction">
<!-- <forward name="menu" path="/index.jsp" redirect="false" /> -->
</action>
</config>
MenuDao.java的修改:
public class MenuDao extends BaseDao<Menu>{
public List<Menu> list(Menu menu,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_easyui_menu";
return super.executeQuery(sql, Menu.class, pageBean);
}
public List<TreeVo<Menu>> topNode(Menu menu,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
List<Menu> list = this.list(menu, pageBean);
List<TreeVo<Menu>> nodes=new ArrayList<TreeVo<Menu>>();
TreeVo treeVo=null;
for (Menu p : list) {
treeVo=new TreeVo<>();
treeVo.setId(p.getMenuid());
treeVo.setText(p.getMenuname());
treeVo.setParentId(p.getParentid()+"");
Map<String, Object> attributes=new HashMap<String, Object>();
attributes.put("self", p);
treeVo.setAttributes(attributes);
nodes.add(treeVo);
}
// 0 :可以指定哪个节点为父节点,选择自己想要的菜单
return BuildTree.buildList(nodes,"-1");
}
}
ResponseUtil:将对象转为json格式的工具类
public class ResponseUtil {
// 返回结果集到前端页面
public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
// o:对象
public static void writeJson(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
ObjectMapper om=new ObjectMapper();
// 对象转换成json
String jsonStr = om.writeValueAsString(o);
// 输出到jsp页面端去
PrintWriter out=response.getWriter();
out.println(jsonStr.toString());
out.flush();
out.close();
}
}
MenuAction.java:处理json格式可以拿到json字符串再返回到前端页面
public class MenuAction extends ActionSupport implements ModelDriver<Menu>{
private MenuDao pd=new MenuDao();
private Menu menu=new Menu();
@Override
public Menu getModel() {
return menu;
}
public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
try {
// TreeVo<Menu> topNode = this.pd.topNode(null, null);
// 放到一个容器里面 json格式数据
// List<TreeVo<Menu>> list=new ArrayList<TreeVo<Menu>>();
// list.add(topNode);
ResponseUtil.writeJson(resp, this.pd.topNode(null, null));
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// 结果码的配置就是为了,在mvc.xml中寻找到底是重定向还是转发
return null;
}
}
前端index.jsp中的引入的js文件(index.js):
作用:渲染数据
$(function() {
$('#tt').tree({
url:$("#ctx").val()+'/menu.action?methodName=menuTree'
});
})