Package com.jk.web.util
Class JKJsfUtil
java.lang.Object
com.jk.web.util.JKJsfUtil
This class provides utility methods for working with Jakarta Server Faces
 (JSF) in web applications.
 
It includes methods for manipulating UI components, handling messages, managing views, and performing other common tasks related to JSF applications.
- Version:
 - 1.0
 - Author:
 - Dr. Jalal H. Kiswani
 
- 
Constructor Summary
Constructors - 
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddFullRow(jakarta.faces.component.UIComponent comp, int colSpan, String style) This method adds a complete row containing a UIComponent to an HTML table within a JSF page.static voidaddFullRow(String contents, int colSpan, String style) This method adds a complete row to an HTML table within a JSF page.static voidappendAttribute(jakarta.faces.component.UIComponent component, String sourceKey, String targetKey, String valueToAppend) This method appends or adds a new attribute to a UIComponent, with the option to target a different key.static StringThis method builds and renders a JSF view.static StringThis method builds and returns the HTML content of a JSF view given its view ID.static longcalculateChecksum(jakarta.faces.component.UIComponent component) This method calculates a checksum for the provided JSF UIComponent and its children.static longThis method calculates a checksum for the current JSF view's UIComponent tree.static voidThis method clears the view states associated with the current JSF view.static jakarta.el.MethodExpressioncreateMethodExpression(String expression, Class<?> returnType) This method creates a MethodExpression for the given expression and return type.static jakarta.el.ValueExpressioncreateValueException(String value) This method creates a ValueExpression for the given expression string.static jakarta.el.ValueExpressioncreateValueException(String value, Class<?> clas) This method creates a ValueExpression for the given expression string and a specified target class.static jakarta.el.ValueExpressioncreateValueExceptionWithValue(Object originalValue) This method creates a ValueExpression for the given original value.static jakarta.faces.component.UIViewRootcreateView(String viewId) This method creates a new UIViewRoot instance for the specified view ID.static voidThis method adds an error message to the FacesContext with the specified error message text.static ObjectevaluateExpressionToObject(jakarta.el.ValueExpression valueExpression) This method evaluates the given ValueExpression and returns the result as an Object.static ObjectThis method evaluates the given Expression Language expression and returns the result as an Object.static jakarta.faces.component.UIComponentfindComponent(jakarta.faces.component.UIComponent base, String id) This method recursively searches for a UI component with the specified ID within the given base component and its descendants.static jakarta.faces.component.UIComponentThis method searches for a UI component with the specified ID within the root component of the current view.static List<jakarta.faces.component.UIComponent>findComponents(jakarta.faces.component.UIComponent parent, Class type) This method recursively searches for UI components of a specified type within the given parent component and its descendants.static voidforceClearCache(jakarta.servlet.ServletResponse resp) This method forces the HTTP cache to clear for the provided ServletResponse object.static ObjectgetAttribute(jakarta.faces.component.UIComponent component, jakarta.faces.component.StateHelper stateHelper, String key) This method retrieves an attribute from the given UIComponent's state helper or component attributes.static ObjectgetAttribute(jakarta.faces.component.UIComponent component, String key, Object defaultValue) This method retrieves an attribute from the given UIComponent's attributes or returns a default value if the attribute is not found.static booleangetBooleanAttribute(jakarta.faces.component.UIComponent uiComponent, String key, boolean defaultValue) This method retrieves a boolean attribute from the given UIComponent's attributes or returns a default value if the attribute is not found.static ObjectgetComponentAttribute(jakarta.faces.component.UIComponent comp, String attributeName) This method retrieves an attribute associated with a UIComponent.static StringThis method retrieves the current view identifier in a Jakarta Server Faces (JSF) application.static longThis method retrieves the original checksum value associated with the current view in a Jakarta Server Faces (JSF) application.static jakarta.el.ExpressionFactoryThis method retrieves the ExpressionFactory associated with the current Jakarta Server Faces (JSF) application.static jakarta.faces.view.facelets.FaceletContextThis method retrieves the FaceletContext associated with the current Jakarta Server Faces (JSF) application.static intgetIntegerAttribute(jakarta.faces.component.UIComponent component, String key, Object defaultValue) This method retrieves an integer attribute associated with a UIComponent.This method retrieves information about the Jakarta Server Faces (JSF) environment.static StringgetLocalNameFromQName(String qName) This method extracts the local name from a qualified name.static StringThis method extracts the name space letter from a qualified name.static StringThis method retrieves the remote host name or IP address of the client making the current HTTP request.static booleangetRequestAttributeAsBoolean(String key, Object defaultValue) This method retrieves a request attribute by its key and converts it to a boolean value, using a default value if not found.This method retrieves the request map from the current FacesContext.static jakarta.servlet.ServletContextThis method retrieves the ServletContext associated with the current FacesContext.static StringgetServletContextParamter(String param) This method retrieves the value of a specified initialization parameter from the ServletContext.This method retrieves the session map from the current FacesContext.static voidinvalidateSession(String... keepAttributes) This method invalidates the current user session while optionally preserving specified session attributes.static booleanisJSF22()This method checks whether the Jakarta Server Faces (JSF) version being used is 2.2.static voidThis method redirects the current HTTP request to a specified path or URL.static voidredirect(String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method redirects the current HTTP request to a specified path or URL using the provided request and response objects.static voidThis method calculates and saves the checksum of the current JSF view in the session.static voidsetComponentAttribute(jakarta.faces.component.UIComponent comp, String attributeName, Object atributeValue) This method sets a custom attribute on a UIComponent and associates it with the given attribute name.static voidsetRequestAttribute(String key, Object value) This method sets an attribute in the request scope of the current FacesContext.static voidsetSessionAttribute(String key, Object value) This method sets an attribute in the session scope of the current FacesContext.static voidThis method displays a success message using FacesMessage with severity set to INFO.static org.primefaces.model.StreamedContenttoStreamedContents(File file, String fileName, String contentTypes) This method converts a File object into a StreamedContent object for use in streaming file downloads.static UIFacesVisitorvisitComponent(jakarta.faces.component.UIComponent component) This method visits and processes the given UIComponent and its descendants using a UIFacesVisitor.static UIFacesVisitorThis method visits and processes the components within the current JSF view using a UIFacesVisitor.static UIFacesVisitorThis method visits and processes the components within a specific JSF view using a UIFacesVisitor.static voidThis method displays a warning message in the JSF application.static voidwriteAttribue(jakarta.faces.component.UIComponent component, String key, Object defaultValue) This method writes an attribute to the response generated by the JSF component.static voidwriteAttribue(jakarta.faces.component.UIComponent component, String sourceKey, String targetKey, Object defaultValue) This method writes an attribute to the response generated by the JSF component. 
- 
Constructor Details
- 
JKJsfUtil
public JKJsfUtil() 
 - 
 - 
Method Details
- 
addFullRow
This method adds a complete row to an HTML table within a JSF page.- Parameters:
 contents- Specifies the contents of the row.colSpan- Specifies the colspan attribute for the row.style- Specifies the CSS style class for the row.- Throws:
 IOException- if an I/O exception has occurred.
 - 
addFullRow
public static void addFullRow(jakarta.faces.component.UIComponent comp, int colSpan, String style) throws IOException This method adds a complete row containing a UIComponent to an HTML table within a JSF page.- Parameters:
 comp- Specifies the UIComponent to be added to the row.colSpan- Specifies the colspan attribute for the row.style- Specifies CSS style class for the row.- Throws:
 IOException- if an I/O exception has occurred.
 - 
appendAttribute
public static void appendAttribute(jakarta.faces.component.UIComponent component, String sourceKey, String targetKey, String valueToAppend) throws IOException This method appends or adds a new attribute to a UIComponent, with the option to target a different key.- Parameters:
 component- Specifies the UIComponent to which the attribute is added or appended.sourceKey- Specifies the key of the source attribute to read from.targetKey- Specifies the key under which the attribute will be added or appended.valueToAppend- Specifies the value to append to the attribute, separated by a space.- Throws:
 IOException- if an I/O exception has occurred.
 - 
buildView
public static String buildView(jakarta.faces.context.FacesContext context, String viewId) throws IOException This method builds and renders a JSF view.- Parameters:
 context- Specifies the FacesContext for the current request.viewId- Specifies the view identifier for the view to be rendered.- Returns:
 - the rendered view as a string.
 - Throws:
 IOException- if an I/O exception has occurred.
 - 
buildView
This method builds and returns the HTML content of a JSF view given its view ID.- Parameters:
 viewId- Specifies the view ID of the JSF view to build.- Returns:
 - the HTML content of the JSF view as a String.
 - Throws:
 IOException- if an I/O exception has occurred.
 - 
calculateChecksum
public static long calculateChecksum(jakarta.faces.component.UIComponent component) This method calculates a checksum for the provided JSF UIComponent and its children.- Parameters:
 component- Specifies the root UIComponent for which to calculate the checksum.- Returns:
 - the calculated checksum as a long value.
 
 - 
calculateCurrentViewChecksum
public static long calculateCurrentViewChecksum()This method calculates a checksum for the current JSF view's UIComponent tree.- Returns:
 - the calculated checksum as a long value.
 
 - 
clearViewStates
public static void clearViewStates()This method clears the view states associated with the current JSF view. - 
createMethodExpression
public static jakarta.el.MethodExpression createMethodExpression(String expression, Class<?> returnType) This method creates a MethodExpression for the given expression and return type.- Parameters:
 expression- Specifies the method expression string.returnType- Specifies the expected return type of the method.- Returns:
 - the MethodExpression representing the specified expression and return type.
 
 - 
createValueException
This method creates a ValueExpression for the given expression string.- Parameters:
 value- Specifies the expression string.- Returns:
 - a ValueExpression representing the specified expression.
 
 - 
createValueException
This method creates a ValueExpression for the given expression string and a specified target class.- Parameters:
 value- Specifies the expression string.clas- Specifies the target class for the value to be coerced to.- Returns:
 - a ValueExpression representing the specified expression with the specified target class.
 
 - 
createValueExceptionWithValue
This method creates a ValueExpression for the given original value.- Parameters:
 originalValue- Specifies the original value to be contained in a ValueExpression.- Returns:
 - a ValueExpression encapsulating the provided original value.
 
 - 
createView
This method creates a new UIViewRoot instance for the specified view ID.- Parameters:
 viewId- Specifies the view identifier for the view to be created.- Returns:
 - the new UIViewRoot instance representing the specified view.
 
 - 
error
This method adds an error message to the FacesContext with the specified error message text.- Parameters:
 message- Specifies the error message text to be added.
 - 
evaluateExpressionToObject
This method evaluates the given Expression Language expression and returns the result as an Object.- Parameters:
 el- Specifies the expression to be evaluated.- Returns:
 - the result of the expression evaluation as an Object.
 
 - 
evaluateExpressionToObject
This method evaluates the given ValueExpression and returns the result as an Object.- Parameters:
 valueExpression- Specifies the ValueExpression to be evaluated.- Returns:
 - the result of the ValueExpression evaluation as an Object.
 
 - 
getAttribute
public static Object getAttribute(jakarta.faces.component.UIComponent component, jakarta.faces.component.StateHelper stateHelper, String key) This method retrieves an attribute from the given UIComponent's state helper or component attributes.- Parameters:
 component- Specifies the UIComponent from which to retrieve the attribute.stateHelper- Specifies the StateHelper associated with the component.key- Specifies the key of the attribute to retrieve.- Returns:
 - the value of the attribute.
 
 - 
getAttribute
public static Object getAttribute(jakarta.faces.component.UIComponent component, String key, Object defaultValue) This method retrieves an attribute from the given UIComponent's attributes or returns a default value if the attribute is not found.- Parameters:
 component- Specifies the UIComponent from which to retrieve the attribute.key- Specifies the key of the attribute to retrieve.defaultValue- Specifies the default value to return if the attribute is not found.- Returns:
 - the value of the attribute if found, or the default value if not found.
 
 - 
getBooleanAttribute
public static boolean getBooleanAttribute(jakarta.faces.component.UIComponent uiComponent, String key, boolean defaultValue) This method retrieves a boolean attribute from the given UIComponent's attributes or returns a default value if the attribute is not found.- Parameters:
 uiComponent- Specifies the UIComponent from which to retrieve the attribute.key- Specifies the key of the attribute to retrieve.defaultValue- Specifies the default value to return if the attribute is not found.- Returns:
 - the boolean value of the attribute if found, or the default value if not found.
 
 - 
getComponentAttribute
public static Object getComponentAttribute(jakarta.faces.component.UIComponent comp, String attributeName) This method retrieves an attribute associated with a UIComponent.- Parameters:
 comp- Specifies the UIComponent from which to retrieve the attribute.attributeName- Specifies the name of the attribute to retrieve.- Returns:
 - the value of the specified attribute associated with the UIComponent.
 
 - 
getCurrentView
This method retrieves the current view identifier in a Jakarta Server Faces (JSF) application.- Returns:
 - a string representing the identifier of the current view.
 
 - 
getCurrentViewOriginalChecksum
public static long getCurrentViewOriginalChecksum()This method retrieves the original checksum value associated with the current view in a Jakarta Server Faces (JSF) application.- Returns:
 - the original checksum value of the current view.
 
 - 
getExpressionFactory
public static jakarta.el.ExpressionFactory getExpressionFactory()This method retrieves the ExpressionFactory associated with the current Jakarta Server Faces (JSF) application.- Returns:
 - the ExpressionFactory instance or null if it's not available.
 
 - 
getFaceletsContext
public static jakarta.faces.view.facelets.FaceletContext getFaceletsContext()This method retrieves the FaceletContext associated with the current Jakarta Server Faces (JSF) application.- Returns:
 - the FaceletContext instance.
 
 - 
getIntegerAttribute
public static int getIntegerAttribute(jakarta.faces.component.UIComponent component, String key, Object defaultValue) This method retrieves an integer attribute associated with a UIComponent.- Parameters:
 component- Specifies the UIComponent to retrieve the attribute from.key- Specifies the key of the desired attribute.defaultValue- Specifies the default value to return if the attribute is not found.- Returns:
 - the integer value of the attribute or the default value if the attribute is not found.
 
 - 
getJSFInfo
This method retrieves information about the Jakarta Server Faces (JSF) environment.- Returns:
 - a map containing information about the JSF environment, including JSF version, classes, and factories.
 
 - 
getRequestAttributeAsBoolean
This method retrieves a request attribute by its key and converts it to a boolean value, using a default value if not found.- Parameters:
 key- Specifies the key of the request attribute to retrieve.defaultValue- Specifies the default value to return if the attribute is not found.- Returns:
 - the boolean value of the request attribute or the default value if not found.
 
 - 
getRequestMap
This method retrieves the request map from the current FacesContext.- Returns:
 - the request map containing request-specific attributes and values.
 
 - 
getSessionMap
This method retrieves the session map from the current FacesContext.- Returns:
 - the session map containing session-specific attributes and values.
 
 - 
isJSF22
public static boolean isJSF22()This method checks whether the Jakarta Server Faces (JSF) version being used is 2.2.- Returns:
 - true, if the JSF version is 2.2, false otherwise.
 
 - 
saveCurrentViewChecksum
public static void saveCurrentViewChecksum()This method calculates and saves the checksum of the current JSF view in the session. - 
setComponentAttribute
public static void setComponentAttribute(jakarta.faces.component.UIComponent comp, String attributeName, Object atributeValue) This method sets a custom attribute on a UIComponent and associates it with the given attribute name.- Parameters:
 comp- Specifies the UIComponent to which the attribute will be attached.attributeName- Specifies the name of the custom attribute.atributeValue- Specifies the value of the custom attribute to be set.
 - 
setRequestAttribute
This method sets an attribute in the request scope of the current FacesContext.- Parameters:
 key- Specifies the name of the attribute to set.value- Specifies the value of the attribute to set.
 - 
setSessionAttribute
This method sets an attribute in the session scope of the current FacesContext.- Parameters:
 key- Specifies the name of the attribute to set.value- Specifies the value of the attribute to set.
 - 
success
This method displays a success message using FacesMessage with severity set to INFO.- Parameters:
 message- Specifies the message to display as a success message.
 - 
visitComponent
This method visits and processes the given UIComponent and its descendants using a UIFacesVisitor.- Parameters:
 component- Specifies the UIComponent to start the visitation from.- Returns:
 - the UIFacesVisitor containing the results of the visitation.
 
 - 
visitCurrentView
This method visits and processes the components within the current JSF view using a UIFacesVisitor.- Returns:
 - a UIFacesVisitor containing the results of the visitation.
 
 - 
visitView
This method visits and processes the components within a specific JSF view using a UIFacesVisitor.- Parameters:
 viewId- Specifies the identifier of the JSF view to visit.- Returns:
 - a UIFacesVisitor containing the results of the visitation.
 
 - 
warning
This method displays a warning message in the JSF application.- Parameters:
 message- Specifies the warning message to be displayed.
 - 
writeAttribue
public static void writeAttribue(jakarta.faces.component.UIComponent component, String key, Object defaultValue) throws IOException This method writes an attribute to the response generated by the JSF component.- Parameters:
 component- Specifies the JSF UIComponent to which the attribute belongs.key- Specifies the key or name of the attribute.defaultValue- Specifies the default value to be used if the attribute is not found.- Throws:
 IOException- if an I/O exception has occurred.
 - 
writeAttribue
public static void writeAttribue(jakarta.faces.component.UIComponent component, String sourceKey, String targetKey, Object defaultValue) throws IOException This method writes an attribute to the response generated by the JSF component.- Parameters:
 component- Specifies the JSF UIComponent to which the attribute belongs.sourceKey- Specifies the source key.targetKey- Specifies the target key.defaultValue- Specifies the default value to be used if the attribute is not found.- Throws:
 IOException- if an I/O exception has occurred.
 - 
invalidateSession
This method invalidates the current user session while optionally preserving specified session attributes.- Parameters:
 keepAttributes- Specifies the list of session attribute names to preserve during session invalidation.
 - 
getLocalNameFromQName
This method extracts the local name from a qualified name.- Parameters:
 qName- Specifies the qualified name.- Returns:
 - the local name extracted from the QName.
 
 - 
getNamespaceLetterFromQName
This method extracts the name space letter from a qualified name.- Parameters:
 qName- Specifies the qualified name.- Returns:
 - the name space letter extracted from the QName.
 
 - 
findComponentInRoot
This method searches for a UI component with the specified ID within the root component of the current view.- Parameters:
 id- Specifies the ID of the UI component to search for.- Returns:
 - the UI component with the specified ID.
 
 - 
findComponent
public static jakarta.faces.component.UIComponent findComponent(jakarta.faces.component.UIComponent base, String id) This method recursively searches for a UI component with the specified ID within the given base component and its descendants.- Parameters:
 base- Specifies the base UI component to start the search from.id- Specifies the ID of the UI component to search for.- Returns:
 - the UI component with the specified ID.
 
 - 
findComponents
public static List<jakarta.faces.component.UIComponent> findComponents(jakarta.faces.component.UIComponent parent, Class type) This method recursively searches for UI components of a specified type within the given parent component and its descendants.- Parameters:
 parent- Specifies the parent UI component to start the search from.type- Specifies the type of UI components to search for.- Returns:
 - a List of UI components of the specified type found within the parent component and its descendants.
 
 - 
getRemoteHostName
This method retrieves the remote host name or IP address of the client making the current HTTP request.- Returns:
 - the String representing the remote host name.
 
 - 
redirect
This method redirects the current HTTP request to a specified path or URL.- Parameters:
 path- Specifies the relative path or URL to which the request should be redirected.
 - 
redirect
public static void redirect(String path, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) This method redirects the current HTTP request to a specified path or URL using the provided request and response objects.- Parameters:
 path- Specifies the relative path or URL to which the request should be redirected.request- Specifies the current HttpServletRequest object.response- Specifies the current HttpServletResponse object.
 - 
forceClearCache
public static void forceClearCache(jakarta.servlet.ServletResponse resp) This method forces the HTTP cache to clear for the provided ServletResponse object.- Parameters:
 resp- Specifies the ServletResponse object for which the cache should be cleared.
 - 
getServletContext
public static jakarta.servlet.ServletContext getServletContext()This method retrieves the ServletContext associated with the current FacesContext.- Returns:
 - the ServletContext object associated with the current FacesContext.
 
 - 
getServletContextParamter
This method retrieves the value of a specified initialization parameter from the ServletContext.- Parameters:
 param- Specifies the name of the initialization parameter to retrieve.- Returns:
 - the value of the specified initialization parameter.
 
 - 
toStreamedContents
public static org.primefaces.model.StreamedContent toStreamedContents(File file, String fileName, String contentTypes) This method converts a File object into a StreamedContent object for use in streaming file downloads.- Parameters:
 file- Specifies the File object to be streamed.fileName- Specifies the name to be used for the downloaded file.contentTypes- Specifies the content type of the file.- Returns:
 - the StreamedContent.
 
 
 -