Weather
Source
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:pm="http://primefaces.org/mobile"> <f:view renderKitId="PRIMEFACES_MOBILE" /> <h:head> </h:head> <h:body> <pm:page id="main"> <pm:header title="Weather" swatch="b"> <p:button styleClass="ui-toolbar-header-button-right ui-button-inline" value="Settings" icon="ui-icon-gear" outcome="pm:settings" /> </pm:header> <pm:content> <h:form id="mainForm"> <pm:field> <p:outputLabel for="city" value="Select City:" /> <p:selectOneMenu id="city" value="#{weatherView.city}"> <f:selectItems value="#{weatherView.cities}" /> </p:selectOneMenu> </pm:field> <p:commandButton value="Get Forecast" update="display" actionListener="#{weatherView.retrieveConditions}"/> <p:outputPanel id="display" style="text-align:center"> <h:outputText value="#{weatherView.conditions}" escape="false" /> </p:outputPanel> </h:form> </pm:content> </pm:page> <pm:page id="settings"> <pm:header title="Settings" swatch="b"> <p:button styleClass="ui-toolbar-header-button-left ui-button-inline" value="Back" icon="ui-icon-arrow-l" outcome="pm:main" /> </pm:header> <pm:content> <h:form id="settingsForm"> <pm:field> <p:outputLabel for="unit" value="Unit:" /> <p:selectOneMenu id="unit" value="#{weatherView.unit}"> <f:selectItem itemLabel="Celcius" itemValue="c" /> <f:selectItem itemLabel="Fahrenheit" itemValue="f" /> </p:selectOneMenu> </pm:field> <p:commandButton value="Save" action="#{weatherView.saveSettings}"/> </h:form> </pm:content> </pm:page> </h:body> </html>
package org.primefaces.showcase.mobile; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.io.SyndFeedInput; import com.sun.syndication.io.XmlReader; import java.io.Serializable; import java.net.URL; import java.util.LinkedHashMap; import java.util.Map; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ManagedBean @ViewScoped public class WeatherView implements Serializable { private String conditions; private String city; private String unit = "c"; private Map<String,String> cities; private static final Logger logger = Logger.getLogger(WeatherView.class.getName()); @PostConstruct public void init() { cities = new LinkedHashMap<String, String>(); cities.put("Istanbul", "TUXX0014"); cities.put("Barcelona", "SPXX0015"); cities.put("London", "UKXX0085"); cities.put("New York", "USNY0996"); cities.put("Paris", "FRXX2071"); cities.put("Rome", "ITXX0067"); } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getConditions() { return conditions; } public void setConditions(String conditions) { this.conditions = conditions; } public String getUnit() { return unit; } public void setUnit(String unit) { this.unit = unit; } public Map<String, String> getCities() { return cities; } public void retrieveConditions() { try { URL feedSource = new URL("http://weather.yahooapis.com/forecastrss?p=" + city + "&u=" + unit); SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(new XmlReader(feedSource)); String value = ((SyndEntry) feed.getEntries().get(0)).getDescription().getValue(); conditions = value.split("<a href")[0]; } catch (Exception e) { logger.severe(e.getMessage()); conditions = "Unable to retrieve weather forecast at the moment."; } } public String saveSettings() { conditions = null; return "pm:main"; } }