0

I have a <h:outputText> in the view. In the controller, I would like to use below code to manipulate the value and style attributes. Is it possible?

private void updateMsgCreateRegistrationKey(String objectName, String msg, String msgType){
    UIComponent UIOutputText = getUIComponentOfId(FacesContext.getCurrentInstance().getViewRoot(), objectName);
    if (UIOutputText != null){
        UIOutputText.setRendered(true);
        if(msgType.equalsIgnoreCase("Info")){

        }else if(msgType.equalsIgnoreCase("Error")){

        }
    }
    RequestContext.getCurrentInstance().update(objectName);
    FacesContext.getCurrentInstance().renderResponse();
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

1

This is not the normal approach. You normally don't manipulate the view from the controller on. Instead, in MVC, you let the controller (the backing bean) manipulate the model (the backing bean properties) and let the view (the XHTML file) check the model (the backing bean properties).

private String value;
private String styleClass;
private boolean rendered;

public void someMethod() {
    value = "some value";
    styleClass = "someClass";
    rendered = true;
}

// +getters (no setters necessary)
<h:outputText value="#{bean.value}"
              styleClass="#{bean.styleClass}"
              rendered="#{bean.rendered}" />

Or even without rendered property:

<h:outputText value="#{bean.value}"
              styleClass="#{bean.styleClass}"
              rendered="#{not empty bean.value}" />

Every time you thought you need to grab an UIComponent from the view in the controller, stop immediately and think twice if you're really doing things the right way.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555