My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

luni, 30 martie 2015

[OmniFaces utilities (2.0)] Sets the HTTP response status code


[OmniFaces utilities] The setResponseStatus() method sets the HTTP response status code. You can use the constant field values of HttpServletResponse for this. For example, Faces.setResponseStatus(HttpServletResponse.SC_BAD_REQUEST).

Method:
See also: Faces#getContext()
Usage:

import org.omnifaces.util.Faces;
...
// use HttpServletResponse constants field values
Faces.setResponseStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
// or simply uses the integers codes - http://www.w3.org/Protocols/HTTP/HTRESP.html
Faces.setResponseStatus(501);

[OmniFaces utilities (2.0)] Get the current UI component from the EL context


[OmniFaces utilities] The getCurrentComponent() method returns the current UI component from the EL context.

Method:
Usage:

import org.omnifaces.util.Components;
...
UIComponent currentComponent = Components.getCurrentComponent();

This method return the UIComponent instance that is currently processing. For example, OmniFaces uses this method to obtain the current component that causes a constraint violation in the JsfLabelMessageInterpolator (custom message interpolator):
...
if (hasContext()) {
    label = getLabel(getCurrentComponent());
}
...

duminică, 29 martie 2015

JSF 2.3 new features reported from the inside by Arjan Tijms

JSF 2.3 new features reported from the inside by Arjan Tijms, 6 February 2015:

                Injection and EL resolving ofJSF artifacts
                Injection in more JSF artifacts

                System event published afterview rendered



Expected release date: Q3 2016
Latest 2.3 snapshot: implementation jar, source jar

[OmniFaces utilities (2.0)] Get HTTP servlet response / response buffer size / response character encoding


[OmniFaces utilities] The getResponse() method returns the HTTP servlet response.
[OmniFaces utilities] The getResponseBufferSize() method returns the HTTP response buffer size. If Facelets is used and the javax.faces.FACELETS_BUFFER_SIZE context parameter is been set, then it's the context parameter value which will be returned. Otherwise it returns the implementation independent default value, which is 1024 in Mojarra.
[OmniFaces utilities] The getResponseCharacterEncoding() method returns the HTTP response character encoding.

Method Faces#getResponse() - returns the HTTP servlet response
See also: Faces#getContext()

Method Faces#getResponseBufferSize() - returns the HTTP response buffer size
See also: Faces#getContext()

Method Faces#getResponseCharacterEncoding() - returns the HTTP response character encoding
See also: Faces#getContext()
Usage:

The HTTP servlet response can be obtain via Faces#getResponse(), as below:

import org.omnifaces.util.Faces;
import javax.servlet.http.HttpServletResponse;
...
HttpServletResponse response = Faces.getResponse();
// further, invoke HttpServletResponse methods

The HTTP servlet response buffer size can be obtain via Faces#getResponseBufferSize(), as below:

import org.omnifaces.util.Faces;
...
// e.g. 8192
int bufferSize = Faces.getResponseBufferSize();

Or, you can achieve this via Faces#getResponse(), as:

import org.omnifaces.util.Faces;
import javax.servlet.http.HttpServletResponse;
...
HttpServletResponse response = Faces.getResponse();
int bufferSize = response.getBufferSize();

The HTTP servlet response character encoding can be obtain via Faces#getResponseCharacterEncoding(), as below:

import org.omnifaces.util.Faces;
...
// e.g. ISO-8859-1
String characterEncoding = Faces.getResponseCharacterEncoding();

Or, you can achieve this via Faces#getResponse(), as:
import org.omnifaces.util.Faces;
import javax.servlet.http.HttpServletResponse;
...
HttpServletResponse response = Faces.getResponse();
String characterEncoding = response.getCharacterEncoding();

sâmbătă, 28 martie 2015

[OmniFaces utilities (2.3/2.0)] Get the Internet Protocol (IP) address of the client that sent the request


[OmniFaces utilities] The getRemoteAddr() method returns the Internet Protocol (IP) address of the client that sent the request. This will first check the X-Forwarded-For request header and if it's present, then return its first IP address, else just return HttpServletRequest#getRemoteAddr() unmodified.

Starting with OmniFaces 2.3 this method is available in Servlets utility class also as, Servlets#getRemoteAddr().

Method:

OmniFaces 2.3OmniFaces 2.0
Usage:

import org.omnifaces.util.Faces;
...
// 'remoteAddr' may be '0:0:0:0:0:0:0:1'
String remoteAddr = Faces.getRemoteAddr();

[OmniFaces utililites (2.0)] Get original HTTP request URI (including query string) behind this forwarded request


[OmniFaces utilities (deprecated from OmniFaces 3.0)] The getForwardRequestURI() method returns the original HTTP request URI behind this forwarded request, if any. This does not include the request query string.
[OmniFaces utilities (deprecated from OmniFaces 3.0)] The getForwardRequestQueryString() method returns the original HTTP request query string behind this forwarded request, if any.
[OmniFaces utilities (deprecated from OmniFaces 3.0)] The getForwardRequestURIWithQueryString() method returns the original HTTP request URI with query string behind this forwarded request, if any.

Method Faces#getForwardRequestURI()- returns the original HTTP request URI behind this forwarded request, if any

Method Faces#getForwardRequestQueryString() - returns the original HTTP request query string behind this forwarded request, if any

Method Faces#getForwardRequestURIWithQueryString() - returns the original HTTP request URI with query string behind this forwarded request, if any
Usage:

Let's suppose that we have a simple application named MyApp, and a simple scenario, based on the following items:

·         a JSF page (index.xhtml) that contains a <h:outputLink> that points to a Servlet, named ForwardServlet, and a simple form that contains a command button for navigating from page index.xhtml to a page named home.xhtml (this form is just for better highlighting the use of forward(request,response); effect):
...
<h:body>    
  <h:outputLink value="ForwardServlet?name=Rafael&amp;surname=Nadal">Go</h:outputLink>
       
  <h:form>              
    <h:commandButton value="Go" action="home"/>       
  </h:form>
</h:body>       
...

·         The ForwardServlet, which simply forwards the request/response to the home.xhtml page, via a RequestDispatcher#forward() call, as below:

@WebServlet(name = "ForwardServlet", urlPatterns = {"/ForwardServlet"})
public class ForwardServlet extends HttpServlet {
  ...
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    String nextPage = "/faces/home.xhtml";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextPage);
    dispatcher.forward(request, response);
  }
  ...
}

·         The home.xhtml page, which needs to check if the flow reaches there via a forward (e.g. passed through ForwardServlet) or not (e.g.  <h:form> submit), and for this it uses a simple test, as below:
...
<h:body>              
  <h:panelGroup rendered="#{!empty forwardBean.forward}">#{forwardBean.forward}</h:panelGroup>
  <h:panelGroup rendered="#{empty forwardBean.forward}">This is not a forward</h:panelGroup>
</h:body>
...

The  ForwardBean uses the OmniFaces, Faces#getForwardRequestURI(), which will return null if you navigate to home.xhtml by submitting the form, and will return the forward request URI (without query string) if you navigate to home.xhtml via ForwardServlet:

import org.omnifaces.util.Faces;
...
@Named
@RequestScoped
public class ForwardBean {

  private String forward;      

  public String getForward() {
   forward = Faces.getForwardRequestURI();
   return forward;
  }
}

The value of forward will be /MyApp/ForwardServlet.

If you need to obtain the forward request query string (without forward request URI), then just call Faces#getForwardRequestQueryString():
...
<h:body>              
 <h:panelGroup rendered="#{!empty forwardBean.forward}">#{forwardBean.queryString}</h:panelGroup>
 <h:panelGroup rendered="#{empty forwardBean.forward}">This is not a forward</h:panelGroup>
</h:body>
...

import org.omnifaces.util.Faces;
...
@Named
@RequestScoped
public class ForwardBean {

  private String forward;      

  public String getForward() {
    forward = Faces.getForwardRequestURI();
    return forward;
  }
   
  public String getQueryString(){
    return Faces.getForwardRequestQueryString();
  }
}

The value of queryString will be name=Rafael&surname=Nadal.

If you need both, the forward request URI and the forward request query string then simply invoke Faces#getForwardRequestURIWithQueryString():
...
<h:body>              
  <h:panelGroup rendered="#{!empty forwardBean.forward}">#{forwardBean.forwardAndQueryString}</h:panelGroup>
  <h:panelGroup rendered="#{empty forwardBean.forward}">This is not a forward</h:panelGroup>
</h:body>
...

import org.omnifaces.util.Faces;
...
@Named
@RequestScoped
public class ForwardBean {

  private String forward;      

  public String getForward() {
    forward = Faces.getForwardRequestURI();
    return forward;
  }
   
  public String getQueryString(){       
    return Faces.getForwardRequestQueryString();
  }
   
  public String getForwardAndQueryString(){
    forward = Faces.getForwardRequestURIWithQueryString();
    return forward;
  }
}

The value of forwardAndQueryString will be /MyApp/ForwardServlet?name=Rafael&surname=Nadal

joi, 26 martie 2015

[OmniFaces utilities (2.0)] Get HTTP request query string as String and Map / URL with query string / URI with query string


[OmniFaces utilities] The getRequestQueryString() method returns the HTTP request query string. This is the part after the ? in the request URL as the enduser sees in browser address bar.
[OmniFaces utilities] The getRequestQueryStringMap() method returns the HTTP request query string as parameter values map. Note this method returns only the request URL (GET) parameters, as opposed to getRequestParameterValuesMap(), which contains both the request URL (GET) parameters and and the request body (POST) parameters. This is ready for usage in among others ViewHandler#getBookmarkableURL(FacesContext, String, Map, boolean).
[OmniFaces utilities] The getRequestURLWithQueryString() method returns the HTTP request URL with query string. This is the full request URL with query string as the enduser sees in browser address bar.
[OmniFaces utilities] The getRequestURIWithQueryString() method returns the HTTP request URI with query string. This is the part after the domain in the request URL, including the leading slash and the request query string.

MethodFaces#getRequestQueryString() return HTTP request query string

MethodFaces#getRequestQueryStringMap() return HTTP request query string as parameter values map

MethodFaces#getRequestURLWithQueryString() return HTTP request URL with query string

MethodFaces#getRequestURIWithQueryString() return HTTP request URI with query string
Usage:

Let's suppose the following JSF-HTTP request (faces servlet mapping is a prefix mapping, /faces/*):

http://localhost:8080/MyApp/faces/index.xhtml?name=Rafael&surname=Nadal&age=27&rank=1

Further, let's apply the above methods, one by one, as follows:

·         get the HTTP request query string (Faces#getRequestQueryString()) - if query string is missing, returns null
import org.omnifaces.util.Faces;
...
// returns name=Rafael&surname=Nadal&age=27&rank=1
String requestQueryString = Faces.getRequestQueryString();

·         get the HTTP request query string as parameter values map (Faces#getRequestQueryStringMap())
import org.omnifaces.util.Faces;
...
// prints  name#[Rafael], surname#[Nadal], age#[27], rank#[1]
Map<String, List<String>> requestQueryStringMap = Faces.getRequestQueryStringMap();
for (Map.Entry<String, List<String>> entry : requestQueryStringMap.entrySet()) {
     System.out.println(entry.getKey() + "#" + entry.getValue());
     // do something with request parameters
}

·         get the HTTP request URL with query string (Faces#getRequestURLWithQueryString())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/faces/index.xhtml?name=Rafael&surname=Nadal&age=27&rank=1
String requestURLWithQueryString = Faces.getRequestURLWithQueryString();

·         get the HTTP request URI with query string (Faces#getRequestURIWithQueryString())
import org.omnifaces.util.Faces;
...
// returns /MyApp/faces/index.xhtml?name=Rafael&surname=Nadal&age=27&rank=1
String requestURIWithQueryString = Faces.getRequestURIWithQueryString();

In the below figure, you can see these four methods and what they return:

Let's suppose the following JSF-HTTP request (faces servlet mapping is a suffix mapping, *.xhtml):

http://localhost:8080/MyApp/index.xhtml?name=Rafael&surname=Nadal&age=27&rank=1

Further, let's apply the above methods, one by one, as follows:

·         get the HTTP request query string (Faces#getRequestQueryString()) - if query string is missing, returns null
import org.omnifaces.util.Faces;
...
// returns name=Rafael&surname=Nadal&age=27&rank=1
String requestQueryString = Faces.getRequestQueryString();

·         get the HTTP request query string as parameter values map (Faces#getRequestQueryStringMap())
import org.omnifaces.util.Faces;
...
// prints  name#[Rafael], surname#[Nadal], age#[27], rank#[1]
Map<String, List<String>> requestQueryStringMap = Faces.getRequestQueryStringMap();
for (Map.Entry<String, List<String>> entry : requestQueryStringMap.entrySet()) {
     System.out.println(entry.getKey() + "#" + entry.getValue());
     // do something with request parameters
}

·         get the HTTP request URL with query string (Faces#getRequestURLWithQueryString())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/index.xhtml?name=Rafael&surname=Nadal&age=27&rank=1
String requestURLWithQueryString = Faces.getRequestURLWithQueryString();

·         get the HTTP request URI with query string (Faces#getRequestURIWithQueryString())
import org.omnifaces.util.Faces;
...
// returns /MyApp/index.xhtml?name=Rafael&surname=Nadal&age=27&rank=1
String requestURIWithQueryString = Faces.getRequestURIWithQueryString();

In the below figure, you can see these four methods and what they return:

[OmniFaces utilities (2.0)] Get HTTP request URL / URI / domain URL / base URL


[OmniFaces utilities] The getRequestDomainURL() method returns the HTTP request domain URL. This is the URL with the scheme and domain, without any trailing slash.
[OmniFaces utilities] The getRequestBaseURL() method returns the HTTP request base URL. This is the URL from the scheme, domain until with context path, including the trailing slash. This is the value you could use in HTML <base> tag.
[OmniFaces utilities] The getRequestURL() method returns the HTTP request URL. This is the full request URL as the enduser sees in browser address bar. This does not include the request query string.
[OmniFaces utilities] The getRequestURI() method returns the HTTP request URI. This is the part after the domain in the request URL, including the leading slash. This does not include the request query string.

MethodFaces#getRequestDomainURL() returns the HTTP request domain URL

Method Faces#getRequestBaseURL() returns the HTTP request base URL

MethodFaces#getRequestURL() returns the HTTP request URL

Method Faces#getRequestURI() returns the HTTP request URI

Usage:

Let's suppose the following HTTP request (faces servlet mapping is a prefix mapping, /faces/*):

http://localhost:8080/MyApp/faces/index.xhtml

Further, let's apply the above methods, one by one, as follows:

·         get the HTTP request domain URL (Faces#getRequestDomainURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080
String requestDomainURL = Faces.getRequestDomainURL();

·         get the HTTP request base URL (Faces#getRequestBaseURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/
String requestBaseURL = Faces.getRequestBaseURL();

·         get the HTTP request URL (Faces#getRequestURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/faces/index.xhtml
String requestURL = Faces.getRequestURL();

·         get the HTTP request URI (Faces#getRequestURI())
import org.omnifaces.util.Faces;
...
// returns /MyApp/faces/index.xhtml
String requestURI = Faces.getRequestURI();

In the below figure, you can see these four methods and what they return:


Let's suppose the following HTTP request (faces servlet mapping is a suffix mapping, *.xhtml):

http://localhost:8080/MyApp/index.xhtml

Further, let's apply the above methods, one by one, as follows:

·         get the HTTP request domain URL (Faces#getRequestDomainURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080
String requestDomainURL = Faces.getRequestDomainURL();

·         get the HTTP request base URL (Faces#getRequestBaseURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/
String requestBaseURL = Faces.getRequestBaseURL();

·         get the HTTP request URL (Faces#getRequestURL())
import org.omnifaces.util.Faces;
...
// returns http://localhost:8080/MyApp/index.xhtml
String requestURL = Faces.getRequestURL();

·         get the HTTP request URI (Faces#getRequestURI())
import org.omnifaces.util.Faces;
...
// returns /MyApp/index.xhtml
String requestURI = Faces.getRequestURI();

In the below figure, you can see these four methods and what they return:

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors