简体   繁体   中英

ManagedBean method called on XHTML page opening

I'm developping with Eclise Juno, Tomcat 7 and JSF.

When I run my project on Server, it seems that Client.fct1() method is ran. This is setting values in my XHTML InputText.

Upon startup I expect to see only the values set from my Bean Constructor method.

Here is my XHTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="./template.xhtml">
    <ui:param name="onLoadJS" value="initialize()" />
    <ui:define name="top">
        Frontend
        <script type="text/javascript"
                src="#{utils.GMapURL}">
        </script>
        <script type="text/javascript">
            var map;

            function initialize() {
                var x = 500;
                document.getElementById("bottom").style.height = x+'px'; 
                var mapOptions = {
                    center: new google.maps.LatLng(48.84325, 2.237803),
                    zoom: 14,
                    mapTypeId: google.maps.MapTypeId.HYBRID
                };
                map = new google.maps.Map(document.getElementById("bottom"),
                mapOptions);
                google.maps.event.addListener(map, 'click', function(event) {
                                                                addPlacemark(event.latLng.lat(),event.latLng.lng());
                                                            });
            }

            function add(vlat,vlon,vname) {
                ...
            }

            function addPlacemark(vlat,vlon) {
                ...
            }

            function lookAt(vlat,vlon) {
                ...
            }

            function resetBean() {
                alert("ResetBean!");
                #{client.fct1()};
            }

        </script>

        <A HREF="faces/map.xhtml">Placemark</A> <A HREF="faces/directions.xhtml">Directions</A> 
    </ui:define>

    <ui:define name="body">
        <h4>Latitude  : <h:inputText id="lat" value="#{client.lat}"/></h4>
        <h4>Longitude : <h:inputText id="lon" value="#{client.lon}"/></h4>
        <h4>Name      : <h:inputText id="name" value="#{client.name}"/></h4>
    <h:commandButton onclick="add(document.getElementById('lat').value,document.getElementById('lon').value,document.getElementById('name').value)" value="Add"/>
    <h:commandButton onclick="lookAt(document.getElementById('lat').value,document.getElementById('lon').value)" value="Look At"/>
    <h:commandButton onclick="displayAll()" value="Display All"/>
    <h:commandButton onclick="resetBean()" value="Reset Form (bean)"/>
    </ui:define>

    <ui:define name="bottom">
    </ui:define>
</ui:composition>
</html>

And my bean:

package web;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 * Session Bean implementation class Client
 */
@ManagedBean
@SessionScoped
public class Client implements Serializable {

private static final long serialVersionUID = 1L;

private String lat;
private String lon;
private String name;

// Constructor
public Client() {
    this.lat = "11.2051195";
    this.lon = "119.4056492";
    this.name = "[Put name here]";
}

public void fct1(){
    this.lat = "Damn";
    this.lon = "It";
    this.name = "..";
}

public String getLat() {
    return lat;
}

public void setLat(String lat) {
    this.lat = lat;
}

public String getLon() {
    return lon;
}

public void setLon(String lon) {
    this.lon = lon;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

So I am getting "Damn It ..." instead of the default "11.2051195 119.4056492 [Put name here]".

How come the fct1() method is called? How can I prevent it?

Thanks for your help!

You seem toassume that in

        function resetBean() {
            alert("ResetBean!");
            #{client.fct1()};
        }

The #{client.fct1()}; is called when the javascript function resetBean() is called. It is not. It is executed when the page is rendered (so no onload) since it is not in any jsf tag. If you want to call a remote method from javascript, the easiest thing to use is the OmniFaces commandScript (using omnifaces in your project is a good idea anyway)

Your code would then look like

        function resetBean() {
            alert("ResetBean!");
            callFct1();
        }

And in you xhtml add

<o:commandScript name="callFct1" action="#{client.fct1()}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM