编辑Primefaces递归树时的java.lang.NumberFormatException

时间:2013-11-12 10:06:50

标签: java jsf-2 primefaces tree

现在我正从 3.5迁移到4.0 我正在使用递归方法创建此树并从数据库中检索树 使用3.5时,这是按预期工作的。但是当迁移到 4时,我无法使用hibernate jpa进行编辑/合并。该错误仅出现在glassfish控制台 java.lang.NumberFormatException:对于输入字符串:“null”

看看我的实体:

@Entity
@Table(schema = "public", name = "menu")
@XmlRootElement
public class Menu implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(nullable = false)
private Integer menuid;
@Size(max = 100)
@Column(length = 100)
private String menuname;
private Integer parentid;
private Integer level;
private Integer ordering;
@Temporal(TemporalType.DATE)
private Date sessiondate;
@Size(max = 15)
@Column(length = 15)
private String action;
@Size(max = 15)
@Column(length = 15)
private String menutype;
@Column(name = "icon")
private String icon;
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "parentid")
@OrderBy(value = "ordering")
private List<Menu> children = new LinkedList<>();

//appropriate getter setter

}

和我创建递归的方法:

public void generateMenuTree() throws SQLException {
    // findUserConfirmationDefault root menu
    rootMenu = menuFacadeLocal.findHomeMenu();

    //recursively createUserConfirmationDefault tree menu
    menuTree = createTree(rootMenu, null);
}

public TreeNode createTree(Menu menu, TreeNode rootNode) throws SQLException {
    TreeNode newNode = new DefaultTreeNode(menu, rootNode);

    for (Menu subMenu : menu.getChildren()) {
        if (("mm").equalsIgnoreCase(subMenu.getMenutype())) {
            TreeNode newNode2 = createTree(subMenu, newNode);
        }
    }

    return newNode;
}  

我使用以下方式显示树:

<p:tab title="Access Menus">
<p:tree value="#{managePositionBean.menuTree}" var="menutree" selectionMode="checkbox" selection="#{managePositionBean.selectedTreeMenu}" animate="true">
    <p:treeNode icon="#{menutree.icon}">
        <h:outputText value="#{msgs[menutree.menuname]}"/>
    </p:treeNode>
</p:tree>

唯一可以null #{menutree.icon}的事情 啊,是的,堆栈跟踪:

INFO: java.lang.NumberFormatException: For input string: "null"
java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at org.primefaces.component.api.UITree.findTreeNode(UITree.java:156)
at org.primefaces.component.api.UITree.setRowKey(UITree.java:87)
at org.primefaces.component.tree.TreeRenderer.decodeSelection(TreeRenderer.java:86)
at org.primefaces.component.tree.TreeRenderer.decode(TreeRenderer.java:56)
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:787)
at org.primefaces.component.api.UITree.processDecodes(UITree.java:294)
at org.primefaces.component.tree.Tree.processDecodes(Tree.java:370)
at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1176)
at org.primefaces.util.ComponentUtils.processDecodesOfFacetsAndChilds(ComponentUtils.java:414)
at org.primefaces.component.tabview.TabView.processDecodes(TabView.java:325)
at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
at com.sun.faces.context.PartialViewContextImpl$PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:506)
at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183)
at javax.faces.component.UIForm.visitTree(UIForm.java:354)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1601)
at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:376)
at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252)
at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183)
at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57)
at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.omnifaces.facesviews.FacesViewsForwardingFilter.doFilter(FacesViewsForwardingFilter.java:121)
at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.omnifaces.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:115)
at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.omnifaces.filter.GzipResponseFilter.doFilter(GzipResponseFilter.java:148)
at org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at ch.qos.logback.classic.helpers.MDCInsertingServletFilter.doFilter(MDCInsertingServletFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:744)  

我甚至删除了显示的图标,但仍然无法正常工作。
我的环境是JEE 7Mojarra 2.1.24Glassfish 3.1.2.2Primefaces 3.5Primefaces Extension 1.0Omnifaces 1.5

我很困惑,问题是什么,是错误的表面或mojarra或其他地方的错误还是仅仅是我? 感谢。

更新

也许我觉得我发现了问题,在用html检查生成的firebug后,我在树下发现了这个input hidden。它内部的值似乎是被选中的id组件。勾选并取消某些或树节点后,其中的值填充或消失。

<input type="hidden" id="Position:tView:j_idt110_selection" name="Position:tView:j_idt110_selection" value="null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,0,0_0,0_1,0_2,0_3,0_4,0_5,4,4_0,4_1,4_2,6,6_0,6_0_0,6_0_1,6_0_2,6_0_3,6_0_4,6_1,6_1_0,6_1_0_0,6_1_0_1,6_1_0_2,6_1_0_3,6_1_0_4,6_1_0_5,6_1_0_6,6_1_1,6_1_2,6_1_3,6_1_4,6_1_5,6_1_6,6_1_7,6_1_8,6_1_9,6_1_10,6_1_11,6_1_12,6_1_13,6_1_14,6_1_15,6_1_16,6_1_17,6_2,6_2_0,6_2_0_0,6_2_0_1,6_2_1,6_2_2,6_2_3,6_2_4,6_2_5,6_2_6,6_2_6_0,6_2_6_1,6_2_6_2,6_2_7,6_3,6_3_0,6_3_1,6_3_2,6_4,6_4_0,6_4_1,6_4_2,6_4_3,6_4_3_0,6_4_3_1,6_4_3_2,1,1_0,1_0_0,1_0_1,1_0_2,1_1,1_1_0,1_1_1,1_1_2,1_1_3,2,2_0,2_0_0,2_0_1,2_0_2,2_0_3,2_0_4,2_0_5,2_1,2_1_0,2_1_1,2_1_2,2_1_3,2_1_4,2_1_5,2_1_6,2_1_7,2_2,3,3_0,3_1,3_2,3_3,3_3_0,3_3_1,5,5_0,5_0_0,5_0_1,5_0_2,5_0_3,5_0_4,5_0_5,5_1,5_1_0,5_1_1,5_1_2,5_1_3,5_1_4,5_1_5,5_2,5_2_0,5_2_1,5_2_2,5_2_3,5_2_4,5_3,5_3_0,5_3_1,5_3_2,5_3_3,5_3_4,5_3_5,5_3_6,5_4">

我将展示编辑方法:

public void editingPosition(final Position pList) {
        position = pList;

        List<PrivilageuserDefault> defaults = privilageuserDefaultFacadeLocal.findPrivilageuserDefaultByPositionid(position.getPositionid());
        positionPrivilegesMap = privilageuserDefaultFacadeLocal.searchMapPrivilegeUserDefault(position.getPositionid());

        if (!defaults.isEmpty()) {
            PrivilageuserDefault[] arrayOfPositionPrivileges = defaults.toArray(new PrivilageuserDefault[defaults.size()]);

            int i = 0;
//                this.selectedTreeMenu = new TreeNode[defaults.size()]; --> commented on 2nd update
            for (PrivilageuserDefault pd : arrayOfPositionPrivileges) {
//                    TreeNode node = new DefaultTreeNode(pd.getMenu(), null); --> commented on 2nd update
//                    this.selectedTreeMenu[i] = node; --> commented on 2nd update
                i++;
            }

            checkTreeNodes(menuTree);
        }

        status = 2;
    }

如果存在,这就是勾选树:

private void checkTreeNodes(TreeNode root) {
        if (root.getChildCount() > 0) {
            for (TreeNode children : root.getChildren()) {
                Menu menu = (Menu) children.getData();
                if (positionPrivilegesMap.containsKey(menu.getMenuid())) {
                    children.setSelected(true);
                }

                // recursive check
                checkTreeNodes(children);
            }
        }
    }

我想,我在CRUD概念中做错了关于勾选/取消树,或者我错过了一些步骤。

更新

是的,我是对的,上面输入隐藏的null值是selectedTreeMenu变量是罪魁祸首。在我评论填充selectedTreeMenu的行(我已经更新了上面snipets上的注释行)之后,现在我可以从数据库中检索数据并选中树上的选定节点。但是,在我选择了更新所选树而没有勾选/取消选定节点之后,它已成功更新,但所选节点未更新/插入数据库/已删除。所以它的权利,它只是

children.setSelected(true);

看来,selectedTree变量也必须填写,但是怎么做呢? :/

2 个答案:

答案 0 :(得分:0)

最后,我找到了一些workround,快速补丁。我制作了新的List变量

private List<TreeNode> selectedTreeList;

并用节点填充

private void checkTreeNodes(TreeNode root) {
        if (root.getChildCount() > 0) {
            for (TreeNode children : root.getChildren()) {
                Menu menu = (Menu) children.getData();
                if (positionPrivilegesMap.containsKey(menu.getMenuid())) {
                    children.setSelected(true);
                    expand(children);
                    selectedTreeList.add(children); // --> filled with this
                }

                // recursive check
                checkTreeNodes(children);
            }
        }
    }

在向用户界面显示之前,我将节点的List更改为Array

selectedTreeMenu = selectedTreeList.toArray(selectedTreeMenu);

瞧!

答案 1 :(得分:0)

感谢您指出我在PrimeFaces 5.3中遇到的问题。在我的情况下,当我以编程方式添加子项时,rowKey未设置。我可以通过在代码中设置rowKey来解决这个问题,只需将分隔符和子计数添加到当前节点的rowKey

public abstract class TreeNodeWrapper<T extends Serializable>
    implements Serializable, TreeNode
{
    ...

    public TreeNodeWrapper<T> addChild(String name)
    {
        T clone = cloneEntity(name);
        ...
        TreeNodeWrapper<T> childNode = newInstance(clone ...);
        String rowKeyFix = getRowKey();
        if (rowKeyFix != null) {
            rowKeyFix += "_" + this.children.size();
            childNode.setRowKey(rowKeyFix);
        }
        this.children.add(childNode);
        return childNode;
    }
}