HashMap会自行更改值

时间:2019-06-17 18:52:24

标签: java jsf hashmap

我正在使用jsf,并且在bean中存在一些问题。当我按下按钮Fine时,我会打电话给dataTreeBean.addAttributo。问题在于它仅在首次调用时才能正常运行。 假设我作为参数(key1, v1)传递,它们已正确插入attributes中。当我再次按下按钮Fine,并且参数为(key2, v2)时,在方法开始之前我在(key1, v2)中找到了(key1, v1)而不是attributes (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>

1 个答案:

答案 0 :(得分:0)

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

public void generateAtt(TreeNode node) {

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

    }

,然后修改创建createNodeTRel

public void createNodeTRel() {

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

        newNode = null;

    }