Thursday, July 16, 2015

Formatting a String in Java

Below is an example how we can format a string in a java class:

StringBuilder stringBuilderMessage = new StringBuilder("<html><body>");        

String panelHeading = "Heading";
String panelMessage = " Hello ";

stringBuilderMessage .append("<p style='color:red'><b>"+panelHeading +"</b></p>");

stringBuilderMessage .append("<p>"+panelMessage +"<b>");
stringBuilderMessage .append("</body></html>");

String formatedMessage = stringBuilderMessage.toString(); 

this.addFacesMessageWithMessageText(formatedMessage, FacesMessage.SEVERITY_INFO, null);

    public static void addFacesMessageWithMessageText(String messageTxt, FacesMessage.Severity severity,
                                                      UIComponent component) {
        FacesContext fcontxt = FacesContext.getCurrentInstance();
        FacesMessage fm = new FacesMessage(severity, "", messageTxt);
        if (component == null) {
            fcontxt.addMessage(null, fm);
        } else {
            fcontxt.addMessage(component.getClientId(), fm);
        }
    }

No comments:

Post a Comment