spring mobile框架是一个很小的,用于获取设备信息,然后跳转到不同视图的框架。
集成方式
<filter>
<filter-name>sitePreferenceRequestFilter</filter-name>
<filter-class>org.springframework.mobile.device.site.SitePreferenceRequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitePreferenceRequestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
接下来我们看它的源码,这里只分析过滤器这一种方式,还有maven集成方式和拦截器方式,这里不做描述。
public class SitePreferenceRequestFilter extends OncePerRequestFilter {
private final SitePreferenceHandler sitePreferenceHandler;
public SitePreferenceRequestFilter() {
this(new StandardSitePreferenceHandler(newCookieSitePreferenceRepository()));
}
public SitePreferenceRequestFilter(SitePreferenceHandler sitePreferenceHandler) {
this.sitePreferenceHandler = sitePreferenceHandler;
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {
sitePreferenceHandler.handleSitePreference(request,response);
filterChain.doFilter(request, response);
}
我们重点看sitePreferenceHandler.handleSitePreference(request, response)这个方法。
public SitePreference handleSitePreference(HttpServletRequest request, HttpServletResponse response) {
SitePreference preference =getSitePreferenceQueryParameter(request);
if (preference != null) {
sitePreferenceRepository.saveSitePreference(preference, request, response);
} else {
preference = sitePreferenceRepository.loadSitePreference(request);
}
if (preference == null) {
preference = getDefaultSitePreferenceForDevice(DeviceUtils.getCurrentDevice(request));
}
if (preference != null) {
request.setAttribute(CURRENT_SITE_PREFERENCE_ATTRIBUTE, preference);
}
return preference;
}
首先获取请求头里的信息,返回结果为preference这个枚举,这个枚举有三种:
/**
* The user prefers the 'normal' site.
*/
NORMAL {
public boolean isNormal() {
return true;
}
},
/**
* The user prefers the 'mobile' site.
*/
MOBILE {
public boolean isMobile() {
return true;
}
},
/**
* The user prefers the 'tablet' site.
*/
TABLET {
public boolean isTablet() {
return true;
}
};
分别是电脑,手机,平板。
如果获取到了,就调用sitePreferenceRepository.saveSitePreference(preference, request, response)方法,放入cookie中。
如果为空,就直接从cookie里获取。
之后再放到request的attribute中,该属性名称为“currentSitePreference”。
那么接下来我们可以测试一下:
首先是移动端:
Object attribute =request.getAttribute("currentSitePreference");
System.out.println(attribute);
打印结果为:MOBILE
再看PC端:
打印结果为:NORMAL
最终,可以实现一套接口,两套页面,结构如图所示:
- [x] temple 源目录
- [x] member 会员视图
- [x] mobile 手机视图
- [x] member 手机下会员视图
最后贴上springmvc集成spring mobile的配置:
<mvc:argument-resolvers>
<bean class="org.springframework.mobile.device.DeviceWebArgumentResolver" />
<bean class="org.springframework.mobile.device.site.SitePreferenceWebArgumentResolver" />
<bean class="net.shopxx.security.CurrentUserMethodArgumentResolver" />
<bean class="net.shopxx.security.CurrentCartMethodArgumentResolver" />
<bean class="net.shopxx.audit.AuditLogMethodArgumentResolver" />
</mvc:argument-resolvers>
<bean id="viewResolver" class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">
<constructor-arg>
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="${html_content_type}" />
<property name="suffix" value="${template.suffix}" />
</bean>
</constructor-arg>
<property name="mobilePrefix" value="mobile/" />
<property name="tabletPrefix" value="tablet/" />
<property name="enableFallback" value="true" />
</bean>