`
sillycat
  • 浏览: 2490516 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android Framework(III)Spring-mobile and wurfl with Groovy Controller

阅读更多
Android Framework(III)Spring-mobile and wurfl with Groovy Controller

1. backgroup
Get the spring-mobile source codes
>git clone --recursive git://github.com/SpringSource/spring-mobile.git spring-mobile

Get the wurfl files
wurfl zip file: http://sourceforge.net/projects/wurfl/files/WURFL/

wurfl patch file:  http://wurfl.sourceforge.net/web_browsers_patch.xml

2. enable the jar files repository
add 3 more respositories to my ivysettings.xml:
<url name="office4">
<artifact pattern="http://maven.springframework.org/snapshot/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
<url name="office5">
<artifact pattern="http://maven.springframework.org/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>
<url name="office6">
<artifact pattern="http://maven.springframework.org/milestone/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
</url>

add some jar files configuration to ivy.xml:
<!-- spring device -->
<dependency org="org/springframework/mobile" name="spring-mobile-device" rev="1.0.0.M3" />
<!-- wurlf -->
<dependency org="net/sourceforge/wurfl" name="wurfl" rev="1.2.2" />
<!--  velocity -->
<dependency org="org/apache/velocity" name="velocity" rev="1.7" />
<dependency org="org/apache/velocity" name="velocity-tools" rev="2.0" />

3. Spring configuration changes
I changed my core-context.xml to add wurfl and spring mobile part.
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
            <list>
                <ref bean="deviceResolverHandlerInteceptor"/>
                <ref bean="sitePreferenceHandlerInterceptor" />
            </list>
        </property>
</bean>

<bean id="handlerAdapter" class="com.sillycat.easygroovyplugin.servlet.proxy.ProxyAwareAnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
<property name="customArgumentResolvers">
<list>
<ref bean="deviceWebArgumentResolver" />
                <ref bean="sitePreferenceWebArgumentResolver" />
</list>
</property>
</bean>

<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="resourceLoaderPath" value="${velocity.file.path}/template/" />
<property name="velocityProperties">
<props>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
<prop key="contentType">text/html;charset=UTF-8</prop>
</props>
</property>
</bean>
<bean id="velocityViewResolver"
      class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
    <property name="layoutUrl" value="layout/layout.vm"/>
    <property name="cache" value="false" />
<property name="suffix" value=".vm" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
</bean>

I deal with wurfl in my groovy controller, and I add the velocity resolver to my controller.
Here comes the wurfl-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:device="http://www.springframework.org/schema/mobile/device"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mobile/device
    http://www.springframework.org/schema/mobile/device/spring-mobile-device-1.0.xsd
">
<bean id="deviceWebArgumentResolver" class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
<bean id="sitePreferenceWebArgumentResolver" class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
<bean id="sitePreferenceHandlerInterceptor" class="org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor" />
<bean id="deviceResolverHandlerInteceptor" class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor">
<constructor-arg>
<device:wurfl-device-resolver root-location="file:${wurfl.zip.file}"
                              patch-locations="file:${wurfl.patch.file}" />
</constructor-arg>
</bean>
</beans>

Some settings in spring configuration file is in properties, config.properties:
##########################################
# wurfl configuration
##########################################
wurfl.zip.file=/home/luohua/work/easymarket/conf/wurfl/wurfl-2.0.28.zip
wurfl.patch.file=/home/luohua/work/easymarket/conf/wurfl/web_browsers_patch.xml

################################################
# velocity path
################################################
velocity.file.path=file://home/luohua/work/easymarket

4. The Groovy Controller
package com.sillycat.easymarket.controller;
import org.springframework.stereotype.Controller;
import org.springframework.mobile.device.site.SitePreference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {

@RequestMapping("/home")
public ModelAndView home(SitePreference sitePreference) {
ModelAndView view = null;
if (sitePreference == SitePreference.MOBILE) {
println sitePreference.isMobile();
println "home-mobile";
view = new ModelAndView("home_mobile");
} else {
println sitePreference.isMobile();
println "home";
view = new ModelAndView("home");
}
return view;
}
}

And the view of page home.vm:
#set($layout = "layout/layout.vm")

<h3>Device Information</h3>
<p>Your browser:</p>
<ul>
<li>${currentDevice.userAgent}</li>
</ul>
<p>is capable of:</p>
<ul>
${currentDevice.capabilities}
</ul>

<p>and has a preferred markup grade of:</p>
<ul>
<li>${currentDevice.markUp}</li>
</ul>

references:
http://www.springsource.org/spring-mobile
http://static.springsource.org/spring-mobile/docs/1.0.x/reference/htmlsingle/
http://www.abdn.ac.uk/~csc228/teaching/CS5302/practicals/practical_spring_wurfl.shtml

http://hi.baidu.com/hn_why/blog/item/442a171ab3ee3ce41ad57696.html


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics