Spring + Tiles how to use EL in tiles.xml - java

I have tiles set up in my spring project as the view handler like this:
<bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" />
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/**/tiles.xml</value>
</list>
</property>
</bean>
I'd like to use EL to access the session scope inside tiles.xml, for resolving a jsp file name. This should be possible if i use CompleteAutoloadTilesContainerFactory, as described here:
http://tiles.apache.org/framework/tutorial/advanced/el-support.html
How can I set my spring tiles configuration to allow this. Ive tried using EL as it is currently set up but the EL is not parsed.

Figured it out, i left the configuration exactly the same and just included tiles-el.jar . No need to include tiles-extras.jar though. NFV

You may try setting completeAutoload property of TilesConfigurer class. Please see reference doc TilesConfirurer.
This may require Tiles-Extras 2.2 jar file.

Related

Refer to properties in an xml file, which are defined under a bean in another xml file

I have a property defined in an xml file - that.xml
that.xml
that.xml code for toReferProperty
I need to refer to the property - 'toReferProperty' - under the bean - 'thatBean' - in the file - 'that.xml' - in the file 'this.xml' in the below way and then based on that I need to use a property 'newProperty in the 'constructor-arg' as below-
========
this.xml
thisd.xml code for newProperty
Questions 1: How do I refer to the 'toReferProperty' in that.xml in 'this.xml'?
Question 2: How to use the 'toReferProperty' in the IF else loop in XML syntax?
Question 3: How to refer to 'newProperty' in 'this.xml' in the 'constructor-arg' in same xml?
You can try something like this. If you are trying to do this for different environments, i would recommend using spring Profiles instead of if/else but here's what you need to answer your question.
that.xml
<bean id="someProperty" class="java.lang.String">
<constructor-arg value="Hello"/>
</bean>
<bean id="thatBean" class="someClass">
<property name="referProperty" ref="someProperty"/>
</bean>
this.xml
<import resource="that.xml" />
<bean id="thisBean" class="someClass">
<constructor-arg value="#{ ${someProperty} == Hello ? newValue1 : newValue2 }"/>
</bean>

How to Parse XML Document By Class Name Using Java

I'm writing a parsing tool to compare the textual content of two bean XML files in Java. The text content changes and we need a way to run a script to make sure the textual content is the same. I know we have org.w3c.dom which has a method getElementsByTagName("tag_name") and that returns a node list in the XML document. I'm wondering if anyone knows of a way to do this using the class name? I've been searching around but haven't been able to solve this yet.
<bean class="com.mycompany.myText" id="Q1.4">
<property name="name">
<value>Q1.4</value>
</property>
<property name="text">
<value>This is text one</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
<bean class="com.mycompany.myText" id="Q1.5">
<property name="name">
<value>Q1.5</value>
</property>
<property name="text">
<value>This is text two</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
I can't use the 'bean' element name as there are several other beans whith non relevant stuff I need only the ones with the class com.mycompany.myText and the value I'm trying to extract is property name=text - the text content
Any help here would be appreciated. Also as a side note I should mention that we have no direct control of how the XML file is managed and structured as it's fed to us from a 3rd party.
As you say, you need to select tags by their attributes.
You could use XPath to achieve this. See those resources:
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
https://stackoverflow.com/a/24220968/6377268
The expression would be something like this
/bean[#class='com.mycompany.myText']/property[#name='text']/value
I hope I could help at least a little bit.

Front end label configuration in spring

I would like to have a external property (which has label name & value pairs) for each jsp pages in my web application.
I've done in this way like simply reading property file in a Map and placing in jsp using expression language.
Is there any other methodology to handle these in Java/Spring?
You can have these as message properties
http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/
This also explains this in detail - http://www.journaldev.com/2610/spring-mvc-internationalization-i18n-and-localization-l10n-example
If you need these to be by locale, you can use localechangeInterceptor also.
You can PropertyPlaceholderConfigurer for external properties (eg the systemPropertiesMode property)
http://www.summa-tech.com/blog/2009/04/20/6-tips-for-managing-property-files-with-spring
There are also more ways to do this like http://howtodoinjava.com/2015/01/27/how-to-load-external-resources-files-into-spring-context/
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>locale\customer\messages</value>
</property>
</bean>
spring mvc and ReloadableResourceBundleMessageSource
Yes there is another way to show the labels in spring and this one is the best practice code.
enter link description here This will explain you how to use <spring:message code="label.viewUser" /> in your project.

EL expressions in Apache tile definition is not processed

I am using Apache tiles for templating and part of the template is a header text. This text depends on the section the page belongs to. Each page contains a bean and the header text is built using the properties of that bean. The bean will have a different name for each page.
So, in my JSP file I would have something like this:
<div>${myBean.id} - ${myBean.name}</div>
I want to get that expression in the tile definition and I tried this:
<definition template="/WEB-INF/tiles/layout/mytemplate.jsp">
<put-attribute name="title" expression="${myBean.id} - ${myBean.name}" />
</definition>
And in the template I do:
<div class="title-header"><tiles:insertAttribute name="title" /></div>
But the result is the unprocessed EL expression:
<div>${myBean.id} - ${myBean.name}</div>
The code has been simplified here to keep this post concise but this is exactly what I'm trying to do. There are also reasons why I am trying to do it this way.
Any idea why the EL expresion is not being processed?
Thanks
NOTE: I am fairly new to JSP and Apache Tiles so I may not have used the correct terminology.
I just wanted to point out that Barry's answer (in his comment on the original post) helped me out. You need to have tiles-el.jar on your classpath (if you want to use the standard EL; presumably you need the corresponding JARs for MVEL or OGNL).
Tiles 2. Regarding AttributeEvaluator, here's how you can set that up if you're using Spring:
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/**/views.xml</value>
</list>
</property>
<!-- Initialize expression language support for use in Tiles definitions. -->
<property name="tilesProperties">
<props>
<prop key="org.apache.tiles.evaluator.AttributeEvaluator">org.apache.tiles.evaluator.el.ELAttributeEvaluator</prop>
</props>
</property>
</bean>
Tiles 3. Spring's TilesConfigurer for Tiles 3 automatically checks the classpath for the JSP API 2.1 and Tiles EL JARs. If it finds them both, it automatically creates an EL-aware attribute evaluator.

Why would Spring be trying to use the properties variable reference string instead of the value?

Here's the problem in a nutshell:
<bean id="handlerFactory" class="com.westfieldgrp.audit.jdklogging.cpm.CPMHandlerFactory">
<property name="schemaName" value="${env.audit.databaseSchema}" />
<property name="bufferSize" value="${env.audit.bufferSize}" />
<property name="threaded" value="${env.audit.threadedAuditHandler}" />
<property name="dataSourceSelector" ref="dataSourceSelector" />
</bean>
bufferSize on the CPMHandlerFactory is an int. Spring is failing because it is trying to set the value to '${env.audit.bufferSize}' instead of the actual value from the properties file.
Now, when I change the name of the properties file or env.audit.bufferSize in the file, Spring complains that it can't find the property 'env.audit.bufferSize'. This says to me that it CAN find the property, but instead of setting the value to '20', it's trying to set it to '${env.audit.bufferSize}'. Can anyone explain why Spring might be doing this and what I can do about it?
Contents of the properties file below:
env.audit.databaseSchema=TDB2DATA
env.audit.dataSourceName=java:comp/env/AuditDataSourceAlias
env.audit.bufferSize=20
env.audit.threadedAuditHandler=true
Thanks,
Peter
EDIT:
Found the problem thanks to Jamestastic below. Here's what it was:
We have a "master" context file that looks like so:
<import resource="environmentBeans.xml" />
<import resource="serviceBeans.xml" />
<import resource="auditBeans.xml" />
The 'environmentBeans.xml' has the PropertyPlaceholderConfigurer in it. The problem was, I added some code that referenced the 'auditBeans.xml' context, which of course doesn't have the configurer. I switched it to reference the 'master' context and it works great.
The key was understanding why the values wouldn't get substituted out: because there was no property configurer.
So, Thanks!
Did you remember to add <context:property-placeholder /> or a PropertyPlaceholderConfigurer bean definition to your Spring context?

Categories

Resources