Monday, 5 August 2013

Struts to Spring mvc migration

Hi Friends,

Now I will share you step by step approach to migrate application from Struts 1.2 to Spring 3 using annotations.In struts to spring migration libraries, action classes, config files and jsp's will get changed as explained below :

STEP 1 : Replace struts libraries with spring libraries.

First step while migrating from struts to spring is replace your struts related libraries with spring libraries in lib folder. I have mentioned basic libraries of both stuts and spring for your clarification.

Struts basic libraries :
     1. struts.jar
     2. struts-legacy.jar

Spring basic libraries :
     1. standard.jar
     2. org.springframework.asm-3.0.1.RELEASE-A.jar
     3. org.springframework.beans-3.0.1.RELEASE-A.jar
     4. org.springframework.context-3.0.1.RELEASE-A.jar
     5. org.springframework.core-3.0.1.RELEASE-A.jar
     6. org.springframework.expression-3.0.1.RELEASE-A.jar
     7. org.springframework.web.servlet-3.0.1.RELEASE-A.jar
     8. org.springframework.web-3.0.1.RELEASE-A.jar

About *.tld's used in struts application, many tutorials on web says that it will remain same. Which makes me little unconfortable, as we are having some tld's which are solely used by struts. So we need to replace those struts tlds also. Following are the possible tld's which are need to be removed :



We need to change tld's with spring tags, which I will explain in upcoming step for jsp.

STEP 2 : Change in web.xml file for struts to spring migration

In this step, we are going to change intializing of struts config file with spring's confing file.

In Struts :
  We initialize config for struts in following way, which need to be replace :


In Spring :
   We initialize config for spring in following way :



STEP 3 : Replace config files for struts to spring migration

Now it comes the replacement of config files from struts to spring.
In Struts :
   We use struts-confing.xml as config file for dispatching request.
        <form-bean name="LoginForm"
            type="xyz.struts.helloworld.form.LoginForm" />
        <action path="/login" name="LoginForm" validate="true" input="/index.jsp" 
            <forward name="success" path="/welcome.jsp" />
            <forward name="failure" path="/index.jsp" />
    <message-resources parameter="resource.MessageResource"></message-resources>
In Spring :
   We will use spring-servlet.xml. Here name for confign is generated as [servlet-name]-servlet.xml. And servlet-name is mentioned in web.xm, here we have used spring.
        base-package="xyz.spring3.controller" />
    <bean id="viewResolver"
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
 Here, <context:component-scan> tag is used, so that spring will load all the components from given package i.e. "xyz.spring3.controller"
We can use different view resolvers, here I have used UrlBasedViewResolver. In which prefix and suffix are used to resolve the view by prefixing and suffixing values to the object returned by ModelAndView in action class.


STEP 4 : JSP changes for struts to spring migration

While migrating application from struts to spring, we need many changes in Jsp's. Which include tags replacement, tld's replacement and some other things as explained below :

1. tld replacement :

As I said earlier in step 1, we need to remove tld's of struts. So following entries in jsp's will be removed :

<%@ taglib uri="" prefix="bean" %>
<%@ taglib uri="" prefix="html" %>
<%@ taglib uri="" prefix="logic" %>
<%@ taglib uri="" prefix="tiles" %>

Replace these with following spring taglib's :

<%@ taglib prefix="form" uri=""%>
<%@ taglib prefix="spring" uri=""%>

We have removed taglib declarations. Now it's time for replacing its tags according to spring or normal html standards, as below :
                   SPRING (corresponding tag)
 <html:text property="abc">
 <form:input path="abc">
<html:select property="abc">
      <option value="one">
<form:select path="abc">
     <option value="one">
<input type="submit"> 
 <bean:message bundle="resource" key="abc">
<spring:message code="abc"> 
<html:checkbox property="abc">
<form:checkbox path="abc">
These are the mostly used tags. The list will go on...

Now next is the calling action from jsp. For this, we need to change form tag as below :

In Struts :

<html:form action="/addStudent" method="post">

In Spring :

<form:form method="POST" commandName="studentForm" name="studentForm" action="">

Here commandName is going to map with corresponding formbean for that jsp. Next we will see, how action is getting called with spring 3 annotations.  (Remeber that we have removed struts-config.xml, so we don't have any mapping against action mention here)

STEP 5 : Action class changes for struts to spring migration

 Now following changes need to be done in action classes for struts to spring migration using annotations :

Struts action
Spring action
(corresponding replacement)
extends Action
@controller annotation above class declaration
public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
               throws Exception {
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })

public ModelAndView execute (HttpServletRequest request,
  HttpServletResponse response,
 @Valid @ModelAttribute("studentForm")        studentForm studentForm,
  BindingResult bindingResult)
throws Exception {

Here, in @RequestMapping annotation, action is getting mapped to corresponding action mentioned in jsp.
And in @ModelAttribute annotation, form is getting mapped which should be same as that of mentioned in commandName in jsp.
forward = mapping.findForward(“success”);
modelMap.put("studentForm",   studentForm);
return (new ModelAndView(“success”, modelMap));

Imports tobe added in Spring application :

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;


STEP 6 : Validation changes for struts to spring migration

Step 6-1 : Changes in jsp

In Struts :
We add following line for instantiating actionErrors :
 ActionErrors actionErrors = (ActionErrors)request.getAttribute("org.apache.struts.action.ERROR");
This line will get removed in Spring application.
In Struts we use following tag for displaying error message :
<html:errors bundle="resource"/>
In Spring :
In Spring we use following tag for displaying error message :

<form:errors path="*" cssClass="error" />  
Here, in cssClass 'error', you can define css properties for error messages to display.

 Step 6-1 : Changes in Action class

In Struts :
Method signature as :
public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response)
  throws Exception {
For capturing errors :
   ActionErrors errors = new ActionErrors();
  ActionForward forward = new ActionForward();
   errors.add("name", new ActionMessage("id"));
In Spring :
Add following imports :
        import javax.validation.Valid; 
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.validation.BindingResult;
Create one instance of your formbean and aurowire it, as follow:
  public StudentForm studentFormValidator;
 public void setStudentFormValidator(
   StudentForm studentFormValidator) {
  this.studentFormValidator = studentFormValidator;
Method signature :
public ModelAndView execute(
   HttpServletRequest request,
   HttpServletResponse response,
   @Valid @ModelAttribute("StudentForm") StudentForm studentForm,
   BindingResult result) throws Exception {
For capturing errors, call a validate method at the start of business logic as :
 studentFormValidator.validate(studentForm, result, request);
Here, we need to create one validate method in form bean :
public void validate(Object object, Errors errors, HttpServletRequest request) {
Note : considering messages are stoared in
Here, we will get result of validation in 'result' parameter.
Now, you can proceed for business logic as :
if (!result.hasErrors()) {
//business logic goes here
forward = "failure";
modelMap.put("studentForm", studentForm);
return (new ModelAndView(“failure”, modelMap));
And and here we done with basic migration. Hope it will help you.

You may also like to read :


Addition or removal of element in list while iterating 
Java / J2ee interview questions for experience java developer


  1. Thanks for sharing such useful information :)
    We will surely use this approach... !!!

  2. Helpful post, please post your thoughts on a replacement for Structs ActionMessage when used to display success messages.

  3. can anybody tell how to convert the bean tags and html:link tags to spring

  4. It’s a nice blog posted by you. I was seeking for this type of blog that have a fresh and interesting content.

    Java spring developers

  5. Hi, great post.
    Where have you defined "success" and "failure" in the spring ModelAndView? Are the jsps renamed from welcome.jsp in Struts to success.jsp in Spring?

    1. hey thnkx for pointing it out.. in spring you have to actually pass jsp name instead of "success" or "failure" word. I have mentioned these words in blog, just for understanding purpose.

  6. <%@ taglib uri="" prefix="noac"%>

    Iam having the above tag in my jsp file. How can I change it

  7. Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

  8. Thanks For sharing this. Its looking good