Brazilian, developer by profession, gamer for fun, amateour athletic and cyclist promising. Passionate for technology and inovation, Agile methodologies evangelist as SCRUM and Kanban, make continuous process improvements and sharing the knowledge. Especialist in Enterprise Java, web application and Open Source techonologies since 2007. Valdemar has posted 6 posts at DZone. View Full User Profile

Spring MVC and Struts Tiles example

02.21.2011
| 12344 views |
  • submit to reddit

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:

<web-app>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
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:
@Controller
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:
    @InitBinder
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" />
</definition>
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.
Published at DZone with permission of its author, Valdemar Júnior.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Julio Aguilar replied on Wed, 2011/03/16 - 6:20pm

The configuration for Tiles is missing and the last code about the mapping of "cadastro.formulario" is not correct.

Hantsy Bai replied on Thu, 2011/03/17 - 1:53am

The struts tiles(in struts 1.x) now is a standalone project on Apahce, http://tiles.apache.org.

 

Valdemar Júnior replied on Sun, 2011/03/20 - 9:41pm in response to: Julio Aguilar

Hi Julio, The mistake has been corrected. Thank you for your observation.

Carla Brian replied on Thu, 2012/06/14 - 6:13pm

I want to master this. I am new to this actually. I will search for more tutorials about this. - Joe Aldeguer

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.