I have a template in JSF, this template have a button whose action must be different for each page that will use this template.
I wonder if there is any way to set only a part of that action.
Eg
In the template, I have the following attribute on the button:
disabled = "#{managedBeanTemplate.EditMode}"
However, for each page that will use this template, it should replace ONLY the part where it says managedBeanTemplate, thus:
disabled = "#{managedBeanProduct.EditMode}"
disabled = "#{managedBeanSales.EditMode}"
Is there any way to do this?
I know there is the ui:param to replace parts of the xhtml, but i can't nest in the template using something like this:
disabled = "#{#{managedBeanName}.EditMode}"
in the template, and then
<ui:param name="managedBeanName" value="managedBeanProduct"/>
in the page that uses the template.
I found the answer here:
facelets: passing bean name with ui:param to action attribute
i can just do something like this in the template:
disabled = "#{managedBeanName['EditMode']}"
then, i can simply set the "managedBeanName" through the ui:param
you would be better served having method that takes a parameter. by having the template construct the back references to the server side you are creating a maintainance footprint where it should not be. replace the references with a method that abstracts away the logic of whether the component is disabled/rendered/etc.
ultimately the UX should not care "why", just whether it has to render a compoenent a certain way.
Related
I am working on something like this:
A JSF template has a side-navigation bar which contains links(either anchor or h:outputLink), and there are cases where two options lead to the same link(page), but with a different value in view parameter, and thus rendering different data being displayed on the page.
Is there a way to do this? Using commandLink or commandButton does not seem like an option to me since it will mess up the styling.
Thanks in advance.
An output link is just a normal HTML link, so a conventional way to do this is with a query parameter, e.g. /contentarea.xhtml?myparam=value.
I don't think you should bind a method to the output link. That would involve a Javascript onclick handler (commandLink), and I don't think that's necessary here. That said, I'm surprised you say commandLink messes with the styling, as it renders a normal HTML link.
See also
http://incepttechnologies.blogspot.ca/p/view-parameters-in-jsf-20.html (see the first technique using f:viewParam)
http://www.mkyong.com/jsf2/jsf-2-link-commandlink-and-outputlink-example/
I have got some interesting issue for you. I am creating a composite component. I know, that I can use ui:param for storing value and reuse it. But what if I store to this variable some relative value (#{component.namingContainer.clientId}) and want to reuse it as a constant? It is difficult to explain - here you have my code:
<ui:param name="rdfaComp" value="#{component.namingContainer.clientId}"/>
This is in the beginning of the page - I want to store exactly this ID and then use nothing else by this variable. But later if I reuse it inside elements with own IDs, it is bad. It is interesting, what happens - JSF doesn't take the same value, but a relative one. It reads once more #{component.namingContainer.clientId}, not the fixed value.
How to solve this, could you help me, please? THanks a lot.
UPDATED
There is still one important condition: the variable rdfaComp have to be available immediately, because I reuse it in a Javascript function (on the same page). Like this:
<h:commandButton onclick="return selectText('#{rdfaComp}:editor', ...
I am afraid, it is necessarry to use relative ID chains like this: #{component.namingContainer.parent.namingContainer.parent.clientId} etd.
But it is really awful. Is there another solution?
The <ui:param> indeed merely creates an alias and the EL expression is still deferred and evaluated on every access in the very same context as where it's been referenced (and thus not where it's been declared! that explains your concrete problem). Besides, the <ui:param> is designed to be used on <ui:include>, <ui:composition> and <ui:decorate> only.
Use <c:set> instead. It's capable of immediately evaluating an EL expression and storing its result in either request, view, session or application scope via the scope attribute.
<c:set var="rdfaComp" value="#{component.namingContainer.clientId}" scope="request" />
Update: you actually wanted to get the client ID of the composite component itself, here's how you can get it:
<c:set var="rdfaComp" value="#{cc.clientId}" scope="request" />
It is solved - and I must apologise once more :-). I didn't describe correctly the issue - what I really wanted to do. I just wanted to work with the full client ID of the composite component. Finally I found a solution: #{cc.namingContainer.clientId}
Thanks anyway, your answers were very helpful!
Is it possible to set current displayed tab programmatically in <p:wizard>?
For example, I want that for two different request to the same page which contains a wizard, to have a different tab selected.
What I am currently trying to do, is to have a wizard with many tabs, in the second tab I have a redirection to another page, so when I come back I want to come to the last step which caused the redirection.
Can you please help me ? Thank you a lot !
According to primefaces documentation there's a step attribute for p:wizard tag, which specifies the step of the wizard you're currently in.
attribute: step
default value: 0
type: String
description: Id of the current step in flow
You must bind this attribute to a value of your backing bean and maintain it during redirection and coming back. If your wizard's bean is #ViewScoped you'll loose that info during redirection step, so you have to pass it using a view param or flash scope.
My answer would most probably not meet your complete requirements, but, nonetheless, it may point you towards solution to your problem.
As far as I know, the PrimeFaces Wizard UIComponent is designed for a workflow of one page. That effectively means that inputs will be handled by a backing beans that is in a view scope.
This way, making a redirection on a certain step will clear all data inputs, because your view changes and the old one is destroyed.
Anyway, a means of setting a current tab for display is step attribute of Wizard component. So,
<p:wizard step="#{wizardBean.currentStep}" >...</p:wizard>
will force the wizard to show you step which you specified in your bean. You may be able to get it by using, for example, a view parameter, like in
<f:viewParam name="step" value="#{wizardBean.currentStep}" />
But it will make sense if lifetime of your bean is more that for a view, for example, the bean could be put in session scope.
That said, maybe it is a better idea to do login beforehand. Or, if it is absolutely necessary to do it in step 2 of your wizard, provide for a built-in login functionality in a page itself, or in a popular window?
Also, programmatically the setting you speak of can be achieved via a binding of component to your backing bean and setting the step value in the backing bean, for example, in a preRenderView event.
Is there any chance to get the html content (mime) from a rich text component without any datasource. I would like to grab the content from the field like this. getComponent("FieldName").value but this dosen't work.
thanks.
You can bind the control to a scoped variable; for example, #{viewScope.comments}. You can then retrieve the submitted value from the scope instead of from the component itself; for example, viewScope.get("comments").
Alternatively, you can set a dataContext variable to a JS expression, e.g. <dataContext var="richText" value="#{javascript:return {value:""};}" />. Then you can bind the control to #{richText.value} and retrieve it via the same expression.
And, of course, you could define a managed bean and bind the control to one of its properties. This option provides the most flexibility, but isn't quite as simple as the other two options above.
The solution for my problem is
getComponent("FieldName").getValue()
thanks for your help.
I wrote two pages...one a form where data submitted and second just to confirm the transaction actually carried out some calculation.
I have a managed bean i.e. FormDataBean and a class Reservation.java from which i instantiate for each booking made. Now I have at the end of a form a submit button:
<h:commandButton value="Submit" action="confirmation"/>
in the bean I have setters and getters as usual. in a method i defined I create an instance of Reservation, then set the beans variables to the instance variabels, like
reservation.startDate = startDate;
reservation.endDate = endDate;
reservation.checkRange();
The last method, i.e. checkRange() will use the assigned values to instance variables to carry calculation. it should return a string successful or failure.
Now when I enter data in the form, and press submit, it just refreshes the page but nothing is submitted. because it doesn't go to next page :(
Any idea what is happening? I don't need to define a navigation rule, because in other project, I carry out simple calculation and display result in next page and it worsks! Please advice
Thanks,
Your are missing to tell us some of the more important details so the answer is a kind of guesswork.
As you don't use navigation rules I assume you are using JSF 2, aren't you?
With JSF 2 you can directly set the new navigation target, without navigation rules. A forward to "confirmation" should work if your outcome file is named confirmation.xhtml. Check that. With a navigation rule you could forward it do a different file.
This part should work regardless of the rest.
For the bean not getting any values make sure that you are using the correct scope either through annotation or entry in your faces-config.xml. As you have a quite unusal validation mechanism you probably have to use the session scope.
The correct way would be using an actionlistener that does your checks and then sets the navigation depending on your checks. The bean scope could be more restrictive then.
Did you try action="confirmation?faces-redirect=true"?