Sign in to edit and +1 items.
Login required
Download

SpringMVC Plugin

(0)
Author(s) Burt Beckwith
Current Release 0.1   (3 years ago)
Grails Version ?
Tags other frameworks 
Dependency
compile ":springmvc:0.1"
Last updated by ignatony 1 year ago
Last updated by burtbeckwith 3 years ago
The SpringMVC plugin allows you to use controllers written in Java using SpringMVC and use JSPs to render output.


Groovy is slower than Java and sometimes dramatically slower. Realistically, this has little impact on a web application since response time is affected more by the database and network latency, so as long as the slowdown isn't too dramatic, the benefits of Groovy and Grails far outweigh these concerns. But for the case where you're concerned about controller performance, or to help migrate a traditional Spring/Hibernate to Grails, this plugin allows you to define or reuse Java controllers.

Installation

To install the plugin in your application just run the 'install-plugin' Grails script, e.g.

grails install-plugin springmvc

The install script will create a web-app/WEB-INF/jsp folder if it doesn't exist, and copy the sample error.jsp there if it doesn't exist. It also creates an empty web-app/WEB-INF/SpringMVC-servlet.xml - the file has to exist but all beans are defined by the plugin or in the app's resources.groovy and/or resources.xml.

Configuration

There are a few configuration options available - all are optional. They're defined in a springmvc block in Config.groovy:

NameDefault ValueDescription
urlSuffix'action', i.e. .actionthe URL pattern for Spring MVC controller URLs
exceptionMappingsnoneMap with exception class names as keys and JSP names as values
defaultErrorView'error', i.e. web-app/WEB-INF/jsp/error.jspthe default error page if there's no corresponding mapping for the exception class
interceptorsnonebean names of HandlerInterceptors to apply to Spring MVC URLs

So a configuration block that defines a single interceptor, uses .action for URLs, and defines a default error JSP and two exeption-specific exception mappings (note that the values for urlSuffix and defaultErrorView are redundant since they're the default values) would be:

springmvc {
   interceptors = ['loggingInterceptor']
   exceptionMappings = ['java.io.IOException': 'ioexception']
   exceptionMappings = ['com.myapp.MyException': 'myex']
   defaultErrorView = 'error'
   urlSuffix = 'action'
}

Application Beans

Unlike in Grails, there's no automatic URL mapping. To connect a controller to a URL you define a Spring bean (in resources.groovy or resources.xml) whose name is the url and the bean class is the controller, e.g.:

'/test.action'(com.burtbeckwith.mvctest.controller.TestController) {
      cacheSeconds = 0
      bookService = ref('bookService')
   }

This will map http://localhost:8080/yourapp/test.action to TestController. Setting cacheSeconds to 0 instructs Spring to send headers to disable caching. And 'bookService' is a dependency injection for BookService to access Book domain instances. The controller returns 'books' as its view name - this is prefixed by '/WEB-INF/jsp/' and suffixed by '.jsp' to define the JSP that will render the response (i.e. '/WEB-INF/jsp/books.jsp').

You can also define HandlerInterceptors in resources.groovy. They should extend HandlerInterceptorAdapter or implement HandlerInterceptor directly. Add their bean names to the 'interceptors' list and Spring MVC requests will be intercepted. Note that these interceptors are in addition to the standard Hibernate OSIV interceptor and a locale change interceptor.

For example:

loggingInterceptor(com.burtbeckwith.mvctest.interceptor.LoggingInterceptor)

Sample App

You can download a sample app here. It's pretty simple - it has a single domain class, and a single Grails controller and a Spring MVC controller (to test that both work in the same app). There's a Grails service to access domain instances that implements a Java interface so it's callable by the MVC controller. There's also a sample interceptor (it just logs requests) and a sample JSP 2.0 tag file, date.tag.

The app creates six books in BootStrap - you can see the data by going to http://localhost:8080/mvctest/test.action. Go to http://localhost:8080/mvctest/regular to access a regular Grails controller.

Author

Burt Beckwith burt@burtbeckwith.com

Please report any issues to the Grails User mailing list and/or write up an issue in JIRA at http://jira.codehaus.org/browse/GRAILSPLUGINS under the Grails-SpringMVC component.

History

  • September 7, 2008
    • released initial version 0.1
See this blog post and this post for the original discussion about the plugin.
Last updated by admin 3 years ago
Last updated by admin 3 years ago