0

我制作了一个 jsf 项目,并从http://docs.openlayers.org/library/introduction.html获得了以下示例,但作为 xhtml 文件,它不会运行,而作为 html 文件,它会运行。如何使用 jsf 和 .xhtml 运行它。它虽然与 html 一起运行。

     <?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <script src="http://openlayers.org/api/OpenLayers.js"></script>

    </h:head>
    <h:body>
        <div style="width:100%; height:100%" id="map"></div>
        <script defer="defer" type="text/javascript">
        var map = new OpenLayers.Map('map');
        var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
            "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
        var dm_wms = new OpenLayers.Layer.WMS(
            "Canadian Data",
            "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap",
            {
                layers: "bathymetry,land_fn,park,drain_fn,drainage," +
                        "prov_bound,fedlimit,rail,road,popplace",
                transparent: "true",
                format: "image/png"
            },
            {isBaseLayer: false}
        );
        map.addLayers([wms, dm_wms]);
        map.zoomToMaxExtent();
      </script>
    </h:body>
</html>

使用 firebug 在浏览器上的结果:

<head>
<body>
<div style="width: 100%; height: 100%; " id="map" class="olMap">
<div id="OpenLayers.Map_2_OpenLayers_ViewPort" style="position: relative; overflow-x: hidden; overflow-y: hidden; width: 100%; height: 100%; " class="olMapViewport olControlDragPanActive olControlZoomBoxActive olControlPinchZoomActive olControlNavigationActive">
<div id="OpenLayers.Map_2_events" style="position: absolute; width: 100%; height: 100%; z-index: 999; ">
<div id="OpenLayers.Control.PanZoom_5" style="position: absolute; left: 4px; top: 4px; z-index: 1004; " class="olControlPanZoom olControlNoSelect" unselectable="on">
<div id="OpenLayers.Control.ArgParser_6" style="position: absolute; z-index: 1004; " class="olControlArgParser olControlNoSelect" unselectable="on"/>
<div id="OpenLayers.Control.Attribution_7" style="position: absolute; z-index: 1004; " class="olControlAttribution olControlNoSelect" unselectable="on"/>
</div>
</div>
<script defer="defer" type="text/javascript">

var map = new OpenLayers.Map('map');
        var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
            "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
        var dm_wms = new OpenLayers.Layer.WMS(
            "Canadian Data",
            "http://www2.dmsolutions.ca/cgi-bin/mswms_gmap",
            {
                layers: "bathymetry,land_fn,park,drain_fn,drainage," +
                        "prov_bound,fedlimit,rail,road,popplace",
                transparent: "true",
                format: "image/png"
            },
            {isBaseLayer: false}
        );
        map.addLayers([wms, dm_wms]);
        map.zoomToMaxExtent();
</script>
</body>
<script src="chrome-extension://bmagokdooijbeehmkpknfglimnifench/googleChrome.js"/>
</html>
4

3 回答 3

1

您正在内联执行 JavaScript 代码。这意味着当网络浏览器遇到特定的代码行时,JavaScript 代码会立即执行。那时,<div id="map">HTML DOM 树中还不存在 !浏览器即从上到下解析和执行 HTML/JS 代码。

在创建并添加到 HTML DOM 树之后,您需要执行该 JavaScript 代码。<div id="map">您可以通过以下方式实现它:

  1. <script>标记中的块移动<div id="map">:

    <h:head>
        <script src="http://openlayers.org/api/OpenLayers.js"></script>
    </h:head>
    <body>
        <div style="width:100%; height:100%" id="map"></div>
        <script type="text/javascript">
            var map = new OpenLayers.Map('map');
            var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
            "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
            map.addLayer(wms);
            map.zoomToMaxExtent();
        </script>
    </body>
    
  2. 仅当浏览器完成创建 HTML DOM 树时才用于window.onload执行代码。

    <h:head>
        <script src="http://openlayers.org/api/OpenLayers.js"></script>
        <script type="text/javascript">
            window.onload = function() {
                var map = new OpenLayers.Map('map');
                var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                "http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'} );
                map.addLayer(wms);
                map.zoomToMaxExtent();
            };
        </script>
    </h:head>
    

此问题与 JSF 或 XHTML 无关。它只是基本的 HTML/JS。

于 2012-05-01T13:12:14.383 回答
0

我遇到了同样的问题,但我的扩展名是 .jspx。我确实喜欢这个尝试一下。

<html 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"  
    xmlns:h="http://java.sun.com/jsf/html"  
    xmlns:ui="http://java.sun.com/jsf/facelets"  
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">  

<head>
<script src="http://piturralhpxp:1983/geoserver/openlayers/OpenLayers.js" type="text/javascript"></script>
        <script type="text/javascript">
     //<![CDATA[
          var map;
           etc ....
            //]]>
        </script>
        </head>
        <f:view>        
        <body onload="init()">

        </body>
</f:view>
</html>
于 2012-05-01T10:43:58.513 回答
0

@BalusC 是对的。似乎是 OpenLayers 中的一个错误。添加 <!DOCTYPE html> 声明时,JSF 页面崩溃。

我删除了它,无论如何它工作得很好。我知道这不是最佳做法,但这是我发现完成这项工作的唯一方法。试试看。

于 2012-05-29T14:35:08.403 回答