How get GET parameters with JSF2? [duplicate] - jsf

This question already has answers here:
How do I process GET query string URL parameters in backing bean on page load?
(4 answers)
Closed 7 years ago.
I have this url for example:
http://example.com?parameter=content
When the user click in this link, then I should be able to get the parameter value which is 'content'.
I was reading BalusC tutorial but is JSF 1.2 and I'm learning with JSF 2.
How could I do that?

Two ways (both examples assume that the parameter name is parameter as in your question):
Use #ManagedProperty on the desired bean property:
#ManagedProperty("#{param.parameter}")
private String parameter;
This works on request scoped beans only and does not allow for fine grained conversion and validation.
Use <f:viewParam> in the view pointing to the desired bean property:
<f:metadata>
<f:viewParam name="parameter" value="#{bean.parameter}" />
</f:metadata>
This works on view scoped beans as well and allows for fine grained conversion and validation using the standard validators like as on normal input components. It even allows for attaching a <h:message>.
See also:
Communication in JSF 2.0 - Processing GET request parameters
ViewParam vs #ManagedProperty(value = "#{param.id}")

Use tag "<f:view ( View Parameters ) to bind to bean and get the parameter
Also u can inject the parameters using #{param.content}

Related

Is there anyway to pass an URL parameter created in Java to an output in JSF? [duplicate]

I've read how to send parameters using JSF but what if the user types their companyId in the URL when accessing their login page? For example,
http://my.company.url/productName/login.faces?companyId=acme.
The way we do it now, there is a bit of scriptlet code that grabs the value from the request and then set it in the session. That parameter changes their look and feel starting from the login page forward so each customer could have a different login page view. We are using extjs until I switch over to JSF.
Is there a way to do that using JSF 2 or perhaps PrimeFaces?
Yes, you can use the <f:viewParam> to set a request parameter as a managed bean property.
<f:metadata>
<f:viewParam name="companyId" value="#{bean.companyId}" />
</f:metadata>
You can if necessary invoke a bean action using <f:viewAction> (JSF 2.2+ only) or <f:event type="preRenderView">.
<f:metadata>
<f:viewParam name="companyId" value="#{bean.companyId}" />
<f:viewAction action="#{bean.onload}" />
</f:metadata>
When using <f:viewAction> you can even return a navigation outcome.
public String onload() {
// ...
return "somepage";
}
When not on JSF 2.2 yet, you can use ExternalContext#redirect() for that. See also among others How to perform navigation in preRenderView listener method.
Note that this is not specific to PrimeFaces. It's just part of standard JSF. PrimeFaces is merely a component library which provides enhanced ajax and skinnability support.
See also:
What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
Communication in JSF 2.0 - Processing GET request parameters
#ManagedProperty with request parameter not set in a #Named bean
url paramters can also be treated as request parameters so you can also access through
FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
There is a utility library, OmniFaces which does this out of the box.
#Inject #Param
private String key;
#Inject #Param
private Long id;
You can use the request.getQueryString() if you want to get full query parameter string.

Call same bean method from multiple onclick with different parameters not working [duplicate]

This question already has an answer here:
Multiple action listeners with a single command component in JSF
(1 answer)
Closed 6 years ago.
I'm using Primefaces and jsf in my application. I'm using mutiple commandlink with onclick function. All the onclick function call the same bean method but parameters are different. Pages are working fine & passing parameters also different even though all the pages are showing same metrics.
Based on parameter should display different metrics but not working properly.
XHTML:
<h:commandLink value="Defects"
action="#{loginbean.setCurrPage('metrics','defects.xhtml')}"
onclick="#{reportsBean.MetricPage('defect')}"
rendered="#{sub_selected =='defects'}" />
<h:commandLink value="Test Case"
action="#{loginbean.setCurrPage('metrics','testcase.xhtml')}"
onclick="#{reportsBean.MetricPage('testcase')}"
rendered="#{sub_selected =='testcases'}" />
Bean:
public void MetricPage(String ipageid) {
metricssection = metricsProcess.getMetricsProcess().getUserMetrics(ipageid, SessionMgr.getProj());
metricsfeature = metricsProcess.getMetricsProcess().getMetricFeatures(ipageid, SessionMgr.getPro());
}
How can i resove the problem?
onclick is a client side event. You can only call Javascript functions from onlcick. Not methods from Managedbean.
Since you tagged this question as Primefaces, I assume you are using Primefaces. In that case you can use p:remoteCommand
See this on how to use p:remotecommand

validator for input text using the backing bean method not working [duplicate]

This question already has answers here:
JSF custom validator not invoked when submitted input value is null
(2 answers)
Closed 5 years ago.
I have a form with h:inputText and have the attribute validator with the backing bean method name as value, example:
h:inputText id="poolmanagementinput" validator="#{poolManager.validatePoolManagement}"
Now I have method in backing bean named poolManager in following format:
public void validatePoolManagement(final FacesContext context, final UIComponent component,
final Object value) {
... }
I expected this method to be called in the validation phase in JSF cycle. But to my surprise this method is not being called and validation is not happening. Could anyone point out any missing point or direct me to a soultion.
Note: Just as a side note the input is placed inside a composite:implementation.
Thanks in advance.
Cheers!
Incase if you have forgot add these attributes between body and form like this
<h:body>
<h:form>
<your ui item>
</h:form>
</h:body>
sorround your UI attribute with them. <h:form> has to be parent tag for all JSF UI components that can participate in HTML form submission.
I have encountered same issue and this has resolved issue.
I know it is a old question but answering it for the people who have the same problem.
JSF is not executing the validators if the value is blank.
You need to use required=true for validation blank.
for more reference JSF custom validator not invoked when submitted input value is null

How to get parameters from the URL in JSF properly? [duplicate]

This question already has answers here:
How do I process GET query string URL parameters in backing bean on page load?
(4 answers)
Closed 7 years ago.
I believe there are 2 ways to get the parameters from the URL in JSF.
One being in the bean:
Map<String, String> params =FacesContext.getCurrentInstance().
getExternalContext().getRequestParameterMap();
String parameterOne = params.get("parameterOne");
and the other one being in the facelets page
<f:metadata>
<f:viewParam name="parameterOne" value="#{bean.parameterOne}"/>
</f:metadata>
Obviously the latter one will require a field in the class and getter / setter for it.
Besides that, what are the differences between these 2 different approaches? Which one should be preferred?
I think this may help:
Get Request and Session Parameters and Attributes from JSF pages
In fact, there should be a query:
<h:outputText value="#{param['id']}" />

How can I set a bean property when the page is loading in jsf?

In my project, I am using myfaces 1.2, rich faces 3.3.3 and spring 2.5 for backing beans. I searched over Internet for this simple need all weekend but I couldn't managed to set the bean property. h:inputhidden trick is good for calling a bean function. But I couldn't use this to set the property. For instance I want to set a bean property named as "number" to "1" when a page is loading.
Over internet I saw these was being used to set the bean property. I am new in web programming and I don't know how these working.. But maybe these make you remember something.
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
request.getParameter()
<h:inputhidden>
Updated:
I want to set the value from the page not in bean. so I must get the value from the page. I will set the bean property differently in different pages. and I am using one bean for multiple page.
You can use:
<f:view before="#{bean.beforePhaseMethod}"> (if using facelets it's called beforePhase)
a #PostConstruct method for request-scoped beans
if there is no logic, simply give an initial value of the field private int foo = 1

Resources