WURFL introduce

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/

你可能感兴趣的:(jsp,xml,XHTML,mobile,firefox)