[GIS] OpenLayers – Show a WMS Layer from GeoServer

geoserverjavascriptopenlayers-2

I'm trying to acesses with OpenLayers code an example of the embedded GeoServer map (the map of tasmania), which acts as a local server on my machine. However, every time I open the .html, the map appears blank.

Below is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <link rel="stylesheet" href="/theme/default/style.css" type="text/css" />
    <style type="text/css">
        body {
            margin: 1em;
        }
        #map {
            width: 500px;
            height: 375px;
            border: 1px solid black;
        }
    </style>
  <script src="openlayers/OpenLayers.js"></script>
  <script type="text/javascript">
        var lon = 5;
        var lat = 40;
        var zoom = 5;
        var map, layer;

        function init()
        {
            map = new OpenLayers.Map( 'map' );
            layer = new OpenLayers.Layer.WMS("State", 
                "http://localhost:8080/geoserver/wms",
                {
                    workspace: 'topp',
                    layers: 'tasmania_state_boundaries',
                    format: 'image/png'
                }
            );

            map.addLayer(layer);

            map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
            map.addControl(new OpenLayers.Control.LayerSwitcher() );
        }
    </script>
  </head>
<body onload="init()">
    <h1 id="title">WMS Example</h1>

    <div id="tags">
        wms, layer, singletile
    </div>
    <p id="shortdesc">
        Shows the basic use of openlayers using a WMS layer
    </p>

    <div id="map" class="smallmap"></div>

    <div id="docs">
        <p>This is an example of how to add an WMS layer to the OpenLayers
        window. The images are tiled in this instance. If you wanted to not use
        a tiled WMS, "singleTile" option to true like shown in this example.</p>
    </div>
  </body>
</html>

Did I forget any detail? The form of access to GeoServer is correct?

Best Answer

I found the answer! :D

Below is the javascript code:

<script type="text/javascript">
        var map, layer;

        function init()
        {
            map = new OpenLayers.Map( 'map' );
            layer = new OpenLayers.Layer.WMS("State", 
                "http://localhost:8080/geoserver/topp/wms?",
                {
                    workspace: 'topp',
                    layers: 'tasmania_state_boundaries', 
                    format: 'image/png'
                },
                {
                    buffer: 0,
                    ratio: 1.9,
                    displayOutsideMaxExtent: true
                }
            );

            map.addLayer(layer);
            map.addControl(new OpenLayers.Control.LayerSwitcher());
            map.zoomToExtent(
                new OpenLayers.Bounds(145.51045,-44.0,149.0,-40.5)
            );
        }
    </script>

I hope this can help people with the same question as me. :)