簡體   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