Builder Pattern in Java

The Builder pattern eliminates building the complexity of the target object as well as eases the construction by providing such methods that will assemble the object same as building a set of blocks. By implementing the Builder pattern, there won’t be any need of larger constructors, parameter objects or custom types. Furthermore, the Builder pattern

Immutable Objects in Java

Immutability of an object indicates that once an object is constructed with given values, its state and values cannot be altered. In case of any value change must result in new object creation. Out of box Java offers some Immutable objects for instance java.lang.String, java.lang.Integer, java.lang.Float, java.math.BigDecimal. Immutable objects make generally a good use in

JSF Input text placeholder

In the Tag description add the pt entry and its corresponding web address <html xmlns=”http://www.w3.org/1999/xhtml” xmlns:h=”http://java.sun.com/jsf/html” xmlns:pt=”http://xmlns.jcp.org/jsf/passthrough”> in the input text declaration <h:inputText styleClass=”form-control” pt:placeholder=”Enter Search Value” required=”true” requiredMessage=”Please enter search value” value=”#{orderController.keyword}” id=”keywordSearch”/>

Java XML Parser

Using Dom4J <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> XMLParser.java import org.apache.commons.lang.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.StringReader; import java.io.StringWriter; import java.io.StringWriter; public class XMLFormatter { private static Logger

JSF File Download

<h:commandLink action=”#{logController.downloadMessage}”> <h:outputText styleClass=”fa fa-fw fa-download”/> <f:setPropertyActionListener value=”#{log}” target=”#{logController.selectedLog}”/> </h:commandLink> public void downloadMessage() throws IOException { logger.info(“downloading the log file”); File file = new File(searchKeyword + “_log.txt”); FileUtils.writeStringToFile(file, selectedLog.getMessageContent(), Charset.defaultCharset()); FacesContext facesContext = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); response.setHeader(“Content-Disposition”, “attachment;filename=” + file.getName()); response.setContentLength((int) file.length()); FileInputStream input = null; try { int i = 0;

JSF Primefaces Indicator

This example will show how to show a loader indicator while data is being fetch from the server. <h:panelGrid columns=”1″ id=”searchPanelGrid”> <h:panelGroup> <p:inputText id=”employeeId” placeholder=”Employee Id” required=”true” requiredMessage=”Employee Id is required” value=”#{createAdminController.employeeId}”/> <p:commandButton value=”Search in VCN” update=”ldapPanel @form” actionListener=”#{createAdminController.searchInVCN}” id=”searchButton”/> </h:panelGroup> </h:panelGrid> <p:blockUI block=”searchPanelGrid” trigger=”searchButton”> <h:graphicImage library=”images” name=”loader.gif”/> </p:blockUI>   https://www.primefaces.org/showcase/ui/misc/blockUI.xhtml http://ajaxload.info/ https://answers.axonivy.com/questions/14/how-to-create-a-loading-indicator-in-a-html-user-dialog/15

JSF Return Hostname and App name

private static HttpServletRequest request = (HttpServletRequest) FacesContext .getCurrentInstance().getExternalContext().getRequest(); static Logger logger = LoggerFactory.getLogger(JSFUtils.class); public static String getServerHost() { return “http://” + request.getServerName() + “:” + request.getServerPort(); } public static String getServerHostWithAppName(){ String url = null; try { url = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), request.getContextPath()).toString(); } catch (MalformedURLException e) { logger.error(e.toString()); } return url; }