1

我正在尝试根据 JSP 中的服务器调用在运行时构建一个菜单。代表菜单的模型如下所示:

import java.util.ArrayList;
import java.util.List;

public class MenuItem {
    public String menuText;
    public List<MenuItem> subMenuItems=new ArrayList<MenuItem>();

    public MenuItem(String menuText) {
        super();
        this.menuText = menuText;
        this.subMenuItems = new ArrayList<MenuItem>();
    }

    public MenuItem(){

    }

    public MenuItem(String menuText, List<MenuItem> subMenuItems) {
        super();
        this.menuText = menuText;
        this.subMenuItems = subMenuItems;
    }

    // @Override
    // public String toString() {
    // // TODO Auto-generated method stub
    // StringBuffer buffer = new StringBuffer();
    // for (MenuItem menuItem : getSubMenuItems()) {
    // buffer.append(menuItem.toString());
    // }
    // return ("  Menu --->" + getMenuText() + buffer.toString());
    //
    // }

    public String getMenuText() {
        return menuText;
    }

    public void setMenuText(String menuText) {
        this.menuText = menuText;
    }

    public List<MenuItem> getSubMenuItems() {
        return subMenuItems;
    }

    public void setSubMenuItems(List<MenuItem> subMenuItems) {
        this.subMenuItems = subMenuItems;
    }

}

对于上述模型,我们从控制器传递示例菜单:

   List<MenuItem> menuMainList = new ArrayList<MenuItem>();
            List<MenuItem> submenus = new ArrayList<MenuItem>();
            MenuItem item1= new MenuItem();
            item1.setMenuText("****");
            submenus.add(item1);
            MenuItem item2 = new MenuItem();
            item2.setMenuText("SampleMenu");

            item2.setSubMenuItems(submenus);

            menuMainList.add(item2);
            System.out.println(menuMainList);
            model.addAttribute("menuItem", menuMainList);

在 JSP 页面上,我们尝试执行以下操作:

<c:forEach items="${menuItem}"  begin="0" var="menuListItem">
            <c:choose>
                <c:when test="${empty menuListItem.subMenuItems}">
                    <div>
                        <c:out value="${menuListItem.menuText}" />
                    </div>
                </c:when>
                <c:otherwise>
                    <div>
                        <span><c:out value="${menuListItem.menuText}" /></span>
                        <div style="width: 150px;">
                            <cobTags:menuDivItem menuList="${menuListItem.subMenuItems}"></cobTags:menuDivItem>
                        </div>
                    </div>
                </c:otherwise>
            </c:choose>
        </c:forEach>

自定义标签是:

 <%@ tag language="java" pageEncoding="ISO-8859-1"%>
<%@ attribute name="menuList" required="true"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="cobTags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<c:forEach items="${menuList}" begin="0" var="menuListItem">
    <div>
        <cobTags:drawMenuItem  menuItem="${menuListItem}"></cobTags:drawMenuItem>
        <c:out value="${menuListItem}" />
    </div>
</c:forEach>

    <%@ tag language="java" pageEncoding="ISO-8859-1"%>
<%@ attribute name="menuItem" type="com.sample.bean.MenuItem"
    required="true"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div style="color: red;">
    <c:out value="${menuItem.menuText}" />
</div>

当我执行以下代码时,出现错误:

无法将 java.lang.String 类型的 [com.sample.bean.MenuItem@547ca73] 转换为 com.sample.bean.MenuItem 类

当我将对象传递给自定义标签时,我不明白为什么会调用模型的 toString 方法。?任何指针?

4

1 回答 1

4

声明参数的类型应该可以解决问题:

<%@ attribute name="menuList" required="true" type="java.util.Collection" %>
于 2013-11-14T08:02:15.123 回答