How to read a spring property value straight into an xml file - java

I have an xml file that contains values that define a java object, the values are processed via a java method to handle some specific tasks, the xml file has the below architecture :
<javaObject>
<attr1>value1</attr1>
<attr2>${property.name}</attr2>
<attr3>value3</attr3>
</javaObject>
My goal is to get the attr2 from the property file, I've tried ${property.name} but it's not working, I've also tried
<property name="property.name" value="${property.name} />
At runtime, I get a NULL when I call the value of attr2
What is the best way to implement that ?

If I understand your question correctly, you unmarshal the object and want to work with the value of proeprty.name?
You can inject the value of property.name in your class via
#Value("${property.name}")
String propertyName

Related

How to extract xpath list in camel

My requirement is as below:
I have a properties file which has values as:
/message/header/messagetype ='DATA'
So the XPaths are the keys and the values are the dates I want to check in Camel at runtime
I read an ActiveMQ queue which gets me an XML message. Now at runtime I need to check the respective XPaths from the properties file and check there respective values in the XML message I get from the queue. There could be any number of XPath checks defined in the properties file. This is defined by the business needs and the code should take care of it.
There is no standard way to read a properties file and "transform" it to XPath checks. However, you can always use a plain Java Bean and call it from your Camel route. For example like this:
from()
...
.bean(YourBean.class)
...
With this you can use Java code as a "component" in your Camel route.
For more details see the Camel docs about Bean binding and the Bean component.
Thank you for the help. I figured out how to do this by using Predicate functionality in Camel.So what I am doing now is, creating multiple Predicate objects based on my property file entries and adding them to 'PredicateBuilder' as below ::
PredicateBuilder.and(p1,p2....pn);
This returns me an object of Predicate which I can use in when() of Camel.
for eg. choice().
when(predicateBuilder()).

Jasper Report: Store in variable value of parameter bean property

I've a little issue to propose.
I've defined in Spring a bean named EnvParam. I've passed this bean in my report processed by Jasper, using an hashmap of parameters.
In Jasper XML I've mapped my bean with import tag in this way:
<import value="Mypath.EnvParam" />
After that I want to point my bean properties in GUI elements.
So, I've defined in Jasper some variables in this way:
varDummy = $P{EnvParam}.myProperty
so in my GUI element I've that link $V{varDummy}.
When I run my application my report doesn't show the correct value of property, setting NULL in my GUI.
But if I put in my GUI object my property $P{EnvParam}.myProperty without using of variable the value will show correctly.
I've resolved my issue, changing the procession time of variable, put the value as "REPORT"
The default the value has set on "NOW" (I think - in italian, version used the value named "ADESSO")

Dynamic key in bean:message in Struts 1.3

I am working on a project which made use of an old (but nice) framework Struts 1.3, and I am trying to accomplish a simple task.
I have a bean containing a property, which is a key corresponding to a property file. I would like to use it for recall the appropriate translation.
Variable in property file
props.myprop.sample=This is my sample property
The property is in a bean passed to the jsp called for convenience AllProps which has a getter for the property, and this should be a pseudo code:
<bean:define id="sample" name="AllProps" property="sample" type="java.lang.String"/> // should result in sample = props.myprop.sample
<div><bean:message key="sample"/></div>
Which should output:
<div>This is my sample property</div>
But obviously result in a property not found, can you give me help on how to deal with this ?
I would like to stick as much as possible to Struts tag, then Jsp tag, and scriptlet as last resource.
Thanks
Straight from the documentation:
<bean:message>
Render an internationalized message string to the response.
Retrieves an internationalized message for the specified locale, using
the specified message key, and write it to the output stream. Up to
five parametric replacements (such as "{0}") may be specified.
The message key may be specified directly, using the key attribute, or
indirectly, using the name and property attributes to obtain it from a
bean.
(emphasis mine)
So, all you need is
<bean:message name="AllProps" property="sample"/>

Returning an int whenusing webServiceTemplate.marshalSendAndReceive

I'm using Spring WebServiceTemplate class to to create and instantiate a request object of a JAXB generated class, call the marshallSendAndReceive method with it and then to cast the response object to an object of the JAXB generated response class.
This is working fine when returning the XML Objects of the JAXB generated response class (with Select Query) but now I want to execute a Delete query and just want to return the number of rows deleted. But I'm not sure how to achieve this!!
Do I need to convert that int return value into an XML object by using the following in the schema.xsd:
<xs:element name="DelResponse" type="xs:integer"/>
OR
Is there another way of achieving the same.
Thanks
Do I need to convert that int return value into an XML object
Yes. All web service messages are encoded as XML, so you need to find a way of representing everything in XML, even if it's just a plain integer.
If you want something simpler, then SOAP/Spring-WS/JAXB isn't really the tool for the job.

Spring "typemismatch" and required fields

In the context of Spring Webflow 2.0.x......
I handle form binding "typemismatches", i.e. as a result of trying to map a String onto a Integer field, by using the following in my messages.properties
typeMismatch={0} contains invalid data.
This works fine.
The problem is that if the field that the typeMismatch error occurred on was "required" then I also receive an error for the missing required field, which is logical I guess because the value that was submitted was never bound. ("Required" being defined in a Commons Validation XML file)
So, I dont want to see the "XXX is required field" error message when the field is only missing due to the typeMismatch. How do I resolve this? I thought about overriding initBinder() on the FormAction but quickly got nowhere.....
Like Yves mentioned, among the three approaches, i have used a custom validator method and its very easy. You can use a custom validator which checks if the form field already has a xml error message of required. If the field does not have an error, then you can check for your string validation. That way it will display only one.
The other method that you could use is try a multiple xml validation, one being required and the other one being a mask which checks for a particular regular expression. In your case if your field is an integer field, then you can go and perform a mask with regex checking for only numbers. The order of mask, required or required, mask in the xml decides which message gets a higher preference.
For example:
<field property="somefield" depends="required,mask" page="2">
<arg key="somelabel"/>
<var>
<var-name>mask</var-name>
<var-value>${somepattern}</var-value>
</var>
</field>
You have many options, in order of preference:
Set selectively the message typeMismatch.target.yourFieldName or typeMismatch.int in resources files
Implement your own Validator so that you can send a dedicated message when Integer parsing will fail before the binding step
Create a BindingErrorProcessor to handle different kind of parsing issues

Categories

Resources