如何从JSON文件中使用Spring MVC的设计侧菜单栏(How to design side m

2019-09-28 18:22发布

我试图使用JSON文件,下面的Spring MVC是我的三个文件来设计JSP主页的侧边菜单中,JSP页面包含的,是我在找边设计菜单的方式,JSON文件包含菜单和子菜单数据的代码,在春天控制器我读JSON文件和验证验证后的用户,我有数据发送到JSP对此我从JSON文件中获取,并使其菜单和子菜单中有两个不同的菜单,r1和r2

在这里我所面临的问题是如何从春季控制器JSP JSON数据并使其为菜单和子菜单

    user 
      adduser
      deleteuser
      updateuser
job
add job
delete job
update job

JSP

<li>
                        <a href="#"><!-- <i class="fa fa-wrench fa-fw"></i> -->User<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="javascript:void(0)">add user</a>
                            </li>
                            <li>
                                <a href="javascript:void(0)">delete user</a>
                            </li>
                            <li>
                                <a href="javascript:void(0)">update user</a>
                            </li>

                        </ul>
                        <!-- /.nav-second-level -->
                    </li>

                    <li>
                        <a href="#"><!-- <i class="fa fa-wrench fa-fw"></i> -->job<span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li>
                                <a href="javascript:void(0)">add job</a>
                            </li>
                            <li>
                                <a href="javascript:void(0)">delete job</a>
                            </li>
                            <li>
                                <a href="javascript:void(0)">update job</a>
                            </li>

                        </ul>
                        <!-- /.nav-second-level -->
                    </li>

JSON文件

{
    "userid": "abc",
    "roles": ["admin"],
    "sidebar":  
    {           
                "job": ["addjob", "updatejob", "deletejob"]
            ,

                "user": ["adduaser", "deleteuser", "updateuser"]


    }   

调节器

 @RequestMapping(value = "/authenticateLogin", method = RequestMethod.POST)
            public   String loginAuthentication(HttpServletRequest request) {       
    String username=request.getParameter("userName");       
    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(new FileReader("d:\\test.json"));
        JSONObject jsonObject = (JSONObject) obj;

        String userID1 = (String) jsonObject.get("userid");
        if (username.equals(userID1)) {
            JSONArray rolesArray = (JSONArray) jsonObject.get("roles");
            String role = (String) rolesArray.get(0);
            if (role.equals("admin")) {

                JSONObject sideContent = (JSONObject) jsonObject.get("sidebar");
                JSONArray job = (JSONArray) sideContent.get("job");
                JSONArray user = (JSONArray) sideContent.get("user");

                ArrayList<String> jobArrayList = new ArrayList<String>();
                ArrayList<String> userArrayList = new ArrayList<String>();

                Iterator<String> iterator1 = job.iterator();
                while (iterator1.hasNext()) {                       
                    jobArrayList.add(iterator1.next());
                }
                Iterator<String> iterator2 = user.iterator();
                while (iterator2.hasNext()) {                       
                    userArrayList.add(iterator2.next());
                }                   
                return "";
            } 


            if(role.equals("user"))
            {
                JSONObject sideContent = (JSONObject) jsonObject.get("sidebar");
                JSONArray job = (JSONArray) sideContent.get("job");
                ArrayList<String> jobArrayList = new ArrayList<String>();
                Iterator<String> iterator1 = job.iterator();
                while (iterator1.hasNext()) {                       
                    jobArrayList.add(iterator1.next());
                }                   
                return "";
            }
        }           

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }


    return "Error";

}

Answer 1:

我已经做了样品3级菜单,标题的onclick路径和图标。

Java端

    public static ArrayList<Menu> getMenuTree(){
    ArrayList<Menu> menuTree = new ArrayList<Menu>();
    menuTree.add(new Menu("Menu 1", "#", "fa fa-tachometer", getSub()));
    menuTree.add(new Menu("Menu 2", "./onclickPath", "fa fa-cogs", new ArrayList<Menu>()));
    menuTree.add(new Menu("Menu 3", "./onclickPath", "fa fa-cogs", new ArrayList<Menu>()));
    return menuTree ;
}

public static ArrayList<Menu> getSub(){
    ArrayList<Menu> menu = new ArrayList<Menu>();
    menu.add(new Menu("Sub Menu 1", "#", "icofont-dashboard", getSub2()));
    menu.add(new Menu("Sub Menu 2", "./onclickPath", "fa fa-cogs", new ArrayList<Menu>()));
    menu.add(new Menu("Sub Menu 3", "./onclickPath", "fa fa-cogs", new ArrayList<Menu>()));
    return menu ;
}
public static ArrayList<Menu> getSub2(){
    ArrayList<Menu> menu = new ArrayList<Menu>();
    menu.add(new Menu("Sub Menu 1A", "./onclickPath", "fa fa-line-chart", new ArrayList<Menu>()));
    menu.add(new Menu("Sub Menu 1B", "./onclickPath", "fa fa-cogs", new ArrayList<Menu>()));
    menu.add(new Menu("Sub Menu 1C", "./onclickPath", "fa fa-cogs", new ArrayList<Menu>()));
    return menu ;
}

菜单Bean类

public class Menu {
private String title, url, icon;
private ArrayList<Menu> subMenu;

public Menu(String title, String url, String icon, ArrayList<Menu> subMenu) {
    this.title = title;
    this.url = url;
    this.icon = icon;
    this.subMenu = subMenu;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public String getIcon() {
    return icon;
}

public void setIcon(String icon) {
    this.icon = icon;
}

public ArrayList<Menu> issubMenu() {
    return subMenu;
}

public void setsubMenu(ArrayList<Menu> subMenu) {
    this.subMenu = subMenu;
}

public ArrayList<Menu> getSubMenu() {
    return subMenu;
}

public void setSubMenu(ArrayList<Menu> subMenu) {
    this.subMenu = subMenu;
}

public int getSize() {
    return subMenu.size();
}

}

JSP

<ul class="page-sidebar-menu">

            <c:forEach var="menu" items="${AaaTestController.getMenuTree()}">

                <li><a href="${menu.getUrl()}"> <i style="margin-right:5px;" class="${menu.getIcon()}"></i>
                        <c:choose><c:when test="${menu.getSize()>0}">
                        ${menu.getTitle()} <i style="float:right;" class="fa fa-angle-down"></i></a> 
                            <ul class="sub-menu">
                                <c:forEach var="subMenu" items="${menu.getSubMenu()}">
                                    <li><a href="${subMenu.getUrl()}"> <i  style="margin-right:2px;"  class="${subMenu.getIcon()}"></i>
                                        <c:choose><c:when test="${subMenu.getSize()>0}">
                                        ${subMenu.getTitle()} <i style="float:right; margin-right:23px;" class="fa fa-angle-down"></i></a> 
                                            <ul class="sub-menu">
                                                <c:forEach var="subMenu2" items="${subMenu.getSubMenu()}">
                                                    <li><a href="${subMenu2.getUrl()}"> <i  style="margin-right:3px;" class="${subMenu2.getIcon()}"></i>${subMenu2.getTitle()}</a></li>
                                                </c:forEach>
                                            </ul>
                                        </c:when>
                                        <c:otherwise>
                                      ${subMenu.getTitle()}</a> 
                                    </c:otherwise>
                                        </c:choose>
                                    </li>
                                </c:forEach>
                            </ul>
                        </c:when>
                        <c:otherwise>
                        ${menu.getTitle()}</a> 
                        </c:otherwise>
                        </c:choose>
                </li>

            </c:forEach>
 </ul>



文章来源: How to design side menu bar from json file using spring mvc