session.getAttribute and session.setAttribute in Spring MVC

session.getAttribute and session.setAttribute in Spring MVC
Here is an example of how to use session.setAttribute and session.getAttribute, the same way we use in Servlets.  (for Spring mvc simple form handling example-click here and for Multiple submit buttons in spring mvc form – click here).

In this example we are going to see how to use “session.setAttribute” and “session.getAttribute”. We are going to get username in page1.jsp and then control passes to page2.jsp and we are going to display the username in page3.jsp using session attributes.

Structure:
Structure

employee.java

page1.jsp

page2.jsp

page3.jsp

econtroller.java

applicationContext.xml

dispatcher-servlet.xml

Output

op1

op2

op3

 

 

 

 

 

Read More

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

Setter Injection in Spring Example

Setter Injection in Spring Example

Setter injection is a bean wiring technique in which JavaBean setter methods are used for setting bean properties to the objects that need them.

Directory Structure:
structure

employee.java

operational.java

applicationContext.xml

Output:
op1

 

 

 

Read More

Spring Hibernate Integration Example

Spring Hibernate Integration Example:
Here is a code for Spring and Hibernate integration in Netbeans 8.0.2.

Create Project:
File–>New Project –>Java –>Web Application
Give a name to the application and click Next–>Next–>
Choose Spring and Hibernate in Frameworks and choose the database for Hibernate. and click Finish

Create 2 packages inside Source Package and name them as,
1) Dao
2) pojo

Inside pojo, create 2 java class
1) Student.java
2) insert.java
3) Right click pojo –>New–>HibernateUtil.java and click Finish

Inside Dao, Create a java class
1) StudentDao.java

student table (MySQL)

Right Click Source packages, New–> Hibernate Reverse Engineering –> select the table –> Add –>Finish

Right click pojo –>New–>Hibernate Mapping files and Pojo’s from Database–>Finish

Directory Structure:
springhiber

Student.java

StudentDao.java

applicationContext.xml

hibernate.cfg.xml

insert.java

Output:
(insert some sample values in your student table – I have inserted a name with victor 3 times with different id’s)

outpu1

 

 

 

 

 

 

Read More