将ManagedBean字段作为实体属性

时间:2017-02-13 20:45:59

标签: java spring jsf google-maps-api-3 primefaces

我在从managedBean传递地理位置坐标时遇到问题,要将它们保存在数据库新实体中。我正在使用Hibernate创建数据库单元,但无法传递这两个属性。

这是我的前端:

<script type="text/javascript">
    function geocodeEvent() {
        PF('geoMap').geocode(document.getElementById('address').value);
    }
</script>

<h3 style="margin-top:0">Geocode</h3>
<h:panelGrid columns="3" style="margin-bottom:10px" cellpadding="5">
    <p:outputLabel for="address" value="Address:" />
    <p:inputText id="address" />
    <p:commandButton value="Geocode" icon="ui-icon-search" onclick="geocode()" type="button" />
</h:panelGrid>

<!-- This is easy... -->
<p:inputText id="entityName" value="#{entity.name}" required="true" label="Entity Name" title="Enter Entity Name" />

<p:gmap id="geoGmap" widgetVar="geoMap" center="#{entityGeocodeView.centerGeoMap}" zoom="2" type="ROADMAP" model="#{entityGeocodeView.geoModel}" style="width:100%;height:400px">
    <p:ajax event="geocode" listener="#{entityGeocodeView.onGeocode}" update="@this entityLongitude entityLatitude" />
</p:gmap>

<!-- ...and here is the problem -->
<!-- Values from there... -->
<p:inputText id="thisValueLongitude" value="#{entityGeocodeView.longitude}"/?
<p:inputText id="thisValueLatitude" value="#{entityGeocodeView.latitude}"/>

<!-- ...I want to pass for set entity attributes when submit entity for saving to database -->
<p:inputText id="entityLongitude" value="#{entity.locationLongitude}"/>
<p:inputText id="entityLatitude" value="#{entity.locationLatitude}"/>

支持Bean:

@ManagedBean
@ViewScoped
public class EntityGeocodeView {

    private MapModel geoModel;
    private String centerGeoMap = "41.850033, -87.6500523";
    private double latitude;
    private double longitude;

    @PostConstruct
    public void init() {
        geoModel = new DefaultMapModel();
    }

    public void onGeocode(GeocodeEvent event) {
        List<GeocodeResult> results = event.getResults();

        if (results != null && !results.isEmpty()) {
            LatLng center = results.get(0).getLatLng();
            centerGeoMap = center.getLat() + "," + center.getLng();

            for (int i = 0; i < results.size(); i++) {
                GeocodeResult result = results.get(i);
                Marker currentMarker = new Marker(result.getLatLng(),    result.getAddress());
                geoModel.addOverlay(currentMarker);
                latitude = currentMarker.getLatlng().getLat();
                longitude = currentMarker.getLatlng().getLng();
            }
        }
    }

    public MapModel getGeoModel() {
        return geoModel;
    }

    public void setGeoModel(MapModel geoModel) {
        this.geoModel = geoModel;
    }

    public String getCenterGeoMap() {
        return centerGeoMap;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
}

1 个答案:

答案 0 :(得分:0)

好的,明白了。我在谷歌地图ajax完成时使用javascript解决了它。

这是一个解决方案:

<script type="text/javascript">
    function changeValues() {
        document.getElementById("entityLatitude").value = document.getElementById("thisValueLatitude").value;
        document.getElementById("entityLongitude").value = document.getElementById("thisValueLongitude").value;
    }
</script>

<p:gmap id="geoGmap" widgetVar="geoMap" center="#{entityGeocodeView.centerGeoMap}" zoom="2" type="ROADMAP" model="#{entityGeocodeView.geoModel}" style="width:100%;height:400px">
    <p:ajax event="geocode" listener="#{entityGeocodeView.onGeocode}" update="@this thisValueLongitude thisValueLatitude" oncomplete="changeValues()"/>
</p:gmap>