How to get parameters from the URL in JSF properly? [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 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']}" />

Related

PrimeFaces dataTable method called many times [duplicate]

This question already has answers here:
Defining and reusing an EL variable in JSF page
(2 answers)
Closed 6 years ago.
I have a datatable with a column:
<p:dataTable value="#{cc.attrs.bean.model}"
...
<p:column style="width:#{bean.getWidth('colDate', 55)}px;"
It seems that the bean.getWidth method is called for every row in the table. Thus, when having 100 rows, the method is called a hundred times. I expected the method to be called only once.
Am I wrong?
No, it's right.
You can cache the value into your bean or use JSTL
<c:set var="width" value="#{bean.getWidth('colDate', 55)}" />
<p:dataTable value="#{cc.attrs.bean.model}"
...
<p:column style="width:#{width}px;"
You can find more infos here https://stackoverflow.com/tags/jstl/info.
Don't forget the namespace .
you can save result of getWidth in managed bean attribute then use this in column style

How does #{ManagedBean.all} call ManagedBean.getAll() [duplicate]

This question already has answers here:
How does EL #{bean.id} call managed bean method bean.getId()
(2 answers)
Closed 6 years ago.
I'm not understanding how <h:dataTable value="#{ManagedBean.all}" /h:dataTable> results in a function within the ManagedBean class getAll() being called.
I understand that <h:form binding="#{ManagedBean.form}" /h:form> calls the constructor of the ManagedBean but not sure how the example above calls a seeming unrelated function.
As per JSF life cycle, during phase 6 i.e Render response.
"The values to be shown are retrieved from the value binding getters in the backing bean. Also If a converter is definied, then the value will be passed through the converter getAsString() method and the result will be shown in the form." . Hence to collection to render in datatable with value="#{ManagedBean.all}", the getAll() method is invoked by JSF.
You can refer below link for more info:
Debug JSF lifecycle

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 get GET parameters with JSF2? [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 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}

Resources