Posts

AWS Configuration For RDS(postgres),ElastiCache(Redis) with ElasticBean

AWS Configuration For RDS(postgres),ElastiCache(Redis) with ElasticBean RDS Database Creation Go to AWS Management Console and use Find Services to search for RDS Click Create database button Select PostgreSQL Check 'only enable options eligible for RDS Free Usage Tier' and click Next button Scroll down to Settings Form Set DB Instance identifier to multi-docker-postgres Set Master Username to postgres Set Master Password to postgres and confirm Click Next button Make sure VPC is set to Default VPC Scroll down to Database Options Set Database Name to fibvalues Scroll down and click Create Database button ElastiCache Redis Creation Go to AWS Management Console and use Find Services to search for ElastiCache Click Redis in sidebar Click the Create button Make sure Redis is set as Cluster Engine In Redis Settings form, set Name to multi-docker-redis Change Node type to 'cache.t2.micro' Change Number of re...

Spring MVC Form Validation - Creating Custom Validation Rule

Image
Custom Validation Creating a custom validator entails us rolling out our own annotation and using it in our model to enforce the validation rules. So, let's create our  custom validator – which checks course code . The course code must starts with "LUV". The New Annotation Let's now create a validator class that enforces rules of our validation: The validation class implements the  ConstraintValidator  interface and must implement the  isValid  method; it's in this method that we defined our validation rules. Naturally, we're going with a simple validation rule here, to show how the validator works. ConstraintValidator d efines the logic to validate a given constraint for a given object. Implementations must comply with the following restriction: the object must resolve to a non-parametrized type generic parameters of the object must be unbounded wildcard types Applying Validation Annotation We defined a string fi...

How to handle String inputs for Integer fields

Image
Development Process If the user enters String input such as "abcde" for the Free Passes integer field, we'd like to give a descriptive error message. We basically need to override the default Spring MVC validation messages. Follow these steps. 1. In your Eclipse project, expand the node:  Java Resources 2. Right-click the  src  directory and create a new sub-directory:  resources 3. Right-click the  resources  sub-directory and create a new file named:  messages.properties Your directory structure should look like this: 4. Add the following entry to the  messages.properties  file 5. Save file --- This file contains key/value pairs for the error message type For a basic example:    typeMismatch.customer.freePasses=Invalid number The format of the error key is:    code + "." + object name + "." + field To find out the given error code, in your Spring controller, you can log the detail...

Resolve issue on white space in validation(Spring validation)

Image
In the above form we need to fill all required fields to submit the form. In some case we able to by pass the form by using white spaces. But the application could not able to allow the user to submit with white space. For resolve this issue we can use @InitBinder in spring. This @InitBinder will Pre-process every string form data  Remove leading and trailing white space If string only has white space ... trim it to null For use this @InitBinder we need to initBinder method in the Controller. @InitBinder public void initBinder(WebDataBinder dataBinder) { StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true); dataBinder.registerCustomEditor(String.class, stringTrimmerEditor); } By using this we can resolve the issue

How to populate radio buttons with items from Java class?

Here are the steps 1. Set up the data in your Student class Add a new field     private LinkedHashMap<String, String> favoriteLanguageOptions; In your constructor, populate the data         // populate favorite language options         favoriteLanguageOptions = new LinkedHashMap<>();         // parameter order: value, display label         //         favoriteLanguageOptions.put("Java", "Java");         favoriteLanguageOptions.put("C#", "C#");         favoriteLanguageOptions.put("PHP", "PHP");         favoriteLanguageOptions.put("Ruby", "Ruby");         Add getter method     public LinkedHashMap<String, String> getFavoriteLanguageOptions() {    ...

Use properties file to load select options (Drop down list) in spring form

This solution will show you how to place the country options in a properties file. The values will no longer be hard coded in the Java code. 1. Create a properties file to hold the countries. It will be a name value pair.  Country code is name. Country name is the value. New text file:  WEB-INF/countries.properties BR = Brazil FR = France CO = Colombia IN = India Note the location of the properties file is very important. It must be stored in WEB-INF/countries.properties 2. Update header section for Spring config file We are going to use a new set of Spring tags for <util>. As a result, you need to update the header information in the Spring config file. File: spring-mvc-dmo-servlet.xml Remove the previous header and add this. <? xml version = "1.0" encoding = "UTF-8" ?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/contex...

Deploying your App to Tomcat as a Web Application Archive (WAR) file

When you deploy your Java web apps, you can make use of a Web Application Archive (WAR) file. The Web Application Archive (WAR) file is a compressed version of your web application. It uses the zip file format but the file has the .war extension. If you are using Eclipse, then the best way to visualize it is think of your "WebContent" directory being compressed as a zip file with the .war extension. This includes all of your web pages, images, css etc. It also includes the WEB-INF directory which includes your classes in WEB-INF/classes and supporting JAR files in WEB-INF/lib. The WAR file format is part of the Java EE / Servlet specification. As a result, all Java EE servers support this format (ie jboss, weblogic, websphere, glassfish and tomcat). Below, I provide steps on how to create a WAR file in Eclipse. I also show how to deploy the WAR file on Tomcat. 1. In Eclipse, stop Tomcat 2. Right-click your project and select Export > WAR File 3. In the Des...

How to use CSS, JavaScript and Images in Spring MVC Web App

Image
Here are the steps on how to access static resources in a Spring MVC. For example, you can use this to access images, css, JavaScript files etc. Any static resource is processed as a URL Mapping in Spring MVC. You can configure references to static resources in the spring-mvc-demo-servlet.xml. In my example, I'm going to have the following directory structure: I chose to put everything in the "resources" directory. But you can use any name for "resources", such as "assets", "foobar" etc. Also, you can give any name that you want for the subdirectories under "resources". Step 1:  Add the following entry to your Spring MVC configuration file:  spring-mvc-demo-servlet.xml You can place this entry anywhere in your Spring MVC config file. <mvc:resources mapping="/resources/**" location="/resources/"></mvc:resources>   Step 2:  Now in your view pages, you can access the static files using ...

Configure the Spring Dispatcher Servlet using all Java Code (no xml)

Here are the steps 1. Delete the files: web.xml file and spring-mvc-demo-servlet.xml files 2. Create a new Java package:  com.luv2code.springdemo.config 3. Add the following Java files in your package File: DemoAppConfig.java package com . luv2code . springdemo . config ;   import org . springframework . context . annotation . Bean ; import org . springframework . context . annotation . ComponentScan ; import org . springframework . context . annotation . Configuration ; import org . springframework . web . servlet . ViewResolver ; import org . springframework . web . servlet . config . annotation . EnableWebMvc ; import org . springframework . web . servlet . view . InternalResourceViewResolver ;   @Configuration @EnableWebMvc @ComponentScan ( basePackages = "com.luv2code.springdemo" ) public class DemoAppConfig {   // define a bean for ViewResolver   @Bean public ViewResolver viewResolver () { InternalResou...