Skip to end of metadata
Go to start of metadata

The Spring MVC variant of Trails can be found on this branch: http://trails.inspring.nl/branches/trails-spring-mvc.

The main parts of the Spring MVC Version are:

  • org.trails.spring.mvc.TrailsMultiActionController
  • org.trails.spring.mvc.ObjectDataDescriptorList
  • org.trails.spring.mvc.TrailsServletRequestDataBinder
  • The JSTL views and property tags.

The TrailsMultiActionController.

For Spring controller we choose to extend the org.springframework.web.servlet.mvc.multiaction.MultiActionController as
we have several actions that perform similar or related logic. This class has a lot of protected methods which can be overridden if you want to extend it with your own customizations.

It also allows you to add custom Spring validators that are executed before saving an object, see http://www.springframework.org/docs/reference/validation.html for more information about the Spring validators. An example on how to add a custom validator; you add the following property to the definition of the defaultController in the trails-servlet.xml file:

The Hibernate ClassValidator's org.hibernate.validator.ClassValidator are explicitly called before saving. We did this so we have more control on when these Validators are called.

In the trails-servlet.xml file you can also set your paging size. This prevents that in case a Class has many instances all the instances are rendered on the screen. This is done by setting the pagingSize property to its desired value.

The ObjectDataDescriptorList.

This (Spring) bean is a variant of the org.trails.component.ObjectTable. It contains a List (rows) of ObjectDataDescriptor's which are a sort of instance counterpart of the IClassDescriptor. The ObjectDataDescriptorList is put in the Model, from the ModelAndView, and is used in the jsp's for rendering the instances.
An ObjectDataDescriptor contains the instance to be rendered and a List of PropertyDataDescriptor's (colums). A PropertyDataDescriptor is the counterpart of the IPropertyDescriptor. The PropertyDataDescriptor contains the value of the property to be rendered and the IPropertyDescriptor that contain the information on how to render the property (is it hidden, searchable etc.).

The TrailsServletRequestDataBinder

This (Spring) bean is used for binding and a bit of validating the form data. It is injected with the PersistenceService and the DescriptorService to retrieve information about the object and possible child objects being bound. Reflection is used to populate the bounded object.

The JSTL views and property tags.

JSTL views.

We used JSTL views, actually JSTL views with a small extension, in combination with tag files as view layer. There are currently four default jsp's:

  • list.jsp
    This jsp is used to show the list of instances.
  • edit.jsp
    This jsp is used for both editing and adding an instance.
  • search.jsp
    This jsp is used to enter you search criteria.
  • index.jsp
    This jsp is the home page.

It is possible to overwrite the default jsp used in the same way as it is done in the Tapestry version. Just change the name of your jsp, for instance editPerson.jsp and put it in the WEB-INF/jsp dir and it will be used.

The property tags.

For the different type of properties there are different tag files. This is done so the way a property is rendered can be changed somewhat easier than if all properties were in the same file. In all the jsp's the property.tag file is used. The property.tag file decides, based upon the IPropertyDescriptor in the PropertyDataDescriptor which of the following tag files should be used.

The tag files available are:

  • boolean-property.tag - Renders a boolean (TrailsProperty.isBoolean).
  • collection-property.tag - Renders a collection (TrailsProperty.isCollection)
  • date-property.tag - Renders a date (TrailsProperty.isDate)
  • numeric-property.tag - Renders a numeric value (TrailsProperty.isNumeric)
  • object-reference-property.tag - Renders a property that is an object reference (TrailsProperty.isObjectReference)
  • string-property.tag - Renders a String (TrailsProperty.isString).

If you want to render a property differently you could adjust these tag files. However adjusting one of the above tag files will effect all properties of that type.
So if you want to just change one property of a certain Class, you have to overwrite the default jsp and make a copy of the property.tag file into you own customized tag file. Then make your own (for instance) custom-string-property.tag and make sure that one is rendered in case of a String by adjusting your customized tag file.

Limitations (for now):

  • Only one date format is supported for now (dd-MM-yyyy).
  • Only bi-directional relations are supported.
  • Not all attributes of the PropertyDescriptor annotation are implemented yet.
  • As it is not as mature as Trails with Tapestry you will probably run into some bugs....

Example Recipe

The recipe example is slightly adjusted for this Trails variant. This is because only bi-directional relations are supported for now.

Labels