WURFL introduce

本文详细介绍了如何通过WURFL库实现设备识别,并根据识别结果为不同设备提供定制化的HTML格式。包括安装配置Firefox、WURFL插件、下载测试文件、配置Spring框架以及实现页面适配的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WURFL introduce

official website:
http://wurfl.sourceforge.net/

JAVA API:
http://wurfl.sourceforge.net/njava/

file download url:
http://sourceforge.net/projects/wurfl/files/WURFL%20Java%20API/

1.the Wireless Universal Resource File
the WURFL is an XML configuration file which contains information about capabilities and features of many mobile devices.

as much information as we can about all the existing mobile devices that access WAP pages.

2. WURFL NEW API
we need install firefox and plugin User Agent Switcher to test WURFL
https://addons.mozilla.org/zh-CN/firefox/addon/59/

download the file wurfl-helloworld-1.0.1-rc3-ant.zip and build the hello world demo

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

first, we need the web.xml like this, because I choose the spring way:
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.sillycat.easyturbine.servlets.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
so, I import the wurfl-context.xml file from main-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"
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
">
<!-- WURFLModel -->
<bean id="wurflModel" class="net.sourceforge.wurfl.core.resource.DefaultWURFLModel">
<constructor-arg index="0">
<bean class="net.sourceforge.wurfl.core.resource.SpringXMLResource">
<constructor-arg index="0" value="/WEB-INF/wurfl-2.0.18.zip" />
</bean>
</constructor-arg>
<constructor-arg index="1">
<bean class="net.sourceforge.wurfl.core.resource.WURFLResources">
<constructor-arg index="0">
<list>
<bean class="net.sourceforge.wurfl.core.resource.SpringXMLResource">
<constructor-arg index="0"
value="/WEB-INF/web_browsers_patch.xml" />
</bean>
</list>
</constructor-arg>
</bean>
</constructor-arg>
</bean>

<!-- Handlers, Filters and Matchers -->
<bean id="matcherManager"
class="net.sourceforge.wurfl.core.handlers.matchers.MatcherManager">
<constructor-arg ref="wurflModel" />
</bean>

<!-- Device -->
<bean id="deviceProvider" class="net.sourceforge.wurfl.core.DefaultDeviceProvider">
<constructor-arg ref="wurflModel" />
</bean>

<!-- Service -->
<bean id="wurflService" class="net.sourceforge.wurfl.core.DefaultWURFLService">
<constructor-arg ref="matcherManager" />
<constructor-arg ref="deviceProvider" />
</bean>

<!-- WURFL Manager -->
<bean id="wurflManager" class="net.sourceforge.wurfl.core.DefaultWURFLManager">
<constructor-arg ref="wurflService" />
</bean>

<!-- WURFL Utils -->
<bean id="wurflUtils" class="net.sourceforge.wurfl.core.WURFLUtils">
<constructor-arg ref="wurflModel" />
</bean>

<!-- WURFL Holder -->
<bean id="wurflHolder" class="net.sourceforge.wurfl.core.DefaultWURFLHolder">
<constructor-arg ref="wurflManager" />
<constructor-arg ref="wurflUtils" />
</bean>

<!-- Put WURFL Holder in ServletContext -->
<bean
class="org.springframework.web.context.support.ServletContextAttributeExporter">
<property name="attributes">
<map>
<entry key="net.sourceforge.wurfl.core.WURFLHolder" value-ref="wurflHolder" />
</map>
</property>
</bean>
</beans>

so, I copy the file wurfl-2.0.18.zip and web_browsers_patch.xml to WEB-INF。

copy the java file HelloWorld.java to my web app and make a small change that we need to put the jsp files in WebContent:
request.getRequestDispatcher("jsp/" + jspView).forward(request,response);
to check up this file,you can see how wurfl works,get the device information from xml according to the user agent, and decide which html format,or rather say,which style of page we will return to response:

WURFLHolder wurflHolder = (WURFLHolder) getServletContext()
.getAttribute("net.sourceforge.wurfl.core.WURFLHolder");
WURFLManager wurfl = wurflHolder.getWURFLManager();
Device device = wurfl.getDeviceForRequest(request);
log.debug("Device: " + device.getId());
log.debug("Capability: " + device.getCapability("preferred_markup"));
MarkUp markUp = device.getMarkUp();
String jspView = null;
if (MarkUp.XHTML_ADVANCED.equals(markUp)) {
jspView = XHTML_ADV;
} else if (MarkUp.XHTML_SIMPLE.equals(markUp)) {
jspView = XHTML_SIMPLE;
} else if (MarkUp.CHTML.equals(markUp)) {
jspView = CHTML;
} else if (MarkUp.WML.equals(markUp)) {
jspView = WML;
}

and then I copy the jsp file from helloworld.war to my WebContent and add something,for example chtml.jsp:
<%-- $Id: chtml.jsp 384 2009-11-17 14:48:24Z filippo.deluca $ --%>
<%@page import="net.sourceforge.wurfl.core.Device"%>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello From CHTML</h1>
<p>ContentType: <%=request.getAttribute("contentType") %></p>
<p>Device: <%=((Device)request.getAttribute("device")).getId() %></p>
<p>UserAgent: <%=((Device)request.getAttribute("device")).getUserAgent() %></p>
</body>
</html>

then we use firefox (Tools----->Default User Agent-I choose iphone here ------->Iphone.30) to visit the website http://localhost:8080/easyturbine/index.htm
you will get the information from the page:
Hello From XHTML ADVANCED

ContentType: application/xhtml+xml

Device: apple_iphone_ver3_sub7a341_enus

UserAgent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16

ok, that is wurfl works. And I you want to see more in details and you can visit http://wurfl.sourceforge.net/njava/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值