我有一个 bean,用于显示拖动标记的更新位置。这是我的代码:
@SessionScoped
@ManagedBean
public class MarkerLocationer implements Serializable {
private static final long serialVersionUID = 1L;
private MapModel draggableModel;
private Marker marker;
public MarkerLocationer() {
draggableModel = new DefaultMapModel();
//Shared coordinates
LatLng coord1 = new LatLng(41.017599, 28.985704);
//Draggable
draggableModel.addOverlay(new Marker(coord1, "Projenin olduğu yere sürükleyin."));
for (Marker marker2 : draggableModel.getMarkers()) {
marker2.setDraggable(true);
}
}
public Marker getMarker() {
return marker;
}
public void setMarker(Marker marker) {
this.marker = marker;
}
public MapModel getDraggableModel() {
return draggableModel;
}
public String lon, lat;
public void onMarkerDrag(MarkerDragEvent event) {
marker = event.getMarker();
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Proje lokasyonu belirlendi.", "X:" + marker.getLatlng().getLat() + " Y:" + marker.getLatlng().getLng()));
lon = String.valueOf(marker.getLatlng().getLng());
lat = String.valueOf(marker.getLatlng().getLat());
}
public void addMessage(FacesMessage message) {
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
我在希望用户填写的表单中使用上面的 bean。用户填写表格,将标记拖到所需位置并保存信息。这是我的另一个将信息保存到数据库的 bean。
@ManagedBean
@ViewScoped
public class AddProjectToDB {
String lat = new MarkerLocationer().lat;
String lon = new MarkerLocationer().lon;
//genel
private String projectName, projectExp, projectCoordLong, projectCoordLat;
/**
* Creates a new instance of AddProjectToDB
*/
static private FileHandler fileTxt;
static private SimpleFormatter formatterTxt;
int getter;
public String Save() throws Exception {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projetakip?useUnicode=true&characterEncoding=utf-8", "root", "");
Statement stmt = con.createStatement();
String SQL = "insert into `projects`(`id`,`projectName`,`projectExp`,`projectCoordLat`,`projectCoordLon`) values "
+ "(NULL,'"
+ projectName + "','"
+ projectExp + "','"
+ lat + "','"
+ lon + "')";
int Res = stmt.executeUpdate(SQL);
ResultSet rs = stmt.getGeneratedKeys();
getter = rs.getInt(1);
rs.close();
stmt.close();
return "0";
}
}
这是我的JSF:
<h:outputLabel value="Proje Adı: " />
<p:inputTextarea value="#{addProjectToDB.projectName}" id="projectName" autoResize="true"/>
<h:outputLabel value="Proje Detayı: " />
<p:inputTextarea value="#{addProjectToDB.projectExp}" id="projectExp" autoResize="true"/>
<h:outputLabel value="Proje Koordinatları: " />
<h:inputHidden value="#{addProjectToDB.projectCoordLat}" id="lat" />
<h:inputHidden value="#{addProjectToDB.projectCoordLong}" id="lon" />
<h:form>
<p:growl id="growl" showDetail="true"/>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<p:gmap center="41.017599,28.985704" zoom="13" type="HYBRID" model="#{markerLocationer.draggableModel}" style="width:600px;height:400px">
<p:ajax event="markerDrag" listener="#{markerLocationer.onMarkerDrag}" update="growl"/>
</p:gmap>
</h:form>
<p:commandButton value="Projeyi Kaydet" icon="ui-icon-disk" action="#{addProjectToDB.Save}" oncomplete="infDialog.show()" />
我的问题是当我尝试保存表单时,它会将位置设为空。但是,当我拖动标记时,我可以看到 long 和 lati。如何正确从 MarkerLocationer bean 中检索 long 和 lati?提前致谢。
好的。我找到了解决方案。这是我基本上是这样做的:
FacesContext facesContext = FacesContext.getCurrentInstance();
Application application = facesContext.getApplication();
ValueBinding binding = application.createValueBinding("#{markerLocationer}");
MarkerLocationer userInfo = (MarkerLocationer) binding.getValue(facesContext);
projectCoordLat = String.valueOf(userInfo.getMarker().getLatlng().getLat());
projectCoordLong = String.valueOf(userInfo.getMarker().getLatlng().getLng());