Use of OGNL expression to access request headers in Tiles 2 - tiles

I'm using Tiles 2.2.2 in my application (Struts 2.2.3). I want to use expressions in order to modify my screen composition depending on some attribute in the request. Basically, I would like to change the extends of a screen depending if there is a header in the request with name "x-requested-with" and the value is "XMLHttpRequest". Is it possible to do that? I've tried to do a simple example with an attribute:
<put-attribute name="test" expression="OGNL:requestScope" cascade="true"/>
I've tried different expressions like OGNL:%{#request.headers.referer}, OGNL:#request.headers.referer, OGNL:requestScope.headers.referer , etc. But it always returns null. I haven't found any documentation on how the OGNL expressions works on Tiles, so I'm working based on how I would do it with Struts. But it doesn't seem to work.
Any ideas?

How are you initializing Tiles? If you're using the StrutsTilesListener, OGNL expressions in tiles.xml won't be evaluated.
In my Struts2 app, I am using the org.apache.tiles.extras.complete.CompleteAutoloadTilesListener in web.xml and OGNL evaluation is working:
<listener>
<listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
</listener>
In my case, I have a custom TilesResult with a property called 'content':
<put-attribute name="content" expression="OGNL:content" />

Related

Use of spring security jstl tag <sec:authority access="hasPermission(#domain, 'permission')> inside a jstl core loop

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?

OmniFaces validateOrder disabling

I'm trying to use validateOrder component to validate two java.util.Date objects. It is similar to showcase example on this link (PrimeFaces example). Everything works perfect, but i have one question:
What if 2nd date field is not required?
In that case i'm getting nullpointer exception, and since validateOrder has "disabled" attribute, i was wondering is it worth/possible enabling/disabling it via ajax every time the 2nd date is inserted/removed. If not, i guess i'll stick to Balus' approach for JSF2.0 cross-field validation that you can read about on this link.
Let the disabled attribute check if the 2nd field is filled in. If it's not filled in, the request parameter value associated with field's client ID will be empty. Use exaclty that to let disabled attribute evaluate to true.
<p:calendar ... binding="#{endDate}" />
...
<o:validateOrder ... disabled="#{empty param[endDate.clientId]}" />
Code is as-is. No additional backing bean property necessary for binding.
See also:
How does the 'binding' attribute work in JSF? When and how should it be used?

Multiple <f:event> tags

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 :

Apache-Shiro: User authenticates within AJAX, how to restore GET-Variables after login?

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>

How do I do substitutions for localised strings in freemarker

I'm using spring and freemarker and have the basics working.
I've got a properties file like
help.text=For further information please see the help page.
I'm currently outputting localised messages using
${rc.getMessage("help.text")}
However I'm having trouble figuring out how I can pass in my substitution variables. Can you help?
Cheers,
Peter
If I read the Spring API documentation about RequestContext (your rc?) correctly, then
${rc.getMessage("help.txt", ["yourHelpUrl"])}
might work, because getMessage can receive an additional List argument with message args, which you can supply via a FreeMarker sequence.
<#import "/spring.ftl" as spring/>
<#assign args = ["yourHelpUrl"]>
<#spring.messageArgs "help.txt" args/>
I always do the substitution variables in my Java code somewhere and then dump the fully localized text into a Map where it's accessed by Freemarker like this:
${localizedValues["help.txt"]}
If you are correctly configuring freemarker in the spring MVC context then the right way to do it is:
Import the spring macros in the template
Use the mensaje macro
<#import "/spring.ftl" as spring />
<#spring.messageText "code", "Default message"/>
See the documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/view.html#view-velocity

Resources