DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Spring MVC and Struts Tiles example

Spring MVC and Struts Tiles example

Valdemar Júnior user avatar by
Valdemar Júnior
·
Feb. 21, 11 · Java Zone · Interview
Like (0)
Save
Tweet
18.68K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Hash, Salt, and Verify Passwords in NodeJS, Python, Golang, and Java
  • Product Owner Anti-Patterns
  • Choosing Between GraphQL Vs REST
  • Synchronization Methods for Many-To-Many Associations

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo