Springboot MVC – Passing Values from JSP to Controller

Springboot MVC – Passing Values from JSP to Controller

In this article let us see how to pass values from JSP page to Spring Controller. We are going to implement MVC with SpringBoot.

For understanding Springboot with MVC – Basic example – Please refer here

In this example, We are going to have a Employee Bean, Controller and a main class. We are going to have 2 JSP pages, home.jsp and welcome.jsp.

home.jsp page loads when the springboot application is deployed, we will enter the name there and the name will be passed to controller and from controller it will go to welcome.jsp and will display the name.

In order to use spring form in the JSP pages, we require to include this taglib

<%@ taglib uri=”http://www.springframework.org/tags/form” prefix=”form”%>

Here is the Project Structure,





In the above code, We have declared model.addAttribute(“Employee”, employee); where employee is a bean and is autowired. So the bean is made available.

What will happen if we don’t declare Bean?
Well, if we dont make the bean available, we will be hitting an exception – BindingResult nor plain target object for bean name available as request attribute.

So when creating an mvc project, make sure bean is available.








Download code here

Read More

SpringBoot MVC with Example

SpringBoot MVC with Example:

In this article let us see an example of MVC (Model View Controller) with SpringBoot.

This is an simple example of SpringBoot MVC, without involving database. We are going to use @RestController with mapping and deploy the application.

Why SpringBoot MVC and Why not Spring MVC?
The main advantage of using SpringBoot for MVC over Spring MVC is,
Spring Boot does this with Zero XML configuration in your project.  We will not be needing deployment descriptors or any other configuration files.

Let us see an example,








Download code here


Read More

SpringBoot @Lazy Annotation with Example

SpringBoot @Lazy Annotation with Example:

In our application we might dont want to initialize all bean when the springboot application starts. We might want to initialize certain beans only when it is required.

In order to initialize the bean only when it is required we use an annotation @Lazy

To notify the spring boot application to initialize the bean as Lazy, we need to declare this annotation in @Component or @Bean definition. If this annotation is not present, spring boot will follow eager initialization.

@Lazy annotation can also be used in @Configuration config class. If @Lazy is at Configuration class then it means all the beans declared in configuration class should be lazily initialized. It is possible to declare @Lazy for specific bean to within Configuration class.

When declaring the @Lazy annotation for specific @Bean, we  need to pass the value as true or false. @Lazy (value = “true”).

Now let us see an example,

We are going to create to model classes, Student and Employee. We are going to have Configuration class to declare these beans.







From the output we can see Employee got initialized but Student is not initialized when the application starts.

Download Code here

Read More

Generics in Java with Examples

Generics in Java with Examples

Generics allows us to pass the generic types as parameters to a class or methods.
Example: class A<T>

Generics Advantages:

  • Stronger type checking
  • Casting elimination

Type Parameter Naming Conventions:

  • E – Element
  • K – Key
  • V – Value
  • N – Number
  • T – Type

There are various ways of using Generics, In this post let us see, generics with

  • Upper Bounded Wildcards
  • Unbounded Wildcards
  • Lower Bounded Wildcards

First let us see an example of using generics along with the class


Now let us see another example of using Generics with Key and Value along with Upper Bound Wildcards, Unbounded Wildcard and Lower Bound Wildcards.

Wildcards usually starts with “?” – Example: List<? extends Number>

Upper Bound Wildcards
Used to relax restrictions on a variable. In the below example we have used upperBound wildcard to pass integers, calculate sum of it and change it double and print it.

Unbounded Wildcards:
Unbounded wildcards is used for unknown types. It can be of any type, Integer or String or object etc..

Lower Bound Wildcards:
Lower Bound Wildcards restricts the unknown type to be specific. (Upper Bound relaxes the restrictions)



Read More

SpringBoot – @Scheduled Annotation with Examples

SpringBoot – @Scheduler Annotation with Examples

In our applications we might need certain logic or method that needs to be executed at certain intervals. For ex: We might require a method to be executed every 15 min.

In SpringBoot we have an annotation to achieve this, @Scheduled annotation. From package –  org.springframework.scheduling.annotation.Scheduled.

@Scheduled types,

  • initialDelay
  • fixedRate
  • fixedDelay
  • fixedDelayString
  • cron


  • The milliseconds delay before the execution of fixedRate or fixedDelay


  • Invocation with specific period of time


  • Fixed period between the last invocation and the next invocation


  • Same as fixedDelay but this accepts string parameter
  • Example: @Scheduled(fixedDelayString = “5000”)

Now for example, if we pass incorrect number as string to fixedDelayString what will happen?

Let us pass this,
@Scheduled(fixedDelayString = “5000r“)
When we try to run this, we will get an exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘myApplication’: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method ‘ScheduledFixedRate’: Invalid fixedDelayString value “5000r” – cannot parse into integer


  • A cron-like expression which has 6 arguments – Minutes, Hour, Date, Month, Week and Year
  • It looks like @Scheduled(cron = “* * * * * *”)

Now let us see an detailed example,

In the above example we have scheduled a cron,

@Scheduled(cron = “10 02 21 * * *”) – This will execute exactly on 21:02:10 – 21 hours, 02 minutes 10 seconds

When the above code is executed,




Read More