Mobile Spring-Based Web Apps for Everyone
Step 3 – Add the Spring Magic
Now that we have JSP pages, a mobile stylesheet and sitemesh configured, we need to configure Spring to serve up our mobile pages for the same Request Mappings that are used for the MVC and iPhone application. We’re almost done, this is the easy part.
Open the TaskManager-generated-web-context.xml file from your project’s resources directory. Near the bottom is an iPhoneUserAgentViewResolver bean. That view resolver is what is directing the iPhone browsers to the pages under WEB-INF/iphone. Once those pages are served up, SiteMesh kicks in and decorates the files with the iphone decorators stored in the sitemesh folders. Not surprisingly, we need to direct our Android and Palm users to our mobile pages using a Spring View Resolver configuration. The TaskManager-generated-web-context.xml’s ViewResolvers look like this after our original MVC and iPhone scaffolding.
If you look at the class attribute of the iPhoneUserAgentViewResolver, it uses a UserAgentViewResolver from the org.skyway.spring.util package. UserAgentViewResolver is a very simple Spring View Resolver provided by MyEclipse for Spring that does a simple match against the user-agent header from the request. The class can be found in the skyway-spring-utils.jar that is added to your project the first time you scaffold. The UserAgentViewResolver can be used to resolve any user-agent string to any sub-directory in the WEB-INF folder.
To configure the view resolver to handle Android and Palm, Paste these two new UserAgentViewResolver bean configurations into the file just after the iPhoneUserAgentViewResolver. These two view resolver configurations are just like the iPhone configuration except:
- The agentSubstring is either “Android” or “webOS”.
- The prefix is /WEB-INF/mobile/
After adding your two new View Resolver bean configs, your generated web context should contain this (along with some other stuff):
Whew, that was a lot but we’re done! Flip to the next page and reap the rewards of your labor.