2016-09-20 135 views
-1

javascript文件我有3個文件:呼叫從一個Java Bean

  • 的index.xhtml:我用JSF,做一個2場形成(X:INT,Y:INT)與提交按鈕。
  • map.js:包含一個Js函數。
  • MbZoomtoXy.java:調用前面的Js函數。

我試圖做的是當我在表單中輸入x和y時。提交按鈕應該給我x + y的「警報」。

的index.xhtml

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:p="http://primefaces.org/ui"> 
    <h:head> 
     <title>Facelet Title</title> 
    </h:head> 
    <h:body> 

     <h:form id="form"> 

     <h:panelGrid columns="2" cellpadding="5"> 
      <p:outputLabel for="x-coor" value="X : " /> 
      <p:inputText id="x-coor" value="#{MbZoomtoXy.x}" required="true"/> 

      <p:outputLabel for="y-coor" value="Y : " /> 
      <p:inputText id="y-coor" value="#{MbZoomtoXy.y}" required="true"/> 
     </h:panelGrid> 

     <p:commandButton value="Save" actionListener="#{MbZoomtoXy.save}"/> 

     </h:form> 

    </h:body> 
</html> 

map.js

function zoomToXy(x,y){ 
    var s = x + y; 
    alert("x+y = "+s); 
} 

MbZoomtoXy.java

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import org.primefaces.context.RequestContext; 

@ManagedBean 
@SessionScoped 
public class MbZoomtoXy { 

    private MbZoomtoXy() { 
     } 

    public int getX() { 
     return x; 
    } 

    public void setX(int x) { 
     this.x = x; 
    } 

    public int getY() { 
     return y; 
    } 

    public void setY(int y) { 
     this.y = y; 
    } 

     int x,y; 

    public MbZoomtoXy(int x, int y) { 
     this.x = x; 
     this.y = y; 
    } 

    //Here I don t know how to call zoomToXy(x,y) function of map.js 
    public void save(){ 
     RequestContext requestContext = RequestContext.getCurrentInstance(); 
     requestContext.execute("..."); 

    } 

} 
+0

您需要點擊處理程序添加到客戶端的代碼,你可以」 t(以一種實用的方式)從Java以這種方式調用JavaScript函數。給按鈕一個ID並使用類似jQuery的東西來添加一個點擊處理器。您可能需要對JSF文件進行一些修改,以防止它立即發回Java(或在click事件中使用preventDefault)。 –

+0

正如@JeffWatkins所說,你已經在客戶端有輸入值。因此,嘗試抓住它們並將它們顯示在警報對話框中。 –

回答

0

你需要把map.jssrc/main/webapp/resources/js/map.js,並且包括在index.html

的index.html

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:p="http://primefaces.org/ui"> 
<h:head> 
    <title>Facelet Title</title> 
    <h:outputScript library="js" name="map.js" /> 
</h:head> 
<h:body> 

    <h:form id="form"> 

     <h:panelGrid columns="2" cellpadding="5"> 
      <p:outputLabel for="x-coor" value="X : " /> 
      <p:inputText id="x-coor" value="#{MbZoomtoXy.x}" required="true"/> 

      <p:outputLabel for="y-coor" value="Y : " /> 
      <p:inputText id="y-coor" value="#{MbZoomtoXy.y}" required="true"/> 
     </h:panelGrid> 

     <p:commandButton value="Save" actionListener="#{MbZoomtoXy.save}"/> 

    </h:form> 

</h:body> 
</html> 

MbZoomtoXy.java

package com.test; 

import org.primefaces.context.RequestContext; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import javax.faces.component.html.HtmlSelectOneMenu; 
import javax.faces.context.FacesContext; 
import javax.faces.event.AbortProcessingException; 
import javax.faces.event.ActionEvent; 
import javax.faces.event.AjaxBehaviorEvent; 
import java.io.Serializable; 
import java.util.Date; 


@ManagedBean(name = "MbZoomtoXy") 
@SessionScoped 
public class MbZoomtoXy implements Serializable { 

    public MbZoomtoXy() { 
    } 

    public int getX() { 
     return x; 
    } 

    public void setX(int x) { 
     this.x = x; 
    } 

    public int getY() { 
     return y; 
    } 

    public void setY(int y) { 
     this.y = y; 
    } 

    int x,y; 

    public MbZoomtoXy(int x, int y) { 
     this.x = x; 
     this.y = y; 
    } 

    //Here I don t know how to call zoomToXy(x,y) function of map.js 
    public void save(){ 
     RequestContext requestContext = RequestContext.getCurrentInstance(); 
     requestContext.execute(String.format("zoomToXy(%1$d, %2$d)", x, y)); 
    } 
} 
+0

它工作!謝謝你vit :) :) –