2

I am trying to get parameters from an URL in my JSF application. The URL as following: http://localhost:8088/*******/pages/rh/evaluation/layouts/evaluationLayout.jsf?crt_affectId=RqI+dgtx6q8=

So here the parameter is crt_affectId and its value is RqI+dgtx6q8=. When I try to get the value by using the following code:

String cryptedValue = FacesContext.getCurrentInstance().getExternalContext()
    .getRequestParameterValuesMap().get(cryptedKey)[0];

I get the wanted value but missing the character + as it is replaced by a blank space as . the result value is as following: RqI dgtx6q8=.

Is there a solution in JSF to get the complete value or a should I just replace the blank space by the "+"? However this latter is not that generic and good way neither.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Daamix
  • 35
  • 4
  • Your URL should be encoded in *UTF-8*. Perhaps you could first [set your character encoding](http://docs.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html#setRequestCharacterEncoding(java.lang.String)) and then retrieve the value you want. Refer to [this](http://stackoverflow.com/a/10786112/1346996) and [this](http://stackoverflow.com/a/5330239/1346996) answers. – António Ribeiro Feb 29 '16 at 17:30

1 Answers1

3

The + is URL-encoded representation of the space. So the behavior is correct. Those parameters should have been URL-encoded beforehand.

If you have full control over those parameters, then you can URL-encode the individual parameter values as below before passing it to e.g. ExternalContext#redirect().

String url = "/evaluationLayout.jsf?crt_affectId=" + URLEncoder.encode("RqI+dgtx6q8=", "UTF-8");
ec.redirect(ec.getRequestContextPath() + url);

In case you happen to use JSF utility library OmniFaces, it can be done more conveniently as its Faces#redirect() utility already transparently takes care of this.

Faces.redirect("/evaluationLayout.jsf?crt_affectId=%s", "RqI+dgtx6q8=");

See also:

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