当我使用数组创建导航栏时,无法为JSP编译类,并且无法从String转换为布尔值

时间:2023-01-23 22:52:36

I have an idea to create a navigation bar with Arrays and looping it with While.

我有一个想法,创建一个带有数组的导航栏,并使用While循环它。

first, I make a variable on my jsp file like this:

首先,我在我的jsp文件上创建一个变量,如下所示:

<%
int i, j;
i = 0;
j = 0;

String[][] NavMenu = new String[6][5];
NavMenu[0][0] = "Company Profile";
NavMenu[1][0] = "Product";
NavMenu[2][0] = "Marketing Plan";
NavMenu[3][0] = "News";
NavMenu[3][1] = "Event";
NavMenu[3][2] = "Promo";
NavMenu[3][3] = "Artikel";
NavMenu[3][4] = "Testimony";
NavMenu[4][0] = "Merchandise";
NavMenu[5][0] = "Job Vacancies";

String[][] NavLink = new String[6][5];
NavLink[0][0] = "company-profile-infiny-niaga-abadi.html";
NavLink[1][0] = "product.html";
NavLink[2][0] = "marketing-plan.html";
NavLink[3][0] = "#";
NavLink[3][1] = "news/event.html";
NavLink[3][2] = "news/promo.html";
NavLink[3][3] = "news/article.html";
NavLink[3][4] = "news/testimony.html";
NavLink[4][0] = "merchandise.html";
NavLink[5][0] = "job-vacancies.html";
%>

The NavMenu is a text for Navbar menu, and NavLink is a link for Navbar menu.

NavMenu是Navbar菜单的文本,NavLink是Navbar菜单的链接。

and then, I make my navbar like this:

然后,我像这样制作我的导航栏:

<%
while (NavMenu[i][0]) { //ERROR ON HERE: cannot convert from String to boolean
    j = 1;

    //IF MENU HAVE A DROPDOWN
    if(NavMenu[i][1]){ //ERROR ON HERE: cannot convert from String to boolean
        if(NavMenu[i][0] == ActivePage) { %>
            <li class="active dropdown">
        <% } else { %>
            <li class="dropdown">
        <% } %>

        <a href="<%= NavLink[i][0] %>" class="dropdown-toggle" data-toggle="dropdown"><%=NavMenu[i][0]%> <b class="caret"></b></a>
        <ul class="dropdown-menu">

        <%
        while(NavMenu[i][j]) { //ERROR ON HERE: cannot convert from String to boolean %>
            <li>
                <a href="<%= NavLink[i][j]%>"><%= NavMenu[i][j]%></a>
            </li>
            <% j++;
        } %>
        </ul>
    </li>

    <% 
    //IF MENU DO NOT HAVE A DROPDOWN
    } else {
        if(NavMenu[i][0] == ActivePage){ %>
            <li class="active"><a href="<%= NavLink[i][0]%>"><%= NavMenu[i][0] %></a></li>
        <% } else {%>
            <li><a href="<%= NavLink[i][0] %>"><%= NavMenu[i][0] %></a></li>
        <%}
    }
    i++;
    j=0;
}
%>

How to solve this Problem? I have no idea what should I do to make this working.

如何解决这个问题呢?我不知道我该怎么做才能使这个工作。

1 个解决方案

#1


Java is not like javascript, you can not use string as condition, In java the condition must be in boolean. you can use a for loop to iterate over the String array and do your logic

Java不像javascript,你不能使用string作为条件,在java中条件必须是boolean。您可以使用for循环迭代String数组并执行您的逻辑

<%


String[][] NavMenu = new String[6][5];
NavMenu[0][0] = "Company Profile";
NavMenu[1][0] = "Product";
NavMenu[2][0] = "Marketing Plan";
NavMenu[3][0] = "News";
NavMenu[3][1] = "Event";
NavMenu[3][2] = "Promo";
NavMenu[3][3] = "Artikel";
NavMenu[3][4] = "Testimony";
NavMenu[4][0] = "Merchandise";
NavMenu[5][0] = "Job Vacancies";

String[][] NavLink = new String[6][5];
NavLink[0][0] = "company-profile-infiny-niaga-abadi.html";
NavLink[1][0] = "product.html";
NavLink[2][0] = "marketing-plan.html";
NavLink[3][0] = "#";
NavLink[3][1] = "news/event.html";
NavLink[3][2] = "news/promo.html";
NavLink[3][3] = "news/article.html";
NavLink[3][4] = "news/testimony.html";
NavLink[4][0] = "merchandise.html";
NavLink[5][0] = "job-vacancies.html";
%>

<%
for (int i=0; i<NavMenu.length; i++) { //ERROR ON HERE: cannot convert from String to boolean

    String navMenuValue = NavMenu[i][0];

    //IF MENU HAVE A DROPDOWN
    if(NavMenu[i].length > 1){ //ERROR ON HERE: cannot convert from String to boolean
        if(NavMenu[i][0].equals("ActivePage")) { %>
            <li class="active dropdown">
        <% } else { %>
            <li class="dropdown">
        <% } %>

        <a href="<%= NavLink[i][0] %>" class="dropdown-toggle" data-toggle="dropdown"><%=NavMenu[i][0]%> <b class="caret"></b></a>
        <ul class="dropdown-menu">

        <%
        for(int j=0;j<NavMenu[i].length;j++) { //ERROR ON HERE: cannot convert from String to boolean %>
            <li>
                <a href="<%= NavLink[i][j]%>"><%= NavMenu[i][j]%></a>
            </li>
            <% 
        } %>
        </ul>
    </li>

    <% 
    //IF MENU DO NOT HAVE A DROPDOWN
    } else {
        if(NavMenu[i][0].equals("ActivePage")){ %>
            <li class="active"><a href="<%= NavLink[i][0]%>"><%= NavMenu[i][0] %></a></li>
        <% } else {%>
            <li><a href="<%= NavLink[i][0] %>"><%= NavMenu[i][0] %></a></li>
        <%}
    }
}
%>

#1


Java is not like javascript, you can not use string as condition, In java the condition must be in boolean. you can use a for loop to iterate over the String array and do your logic

Java不像javascript,你不能使用string作为条件,在java中条件必须是boolean。您可以使用for循环迭代String数组并执行您的逻辑

<%


String[][] NavMenu = new String[6][5];
NavMenu[0][0] = "Company Profile";
NavMenu[1][0] = "Product";
NavMenu[2][0] = "Marketing Plan";
NavMenu[3][0] = "News";
NavMenu[3][1] = "Event";
NavMenu[3][2] = "Promo";
NavMenu[3][3] = "Artikel";
NavMenu[3][4] = "Testimony";
NavMenu[4][0] = "Merchandise";
NavMenu[5][0] = "Job Vacancies";

String[][] NavLink = new String[6][5];
NavLink[0][0] = "company-profile-infiny-niaga-abadi.html";
NavLink[1][0] = "product.html";
NavLink[2][0] = "marketing-plan.html";
NavLink[3][0] = "#";
NavLink[3][1] = "news/event.html";
NavLink[3][2] = "news/promo.html";
NavLink[3][3] = "news/article.html";
NavLink[3][4] = "news/testimony.html";
NavLink[4][0] = "merchandise.html";
NavLink[5][0] = "job-vacancies.html";
%>

<%
for (int i=0; i<NavMenu.length; i++) { //ERROR ON HERE: cannot convert from String to boolean

    String navMenuValue = NavMenu[i][0];

    //IF MENU HAVE A DROPDOWN
    if(NavMenu[i].length > 1){ //ERROR ON HERE: cannot convert from String to boolean
        if(NavMenu[i][0].equals("ActivePage")) { %>
            <li class="active dropdown">
        <% } else { %>
            <li class="dropdown">
        <% } %>

        <a href="<%= NavLink[i][0] %>" class="dropdown-toggle" data-toggle="dropdown"><%=NavMenu[i][0]%> <b class="caret"></b></a>
        <ul class="dropdown-menu">

        <%
        for(int j=0;j<NavMenu[i].length;j++) { //ERROR ON HERE: cannot convert from String to boolean %>
            <li>
                <a href="<%= NavLink[i][j]%>"><%= NavMenu[i][j]%></a>
            </li>
            <% 
        } %>
        </ul>
    </li>

    <% 
    //IF MENU DO NOT HAVE A DROPDOWN
    } else {
        if(NavMenu[i][0].equals("ActivePage")){ %>
            <li class="active"><a href="<%= NavLink[i][0]%>"><%= NavMenu[i][0] %></a></li>
        <% } else {%>
            <li><a href="<%= NavLink[i][0] %>"><%= NavMenu[i][0] %></a></li>
        <%}
    }
}
%>