Spring MVC – Multiple Submit Buttons Example

Spring MVC – Multiple Submit Buttons Example

Let us see an example code today for Spring MVC that has multiple submit buttons in one form. This is Second spring mvc tutotial. (Spring MVC example1 is here).

In this example, We are going to have 2 submit buttons. “Success” and “Failure”. When user clicks “Success” button it should pass to Controller and do some functions and take the user to respective page. Similarly when user clicks “Failure” it should go to Controller and then pass it to respective page.

Structure:
Structure

com.javainfinite.pojo
employee.java

page1.jsp

page2.jsp (Success click page)

page3.jsp (Failure click page)

com.javainfinite.controller
econtroller.java

applicationContext.xml

dispatcher-servlet.xml

Output:
output1

On Clicking Success Button,

output2

On clicking Failure Button,

output3

 

 

 

 

 

 

 

Read More

Autowiring in Spring Example – Autowire byType

Autowiring in Spring Example – Autowire byType

Autowire byName
If the name or id of the bean declared in applicationContext.xml (or xml which you create) is same as the name of the bean property then autowire byName can be applied

Autowire byType
Means it will search for one bean of given property in the container(Only one bean). If there are more than one bean, then it will not autowire.

Autowire Constructor
It will search for compatible constructor with given parameters

Example:
Autowire byName

Directory Structure
structure

employee.java

employeedependent.java

operational.java

applicationContext.xml

Note: autowire byType applies for exactly one type, here we have declared only one type of bean and it has applied to that. In output you can see, the value for employee2.getEname() also shows the same value of employee1.getEname(). This is cause the byType applies depending on the type

Output

op2

 

 

 

 

 

Read More

Autowiring in Spring Example – Autowire byName

Autowiring in Spring Example – Autowire byName

Autowire byName
If the name or id of the bean declared in applicationContext.xml (or xml which you create) is same as the name of the bean property then autowire byName can be applied

Autowire byType
Means it will search for one bean of given property in the container(Only one bean). If there are more than one bean, then it will not autowire.

Autowire Constructor
It will search for compatible constructor with given parameters

Example:
Autowire byName

Directory Structure
structure

employee.java

employeedependent.java

operational.java

applicationContext.xml

Note: you can see that the object name in employeedependent.java and bean id in applicationContext both are same – autowire byName

Output:
op1

 

 

 

 

 

Read More

Dependency Injection using Collections in Spring

Dependency Injection using Collections in Spring
Dependency injection in spring can also be used with collections like List, Set and Map. Here is an example for Dependency Injection using List

 


Directory Structure
structure

employee.java

employeedependent.java

operational.java

applicationContext.xml

Output
op1

 

 

 

 

Read More

Inner Bean in Spring Example

Inner Bean in Spring Example
When a bean is used for only one particular property, we can declare them as inner beans.

Directory Structure
structure

employee.java

employeedependent.java

operational.java

applicationContext.xml

Output
op1

 

 

 

Read More