Browse DevX
Sign up for e-mail newsletters from DevX


Spring Your Struts Apps Ahead: The Struts-to-Spring Migration : Page 3

Find out what it takes to migrate Struts applications to Spring MVC. You'll learn not only how to transform Struts applications into Spring MVC applications, but also the logical mapping between the two frameworks and how key Struts concepts relate to Spring MVC concepts.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Error and Validation Messages
More good news! Spring recognizes Struts message bundles in an identical format. In order to reuse your existing Message resources within Spring MVC, you just configure it as messageSource in the Spring MVC configuration file as follows:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename"> <value>resources.ApplicationResources</value> </property> </bean>

Also, you would need to use it, like your Controllers, as a messageSource property in your Controller implementations. No other changes are required.

Dispatcher Servlet
Spring MVC has its own version of the Request Processor/Action Servlet. It is DispatcherServlet, which is mapped to a group of URL expressions. To understand the concept of the Dispatcher Servlet, look at how Controllers are configured in Spring MVC.

Configuration Files
As a Struts user, you are used to having at least one struts-config.xml file (or more if you are using modules) that holds all the forwards, action mappings, form definitions, and plug-in declarations. In Spring MVC, all the Web application-related controller declarations are configured as Spring Beans. One or more Dispatcher Controllers dispatch all requests for the Web resources to the appropriate Controllers. For instance, if you want to remap your ".do" application into a Spring MVC application, you register the following servlet mapping in the web.xml of your application (I am not actually recommending that you use .do as an extension. Leave “.do” as a Struts-only convention.):

<servlet> <servlet-name>applicationDispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> ... <servlet-mapping> <servlet-name>applicationDispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

Now you have a Spring configuration file (applicationDispatcher-servlet.xml) with your Spring MVC Controller declarations. Note the “-servlet.xml” suffix for the applicationDispatcher file. It is a Spring MVC convention that enables DispatcherServlet to auto-load Spring MVC mapping files.

Mapping Web actions to the appropriate controllers in Spring MVC is quite easy. It is done in the same "wiring" fashion as the rest of the Spring application. The following example shows how to forward URL expression /showCatalog.do into a Controller showCatalog:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/showCatalog.do">showCatalog</prop> </props> </property> </bean>

The Controller showCatalog would be configured as another bean implemented by a class that implements Controller interfaces. In this example, showCatalog is nothing more than a simple forwarding controller that forwards URL requests to a ModelAndView component named catalog:

<bean id="showCatalog" name="showCatalog" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <property name="viewName" value="catalog"/> </bean>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date