Spring MVC Hibernate MySQL – Multiple Controllers

Spring MVC Hibernate MySQL – Multiple Controllers:
In our previous example we implemented Spring MVC Hibernate MySQL CRUD Operations (Here). In this article we are going to see how to use Multiple Controllers using Spring MVC.

In the below example, We are going to create 2 entities Employee and Manager and will have separate controllers for both these entities. Let us see how to add, view and delete. Edit operation is same as we discussed in previous example (Here), So i have not implemented Edit operation in this example.

Project Structure:

POM.xml

com.javainfinite.model:
Employee.java

Manager.java

com.javainfinite.dao:
EmployeeDAO.java

ManagerDAO.java

EmployeeDAOImpl.java

ManagerDAOImpl.java

com.javainfinite.service
EmployeeService.java

ManagerService.java

EmployeeServiceImpl.java

ManagerServiceImpl.java

spring-config.xml

web.xml

employee.jsp

manager.jsp

Output:

After Adding,

Deleting Employee,

On Clicking Manager Link,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Read More

Spring MVC Hibernate MySQL CRUD Operation

Spring MVC Hibernate MySQL CRUD Operation:
The idea of MVC framework is to separate the business logic from front end and change them without affecting others.

Model: Represents data
View: User Interface of the application
Controller: Receives request and perform the operations

 

  • The request is received by the dispatcher servlet
  • The dispatcher Servlet checks with the Handler Mappings to determine the appropriate controller to pass the request
  • The dispatcher Servlet get the appropriate controller from Handler Mapping and passes the request to Controller.
  • The controller sends the request along with the object containing the name of the view and data to be displayed, but not the exact logical name
  • Dispatcher Servlet after receiving the name, checks with view resolver to resolve the actual logical name.
  • Finally the view is displayed to the user

Now let us see the example of Spring MVC, Hibernate with MySQL.

Project Structure:

pom.xml

Employee.java

EmployeeDAO

EmployeeDAOImpl.java

EmployeeService

EmployeeServiceImpl

EmployeeController.java

 

spring-config.xml

web.xml

employee.jsp

edit.jsp

Output:

On clicking Edit

 

Create Another record,

Deleting Alpha record,

 

 

 

 

Read More

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

Spring MVC Example – Simple Form Handling

Spring MVC Example – Simple Form Handling:
Let us see an example of Spring MVC (form handling method). This application is tested using NetBeans.

Files to be Created:
Login.jsp
success.jsp
employeeLogin.java
employeeController.java

Structure:
structure

Login.jsp

success.jsp

employeeController.java

employeeLogin.java

applicationContext.xml

dispatcher-servlet.xml

Output:

op1

op2

op3

 

 

 

 

 

Read More