For the current project we're using JSR-303 annotations to validate our interface parameters.
I needed to create a custom annotation for dates, as the default #Past and #Before also take into account the time.
This is the definition of my annotation:
#Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
#Retention(RUNTIME)
#Documented
#Constraint(validatedBy = { NotInFutureValidator.class })
/**
* Annotated element must be a date before tomorrow, compared to the system's date
*/
public #interface NotInFuture {
String message() default "{be.credoc.contractRegistration.interface_v2.validation.pastignoretime}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
The implementation of the validator is pretty straight forward.
Our webapplication is written in GWT and it makes use of gwt-validation. This allows us to validate our parameters on client side by means of the same annotations.
However, when I annotate my parameters with my custom annotation and I want to validate it by making use of the GwtValidatorFactory, it doesn't give me an error when the input date is in the future.
Does anyone have experiencing defining and using their own annotations in a GWT application and can see what I'm missing?
Thanks in advance
Instead of creating new annotations you might try to re-define the validators for the existing #Past and #Future annotations by providing an XML constraint mapping like this:
<?xml version="1.0" ?>
<constraint-mappings
xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd">
<constraint-definition annotation="javax.validation.constraints.Past">
<validated-by include-existing-validators="false">
<value>x.y.z.NotInFutureValidator</value>
</validated-by>
</constraint-definition>
</constraint-mappings>
This mapping must be registered in the configuration file META-INF/validation.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration validation-configuration-1.0.xsd">
<constraint-mapping>META-INF/validation/custom-constraints.xml</constraint-mapping>
</validation-config>
You can learn more about custom validators for existing constraints in the Bean Validation specification and the Hibernate Validator reference guide.
Apparently we wrote our own JavaScript implementations for each custom annotation, so the solution in this case was to write such an implementation.
Related
We're migrating our application from GXT 3 to GXT 4 (and also from GWT 2.5 to 2.8.2) and one of the things that changed is that lot of components' body/text/heading/etc. now have two separate setters. One accepts String param and the other one accepts SafeHtml.
Here's an example:
public void setToolTip(SafeHtml html) {
...
}
public void setToolTip(String text) {
...
}
The difference in those is that the method accepting String does not render html elements. The other one, however, does, which is perfectly fine if one uses Java code to build UI.
Unfortunately, we do have a lot of our UI built using GWT's XML method and I would like it to stay this way.
The problem is that I cannot figure out how to show e.g. a tooltip with SafeHtml body. When I try to do that I get compilation errors.
This is what I put in my XML file:
...
<form:TextArea ui:field="testField" toolTip="{messages.testMesssage}" >
...
And this is the error:
[ERROR] java.lang.String required, but {testMess.test} returns com.google.gwt.safehtml.shared.SafeHtml: <form:TextArea toolTip='{messages.testMesssage}' ui:field='testField'> (:184)
Thanks!
You may want to look at the GXT examples for tooltips in UiBinder. In there, it shows using the ToolTipConfig instead of trying to set "tooltip" directly.
<ui:with type="com.sencha.gxt.widget.core.client.tips.ToolTipConfig"
field="toolTipConfig">
<ui:attributes title="Information" body="Prints the current document" />
</ui:with>
...
<form:TextArea ui:field="testField" toolTipConfig="{toolTipConfig}" />
I'm writing a custom validator for a custom form component in Angular2.
Right now, i can inject dependencies in my custom validator such as services.
But i need to have access from the validator to informations stored in the Component...
So i'm trying to inject the component in the Validator class, but this does not work..
I think i'm missing something with DI !
Look at this plunker here
When you open it, everything should work fine.
Uncomment everything in the validator constructor, like this:
constructor( private test: TestService, private component: CustomFormControlComponent ) {
test.serviceMethod();
component.componentMethod();
}
And check the log output to see that DI failed to resolve the second parameter ...
Any idea why ?
Thanks a lot !
All
I have seen the jboss-service.xml which use an extended SystemPropertiesService class to reference to custom property file. But I didn't totally understood this kind of usage yet.
Could someone please give me some help to understand how to use these two class? thanks.
The SystemPropertiesService is very useful to define properties that then can be accessed from your application, it's usually used to parametrize the application without having to change to code, or even the application package (provided you place the jboss-service.xml outside de war / ear / jar structure). For example, you can create a myapp-service.xml file with the following content:
<server>
<mbean code="org.jboss.varia.property.SystemPropertiesService" name="jboss:type=Service,name=MyAppProperties">
<!-- Define the properties directly in the service.xml file-->
<attribute name="Properties">
myapp.property1=property1Value
myapp.property2=property2Value
</attribute>
<!-- You can also specify a route to another file where you define properties-->
<attribute name="URLList">
/home/myuser/txtlist.properties
</attribute>
</mbean>
</server>
Then you can deploy this file directly in JBoss, the properties defined will be visible to all the applications deployed in the same JBoss and you'll be able to access them with the static method:
String System.getProperty(String propertyName)
So if you want to access to the value of myapp.property1 from your application you'd do:
String property = System.getProperty("myapp.property");
On the other hand the PropertyListener is really an interface that defines a listener that will be triggered when any event occurs with a property. The org.jboss.util.property.PropertyAdapter is an abstract implementation of this interface. To use it you've to implement its three methods (propertyAdded, propertyChanged, propertyRemoved), that will be called by the container when a property is added, changed or removed respectively. Those methods have a PropertyEvent object as parameter, which let you know the property affected.
This interface/class is useful when you want your application to do something every time a property changes (a bad implementation would be that you check every certain time for a property change), this way, when JBoss detects that a property has changed its value, it will call the respective method (that you should implement with the behaviour you want).
For example, if you want to print the new property value everytime it's changed you could implement the propertyChanged method this way:
void propertyChanged (PropertyEvent pe){
// check the property that has changed
if (pe.getPropertyName().equals("myapp.property1")){
System.out.println("The value of " + pe.getPropertyName() + " has changed to " + pe.getPropertyValue());
}
}
Look for more information in the API, and for PropertyAdapter and PropertyEvent.
In JBOSS 5.1 it only works when you put the properties or URL in properties-service.xml and this file should go under jboss.home/server/default/deploy directory.
This one is related to my previous post. Is it possible in GWT validation framework to specify location of ValidationMessages.properties files and their names? I already have messages translations in my application and I'd prefer them to be in one location.
You could create a custom UserValidationMessagesResolver:
public class CustomValidationMessagesResolver extends AbstractValidationMessageResolver implements UserValidationMessagesResolver {
protected CustomValidationMessagesResolver() {
super((ConstantsWithLookup) GWT.create(ValidationConstants.class));
}
}
In the above code, ValidationConstants is the class resulting from running I18NSync on my properties file.
Then in *.gwt.xml:
<replace-with class="yourpackage.client.validation.CustomValidationMessagesResolver">
<when-type-is class="com.google.gwt.validation.client.UserValidationMessagesResolver" />
</replace-with>
You can find a complete example here.
I'm trying to replicate the loading of a property file of seam in javaee6 but still don't know where to start.
In seam we can load a property file as a seam component by defining it in the components.xml:
<component name="propertyBean" class="PropertyBean" scope="application" auto-create="true" startup="true">
<property name="filename">myPropertyFile.properties</property>
<property name="reload">true</property>
</component>
And then we can access it in code:
PropertyBean.getInstance().getProperty("myProperty");
Is there a javaee6 feature that will replicate this functionality? Or in spring it's called PropertyPlaceholder.
In c#, we can do it by adding configuration property in appsettings.xml. And access via ConfigurationManager.
Thanks,
czetsuya
Unfortunately, there's nothing like a property component manager from seam into javaee6, but I was able to find something similar, a property loader.
It works by having a qualifier:
#Qualifier
#Retention(RUNTIME)
#Target({METHOD, FIELD, PARAMETER, TYPE})
public #interface ConfiguredBy {
#Nonbinding public String value();
}
With a parameter that serves as the name of the property file.
The whole approach is describe here:
http://john-ament.blogspot.com/2010/03/writing-property-loader-in-java-ee-6.html