Can I have multiple tags in a Jsf2 xhtml file?
In that case in what order will the associated listeners be called?
Mojarra 2.1.1 / Apache Tomcat 7.0.22 / PrimeFaces 3.4
Yes you can have multiple <f:event> tags in a xhtml page.The execution order really depends on the type of event you define in the <f:event> tag.Check here for more types .So, far I have worked with type=preRenderView which renders in the sequentail order if you have multiple events
Ex:
<f:event listener="#{bean.method1}" type="preRenderView"> // executes first
<f:event listener="#{bean.method2}" type="preRenderView"> // execues after above tag
There are other events which type attribute in the <f:event> tag takes . Go through this post to learn more about event types :
Related
I'm using facelets (JSF2.1) and I'm trying to do something like:
<c:forEach var="domainObject" items="#{MB.listOfDomainObjects}">
<sec:authorize access="hasPermission(#domainObject,'PERMISSION_X')">
hello world
</sec:authorize>
</c:forEach>
I've tried changing #domainObject by #domainObject, #{domainObject}, $domainObject and a lot of other combinations with the same result: domainObject is not processed correctly. Sometimes I get an error saying the page cannot be constructed, others domainObject is null.
It's like the scope where the tag c:forEach puts the variable domainObject is not scanned by sec:authorize to find it.
I've tried also to force the use of a scope using the tag <c:set ... scope="view"/>. I've also tried to use <ui:repeat> with the same results, but considering the tag sec:authorize is a jstl one, I suppose is executed in build time (like c:forEach) and not in render time (like ui:repeat), so I think I must use c:foreach and not ui:repeat.
Any chance to solve my problem?
in my JavaEE-Application, I am using Apache Shiro[1] for user-authentication.
My users are navigating via GET-URLs, as for example "/company/index.xhtml?companyId=327".
I have enabled programmatic login, following a guide[2] from BalusC:
SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(Faces.getRequest());
My problem is, that savedRequest.getRequestUrl() does not contain the previous mentioned GET-parameteres, when my case is asynchronous POST with or without RememberMe; just "/company/index.xhtml" is returned, for example. It seems as if "FacesAjaxAwareUserFilter" (see [2]) is not GET-params aware. Everything works fine on synchronous GET-calls.
How do I get the GET-parameters after an shiro-redirect because of authentication-needed in case of using "FacesAjaxAwareUserFilter"?
[1] https://shiro.apache.org/
[2] Followed this great article about JavaEE and Shiro: http://balusc.blogspot.de/2013/01/apache-shiro-is-it-ready-for-java-ee-6.html
JSF ajax requests are sent to the URL as generated by <h:form>. This is however by default not exactly the current URL including the query string, it's by default the current URI without the query string.
There are several ways to fix this. The simplest but ugliest way is to use JS:
<h:form id="foo">
...
</h:form>
<script>document.getElementById("foo").action += "?" + location.search;</script>
The cleanest way would be to create a custom ViewHandler whose getActionURL() (as used by <h:form>) will return the desired URL with the query string.
JSF utility library OmniFaces has already such a component which does that based on view parameters: the <o:form> which basically extends the <h:form> with support for includeViewParams="true" (exactly the same way as <h:link> does).
<f:metadata>
<f:viewParam name="companyId" value="#{bean.company}" />
</f:metadata>
...
<o:form includeViewParams="true">
...
</o:form>
What is the execution order of those?
Here is a question about possible f:event event names: List of JSF 2 events?
preRenderComponent
preRenderView
postAddToView
preValidate
postValidate
I want to check if a User is saved in a session bean is logged in and if not redirect to the login site, which needs to occur before view-param conversion phase since the used converter depends on the logged in User. 'preValidate' seems to take place after conversion and so I need an earlier event.
<f:event type="preRenderView" listener="#{beanA.checkLoggedIn()}"/>
<f:viewParam name="param" value="#{beanB.param}" converter="#{beanB.converter}" required="true"/>
I could have put 'checkLoggedIn()' in 'beanB' too, but tried to use a separate request scoped bean just for the check so that I could reuse it easily.
What is the execution order of those?
postAddToView runs right after the component is added to view during view build time (which is usually during restore view phase, but can also be during render response phase, e.g. navigation).
preValidate runs right before the component is to be validated (which is usually during validations phase, but can also be apply request values phase if immediate="true").
postValidate runs right after the component is been validated (which is usually during validations phase, but can also be apply request values phase if immediate="true").
preRenderView runs right before the view is rendered during render response phase.
preRenderComponent runs right before the component is rendered during render response phase.
Click the links to see detailed description in javadoc introduction.
I want to check if a User is saved in a session bean is logged in and if not redirect to the login site, which needs to occur before view-param conversion phase since the used converter depends on the logged in User. 'preValidate' seems to take place after conversion and so I need an earlier event.
You should use a simple servlet filter for this, not a JSF event. I've posted several examples before:
Is there any easy way to preprocess and redirect GET requests?
Are there some issue at inserting some check into template?
I will be having a data table where multiple selections (check boxes) can be done. I should fire a single AJAX request in return I should be able to update the status of each selection (rows) in multiple AJAX responses.
How can I achieve this in JSF 2.0? I have reviewed PrimeFaces, ICEFaces and RichFaces, but I couldn't come to a conclusion of which one to use to accomplish this requirement.
I'm new JSF 2.0, any help will be appreciated.
If you know ids of all the fields need to be update then this can be done like this
List<String> renderList = (List<String>)
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds();
renderList.add(clientId1);
renderList.add(clientId2);
renderList.add(clientId3);
renderList.add(clientId4);
here client id is compbination of id and form id eg:
<h:form id="myBean">
<h:inputText id="textBox" />
</h:form>
then client id will be : myBean:textBox
then all the fields you are adding in re-renderlist will be rerender in single ajax request
I have a form that contains a Part Number inputText field and some other fields that represent the attributes of a part. If the user enters an existing part number, I want to populate the other fields with the part's existing attributes. The operator then has the option to change Part Number to something else (essentially creating a new part using the existing part as a template) and/or modify its attributes. I have an AJAX event defined to update the model when Part Number changes, so I can retrieve the attributes from the database:
<f:ajax event="valueChange" execute="#this" render="partlength" />
My question is this: How does the Part Number setter (e.g. setPartNumber() ) know whether it is being invoked as part of an AJAX event, in which case I want to fetch the attributes, or as part of the Update Model Values phase of the form being posted, in which case I don't? Or is there a better way to accomplish what I'm trying to do?
You can determine by PartialViewContext#isAjaxRequest() whether the current request is an ajax request or not. You can obtain the PartialViewContext by FacesContext#getPartialViewContext().
if (FacesContext.getCurrentInstance().getPartialViewContext().isAjaxRequest()) {
// The current request is an ajax request.
}
An alternative is to just do the job in the listener method of <f:ajax> instead of in the getter/setter (doing business job in getters/setters is a poor practice anyway):
<f:ajax listener="#{bean.listener}" render="partlength" />
(note that I omitted the event and execute attributes as you've used the default values already)