How to build “edit” button in JSF and switch between h:outputText and h:inputText

后端 未结 1 1119
囚心锁ツ
囚心锁ツ 2020-12-17 05:15

How can I create an \"edit\" button so that when the button is clicked it will change the h:outputText to h:inputText?

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 06:08

    Make use of the rendered attribute:

    
    
    ...
    
    
    

    With this in a view scoped bean:

    private boolean editmode;
    
    public void edit() {
        editmode = true;
    }
    
    public void save() {
        entityService.save(entity);
        editmode = false;
    }
    
    public boolean isEditmode() {
        return editmode;
    }
    
    // ...
    

    Note that the bean being view scoped is important for the reason mentioned in point 5 of this answer: commandButton/commandLink/ajax action/listener method not invoked or input value not updated.


    Alternatively, you can use the disabled attribute on input component in combination with a shot of CSS which basically makes it look like an output component (by removing the border).

    
    ...
    
    
    

    with e.g.

    input[disabled] {
        border: 0;
    }
    

    Also here, the bean must be view scoped. See also How to choose the right bean scope?

    0 讨论(0)
提交回复
热议问题