BluePrints JSF Map Viewer 組件(<ui:mapViewer>)封裝 Google Maps API,讓 JSF 應用以聲明式方式顯示地標、信息氣球、縮放控制。本文以官方 Store Catalog 範例為背景,示範從點擊賣場地址連結到地圖頁面的完整串聯。

JSF Store UI 作為 JAX-WS 客戶端呼叫後端 EJB 3.0 服務,Map Viewer 組件獨立於業務邏輯,可移植至任何 JSF 專案(Seam、Spring、純 JPA 等)。
<h:commandLink action="#{MapBean.mapAction}"
value="#{item.item.address.street1}, #{item.item.address.city}, #{item.item.address.state}"/>
commandLink 送出 Action Event,action 指向 Session Scope Managed Bean MapBean。
<managed-bean>
<managed-bean-name>MapBean</managed-bean-name>
<managed-bean-class>sessionpagination.client.MapBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>item</managed-bean-name>
<managed-bean-class>sessionpagination.client.ItemController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<navigation-case>
<from-outcome>map</from-outcome>
<to-view-id>/item/map.jsp</to-view-id>
</navigation-case>
</navigation-rule>
public class MapBean {
private MapMarker mapMarker = new MapMarker();
private MapPoint mapPoint = new MapPoint();
private String location = "";
public MapMarker[] getLocations() { return new MapMarker[]{mapMarker}; }
public String mapAction() {
// 從 Session 獲取 ItemController,取得當前商品地址
ItemController ic = (ItemController) FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get("item");
Address addr = ic.getItem().getAddress();
location = addr.getStreet1() + ", " + addr.getCity() + ", "
+ addr.getState() + ", " + addr.getZip();
return findLocation(); // 解析經緯度並返回 "map" 導航
}
public String findLocation() {
GeoCoder geoCoder = new GeoCoder(); // 內部呼叫 Yahoo 地圖服務
GeoPoint[] points = geoCoder.geoCode(location);
mapMarker.setLatitude(points[0].getLatitude());
mapMarker.setLongitude(points[0].getLongitude());
mapMarker.setMarkup(points[0].toString());
mapPoint.setLatitude(points[0].getLatitude());
mapPoint.setLongitude(points[0].getLongitude());
return "map"; // 對應 faces-config.xml 導航 case
}
}
<%@taglib prefix="ui" uri="https://java.sun.com/blueprints/ui/14" %>
<ui:mapViewer id="mapViewerx"
center="#{MapBean.mapPoint}"
info="#{MapBean.mapMarker}"
markers="#{MapBean.locations}"
zoomLevel="4"
style="height: 500px; width: 700px"/>
屬性對應:
center:MapPoint 決定地圖中心經緯度info:MapMarker 提供信息氣球文字markers:MapMarker[] 標記多個地標(本例僅第一個)zoomLevel:初始縮放級別sessionpagination(含 EJB/Web 模組)與 sessionpagination-wsclient(JSF 客戶端)。sessionpagination(後端服務)。sessionpagination-wsclient(前端 JSF),瀏覽器開啟 https://localhost:8080/sessionpagination-wsclient/。Map Viewer 組件(bp-ui-14.jar)與 GeoCoder 組件解耦,可直接放入任何 JSF 專案的 WEB-INF/lib,在頁面引入 Taglib 即用,**無綁定特定後端架構**。