Book Image

Spring Essentials

Book Image

Spring Essentials

Overview of this book

Spring is an open source Java application development framework to build and deploy systems and applications that run on the JVM. It is the industry standard and the most popular framework among Java developers with over two-thirds of developers using it. Spring Essentials makes learning Spring so much quicker and easier with the help of illustrations and practical examples. Starting from the core concepts of features such as inversion of Control Container and BeanFactory, we move on to a detailed look at aspect-oriented programming. We cover the breadth and depth of Spring MVC, the WebSocket technology, Spring Data, and Spring Security with various authentication and authorization mechanisms. Packed with real-world examples, you’ll get an insight into utilizing the power of Spring Expression Language in your applications for higher maintainability. You’ll also develop full-duplex real-time communication channels using WebSocket and integrate Spring with web technologies such as JSF, Struts 2, and Tapestry. At the tail end, you will build a modern SPA using EmberJS at the front end and a Spring MVC-based API at the back end.By the end of the book, you will be able to develop your own dull-fledged applications with Spring.
Table of Contents (14 chapters)
Spring Essentials
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Validating forms


Spring MVC makes form validation a lot easier using Spring's Validator framework. You might have noticed the @Valid annotation and the usage of the BindingResult.hasErrors() method call inside handler methods listed in the previous section. They are part of the validation framework.

Let's create a validator for a Task object by following these steps:

  1. Add the Validation API's Maven dependency, javax.validation (build file: pom.xml).

  2. Make sure you have defined MessageSourceBean for the validation-errors properties file in your bean definition:

    <beans:bean id="messageSource" class="org.springframework.context.
    support.ReloadableResourceBundleMessageSource">
       <beans:property name="defaultEncoding" value="UTF-8" />
       <beans:property name="basenames" value="classpath:validation-errors" />
    </beans:bean>
  3. Make sure there is a validation-errors.properties file with the following sample content in your root resources location. You may add as many error messages...