{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner
java,annotations,struts,spring mvc,spring,tiles,mvc

Spring MVC and Struts Tiles example

One way to learn a new program language or a new framework is do a "Hello World" or a CRUD application to start to understand about the technology, lifecycles, dependencies, configurations and limitations. I had to learn about Spring MVC and Struts Tiles,  an old Struts lib which is now an independent project. So I developed a CRUD application to practice in order to understand how Spring MVC and Struts Tiles work.

The project is a CRUD of Mobile Models with two fields. I'll just explain about Spring MVC and Struts Tiles. The others parts of the application won't be the focus of this post.

The project connects via JDBC with a test infrastructure around the DAO and Controller layer, Maven to manage the project and Spring framework version 3.0.3.RELEASE.

I used Spring MVC to treat requests between the user interface and the controller to refer objects in a web form. It's like other MVC web frameworks to facilitate the development with the advantage to be totally integrated with Spring Container working with all advantages of Spring's container.

To start to configure it you must to map the servlet DispatcherServlet in web.xml file in your project. For example:

In deploy phase the servlet 'spring-mvc' DispatcherServlet will looks for spring-mvc-servlet.xml file ( [Servlet Name]-servlet.xml ) inside of WEB-INF. In the example above all requests will be treat by 'spring-mvc' servlet DispatcherServlet.

About the Controllers the Spring have specific types of controllers form-specific controllers, command-based controllers and etc. To define a class as controller just annotate with @Controller like in CelularController class example:
public class CelularController {
Classes anotated with @Controller will transform the class serves the role of a controller.
In this class CelularController has another annotation called @RequestMapping, where you can map the URL request to acess a specific method by "value" parameter. The parameter "method" is used to define the method request(GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE). In the classe CelularController there is an example:
@RequestMapping(value = "/cadastro/remove", method = RequestMethod.POST)
public String removerModelo(@RequestParam(value = "nome", required = true) String nome,
@ModelAttribute Celular celular, WebRequest request) throws Exception {
There are another annotation called @ModelAttribute. When you define an attribute with this annotation, the Spring will bind the parameters comes from the request(view) to the objects attributes.
The magic happens when you create a method and set @InitBinder annotation as:
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
binder.setValidator((org.springframework.validation.Validator) this.validator);

@InitBinder annotation identify a method which initialize the WebDataBinder class which is used to bind the attributes comes from form(view) to the object with @ModelAttribute annotation.

To Spring Container detect classes mapped with @Controller, @ModelAttribute or @InitBinder it's necessary define the package of this classes in spring-mvc-servlet.xml file. In the example:
<context:component-scan base-package="net.valdemarjr" scoped-proxy="targetClass" />
To config Struts Tiles integrate with Spring MVC is necessary define two beans in spring-mvc-servlet.xml file:
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"
p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" />

And the return String in your controller's methods mapped in tiles.xml file. For example in "processarFormulario". The method's return is "cadastro.formulario" which is mapped as:
<definition name="cadastro.formulario" extends="titulo">
	<put-attribute name="conteudo" value="/WEB-INF/jsp/cadastro/formulario.jsp" />
And the response will redirect to file "formulario.jsp".

It's the first time I use Spring MVC and Struts Tiles together and I liked it. If you want to see de source code, the project is in github and you can download, update and test.
Sorry about my english. If you have any question, contact me.
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks