繁体   English   中英

HashMap会自行更改值

[英]HashMap changes values by itself

我正在使用jsf,我在bean中遇到了一些问题。 当我按下按钮Fine我希望打电话dataTreeBean.addAttributo 问题是它只在第一次调用时才能正常工作。 假设我传递参数(key1, v1) ,它们被正确插入attributes 当我再次按下按钮Fine ,参数是( key2, v2) ,我在方法开始之前在attributes找到(key1, v2)而不是(key1, v1) ,我有(key1, v2)(key2, v2)方法运行后。 我无法理解正在发生的事情,我不知道在方法运行之前如何更改attributes中的值。

treeBean.java

private Attribute a;
private ArrayList<TreeNode> listaClassi;
private TreeNode newNode; 
private TreeNode selectedNode;   

public void createNodeT() {

        TreeNode childNode = new DefaultTreeNode(newNode, null);
        recursiveTree(newNode, childNode);
        recursiveTree2(newNode, selectedNode);
        crAtt(newNode);
        newNode = null;

    }
public void recursiveTree(TreeNode node, TreeNode c) {

        List<TreeNode> children = node.getChildren();
        Attribute a = new Attribute();

        // if (node.getChildCount() > 0) {

        a.setName(node.toString());
        a.setDistinguishing(distinguishing);
        a.setMandatory(mandatory);
        a.setDisplay(display);
        a.setDataType("Taxonomy");
        a.setSubClasses(node);
        aList.add(a);


        for (int j = 0; j < node.getChildCount(); j++) {
            TreeNode childNode = new DefaultTreeNode(children.get(j), c);
            listaClassi.add(children.get(j));
            recursiveTree(children.get(j), childNode);
        }
    }

    public void crAtt(TreeNode node) {

        a.setName(node.toString());
        a.setDistinguishing(distinguishing);
        a.setMandatory(mandatory);
        a.setDisplay(display);
        a.setDataType("Taxonomy");
        a.setSubClasses(node);

    }

public ArrayList<TreeNode> getListaClassi() {
        return listaClassi;
    }


public Attribute getA() {       
return a;   }

dataTreeBean.java:

private HashMap<String, ArrayList<Attribute>> attributes;
private String classe;
private String option;

public void addAttributo(String key, Attribute att) {

    if (attributes.get(key) == null) {
        ArrayList<Attribute> a = new ArrayList<Attribute>();
        a.add(att);
        attributes.put(key, a);
    } else {
        ArrayList<Attribute> a = attributes.get(key);
        a.add(att);
        attributes.put(key, a);
    }

    int i = 0;

}
public String getClasse() {
        return classe;
    }    
public void setOption(String option) {
        this.option = option;
    }

file.xhtml

    <p:commandButton value="Fine" styleClass="ui-confirmdialog-yes"
                                        action="#{dataTreeBean.addClassi(treeBean.getListaClassi())}"
                                        update=":formData:selected_data_property :br:sel_range aggiungiAttTaxonomy :formClassi:selected_class"
                                        oncomplete="PF('addTaxonomy').hide();"
                                        actionListener="#{dataTreeBean.setOption('Taxonomy')}">
                                        <f:actionListener binding="#{treeBean.createNodeT()}" />
                                        <f:actionListener
                                            binding="#{dataTreeBean.addAttributo(dataTreeBean.getClasse(),treeBean.getA())}" />
   </p:commandButton>

我用以下方法解决了在treeBean替换crAttr问题

public void generateAtt(TreeNode node) {

        for (Attribute a : aList) {
            if (a.getName().equals(newNode.toString()))
                this.a = a;
        }

    }

然后修改create createNodeTRel

public void createNodeTRel() {

        TreeNode childNode = new DefaultTreeNode(newNode, null);
        recursiveTree(newNode, childNode);
        recursiveTree(newNode, selectedNode);
        generateAtt(newNode);

        newNode = null;

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM