Javatpoint Logo
Javatpoint Logo

Spring and Struts 2 Integration

Spring framework provides an easy way to manage the dependency. It can be easily integrated with struts 2 framework.

The ContextLoaderListener class is used to communicate spring application with struts 2. It must be specified in the web.xml file.


You need to follow following steps:

  1. Create struts2 application and add spring jar files.
  2. In web.xml file, define ContextLoaderListener class.
  3. In struts.xml file, define bean name for the action class.
  4. In applicationContext.xml file, create the bean. Its class name should be action class name e.g. com.javatpoint.Login and id should match with the action class of struts.xml file (e.g. login).
  5. In the action class, define extra property e.g. message.

Example of Spring and Struts 2 Integration

You need to create following files for simple spring and struts 2 application:

  1. index.jsp
  2. web.xml
  3. struts.xml
  4. applicationContext.xml
  5. Login.java
  6. welcome.jsp
  7. error.jsp
1) index.jsp

This page gets the name from the user.


2) web.xml

It defines controller class for struts 2 and ContextLoaderListener listener class to make connection between struts2 and spring application.


3) struts.xml

It defines the package with action and result. Here, the action class name is login which will be searched in the applicationContext.xml file.


4) applicationContext.xml

It defines a bean with id login. This beans corresponds to the mypack.Login class. It will be considered as the action class here.

It should be located inside the WEB-INF directory.


5) Login.java

It defines two property userName and message with execute method where success is returned.


6) welcome.jsp

It prints values of userName and message properties.


7) error.jsp

It is the error page. But it is not required in this example because we are not defining any logic in the execute method of action class.

Output

struts 2 and spring integration 1
spring and struts 2 integration 2





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA