Spring Webflow model validation. - spring

We have developed an internet bank project in our company. We have used spring-mvc in this project and all of property validations have been carried out using Hibernate validator. However for some of our services that have a state-machine nature, we need to use spring-webflow.
In doing so we need to validate models in spring webflow without adding any extra validators. As you know There are 2 ways to validate a model programmatically:
Define validation logic in the model object.
Define a separate object called Validator to validate the model.
However, we DO NOT want to use none of these validation techniques.
For example lets imagine we had a Class named Customer. We used annotation validation for the properties in this class in our project. Now, we want to use this class as a model in view states in our webflow.
I was wondering if someone could help us how to validate models in spring webflow using annotation validation with hibernate validator.

Spring Webflow 2.4 supports JSR-303 validation and partial validation through the use of validation groups.
You need to add the following to the application context xml file:
<webflow:flow-registry flow-builder-services="flowBuilderServices" />
<webflow:flow-builder-services id="flowBuilderServices" validator="validator" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
This will allow you to validate your model objects using annotations.

Related

How to add spring editing into config files

I am very new to spring. I saw some spring validations where the code was embedded into java source code. Im am trying to avoid this.
Is there a way to put the editing into the bean config file using properties.
For example #Size min=6 max=10 to
<bean id ="myvalidator"
class = "org.some.spring.class.blahblahblah"
<property name="min" value="6">
<property name="max" value="10">
<property name="errormsg" value=${error.msg}>
/>
or something to that effect
How do I associate #annotation to "com.some.spring.classname"
Validation annotations do not belong to Spring Framework, usually they come from the javax.validation package, sometimes, they come from a vendor, like hibernate ones for example. Check the import statement to be sure about that.
So if your purpose of trying to get rid of the annotations is to not being dependant on any frameworks, unless you are using vendor specific validation annotations you shouldn't worry.
But anyways, if you still want to get rid of the annotations in your code and don't use any validation framework, one way is to create a Validator class elsewhere, and then use Spring for inject it whenever it is needed.
Let's say for example (i'm going to use JavaConfig style, because i'm not very familiar with XML)
#Component
public class MyValidator {
// some validation code
}
In some configuration class
#Configuration
public class MyConfigurationClass {
#Bean
public MyValidator myValidator() {
return new MyValidator();
}
}
And now you can inject with #Autowire an instance of your validator whenever you need it.
Edit: Btw, in this scenario, validation code should be coded manually, that is the price to pay if you want to get rid of the annotations.

Register Spring filter from BeanDefinitionParser

I am creating a custom namespace for use in the Spring XML configuration. I already implemented a NamespaceHandler and BeanDefinitionParser. So, now I can just put <myns:some-awesome-feature /> into my Spring configuration and it creates the required beans automatically.
In addition to creating some normal Spring beans I would also like to have this annotation register a OncePerRequestFilter to extract some information off of a request for my code to be able to utilize. Is there a way to register a filter programmatically using the two classes I have available when implementing a custom XML tag?
It is not possible without touching web.xml or WebApplicationInitializer, respectively.
But you can create an extendable solution that allows modifications in future without hassle.
Spring Security's <http pattern='...' security="..."/> automatically creates and registers a series of chained filter beans for you. All you have to do is to
register DelegatingFilterProxy in you web.xml and reference springSecurityFilterChain.
You can create a similar solution where you are defining e.g. <myns:awesome-http pattern='...' /> which instantiates OncePerRequestFilter. In web.xml you are declaring a DelegatingFilterProxy which references your awesomeFilterChain. In a future version you can add more filter to your chain without touching the configuration.
I have never implemented such a feature but I'm quite confident that it is possible.
As a starting point take a look at the source of HttpConfigurationBuilder and HttpSecurityBeanDefinitionParser to see how Spring Security implemented <http .../>.

mapping controllers without annotations

Is there a way to do mapping in Spring/Spring-boot to controllers without anno's on controllers?
In Grails I was able to write a completely abstracted api without api functionality, checks, annotations on the controller. This allows for all functionality and data to be handled in the front controller through handlerInterceptor... and controller merely has to create resource. This also makes it easier to share functionality/data for api across I/O flow in architecture.
How do we map request to controller/method without annotations? There must be a way since it can be done in Grails.
Put below tag in your bean configuration file. This will scan your controller class in your base package. Just define package name where you put your controller classes.
<beans
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package=" " />
</beans>
As for example -
You have controller class called controller1, controller2 in com.pacakge.controller.demo1
and controller3, controller4 in com.pacakge.controller.demo2
here your code -
<context:component-scan base-package="com.pacakge.controller" />

Spring framework. Set proxy for all controller

How can i set proxy for all my controllers in spring framework?
I wanna change the return expresion of my controllers.
I'm going to assume you're using Spring MVC 3.* and an XML configuration. If this is not the case, please let me know and I will update my answer accordingly.
Spring MVC provides the HandlerInterceptor interface which can be used to both pre and post process requests handled by all Controllers. I would suggest that you create and implementation of this interface and use the postHandle() method to change the output of your Controllers accordingly.
Once you have your HandlerInterceptor implementation complete, you will need to instruct Spring MVC to use it. The namespace configuration for Spring MVC makes this very easy. As an example:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.example.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
In this example I am registering a single HandlerInterceptor implemented by the class com.example.MyInterceptor. I am also configuring Spring MVC to have the HandlerInterceptor implementation work on all requests. You can of course change the mapping to suit your application needs.

spring xslt view

I want to add xslt view to my spring web application. But my web application configured using spring annotations and hence it does not have any expicitly declared view resolvers. I tried to add a view resolver specially for xslt but the rest of my application becomes unresolvable. It it possible to configure xslt view only using annotations? Or may be there is another solution?
Thank you.
I would guess that it would suffice to write a ViewResolver class and annotate it as #Component, provided that you have <mvc:annotation-driven /> in your xml context.

Resources