1

selectOneMenu ajax リスナーを使用しようとしていますが、ページがパラメーターを受け取ると機能しません。

私のテストでは、PrimeFaces ShowCase ページに示されているのと同じ例を使用しましたが、私のページは viewParam と viewAction によってもパラメーターを受け取ります。

selectOneMenu が初めて変更されると、アクション リスナーがトリガーされ、その後動作を停止します。

マイページ:

<?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:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>

    <f:metadata>
        <f:viewParam name="id" value="#{dropdownViewBean.id}" required="true" />
        <f:viewAction action="#{dropdownViewBean.init}" />
    </f:metadata>

    <h:body>
        <h:form>
            <p:growl id="msgs" showDetail="true" />

            <p:panel header="Select a Location" style="margin-bottom:10px;">
                <h:panelGrid columns="2" cellpadding="5">
                    <p:outputLabel for="country" value="Country: " />
                    <p:selectOneMenu id="country" value="#{dropdownViewBean.country}" style="width:150px">
                        <p:ajax listener="#{dropdownViewBean.onCountryChange}" update="city" />
                        <f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
                        <f:selectItems value="#{dropdownViewBean.countries}" />
                    </p:selectOneMenu>

                    <p:outputLabel for="city" value="City: " />
                    <p:selectOneMenu id="city" value="#{dropdownViewBean.city}" style="width:150px">
                        <f:selectItem itemLabel="Select City" itemValue="" noSelectionOption="true" />
                        <f:selectItems value="#{dropdownViewBean.cities}" />
                    </p:selectOneMenu>
                </h:panelGrid>

                <p:separator />

                <p:commandButton value="Submit" update="msgs" actionListener="#{dropdownViewBean.displayLocation}" icon="ui-icon-check" />
            </p:panel>
        </h:form>
    </h:body>
</html>

そして豆:

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named(value = "dropdownViewBean")
@ViewScoped
public class DropdownViewBean implements Serializable {

    private Map<String, Map<String, String>> data = new HashMap<String, Map<String, String>>();
    private String country;
    private String city;
    private Map<String, String> countries;
    private Map<String, String> cities;

    private int id;

    public DropdownViewBean() {
    }

    public void init() {

        System.out.println("init id=" + id); // message is correctly shown when page starts

        countries = new HashMap<String, String>();
        countries.put("USA", "USA");
        countries.put("Germany", "Germany");
        countries.put("Brazil", "Brazil");

        Map<String, String> map = new HashMap<String, String>();
        map.put("New York", "New York");
        map.put("San Francisco", "San Francisco");
        map.put("Denver", "Denver");
        data.put("USA", map);

        map = new HashMap<String, String>();
        map.put("Berlin", "Berlin");
        map.put("Munich", "Munich");
        map.put("Frankfurt", "Frankfurt");
        data.put("Germany", map);

        map = new HashMap<String, String>();
        map.put("Sao Paolo", "Sao Paolo");
        map.put("Rio de Janerio", "Rio de Janerio");
        map.put("Salvador", "Salvador");
        data.put("Brazil", map);
    }

    public Map<String, Map<String, String>> getData() {
        return data;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Map<String, String> getCountries() {
        return countries;
    }

    public Map<String, String> getCities() {
        return cities;
    }

    public void onCountryChange() {

        System.out.println("change"); // message is shown only the first time the combo is changed

        if (country != null && !country.equals("")) {
            cities = data.get(country);
        } else {
            cities = new HashMap<String, String>();
        }
    }

    public void displayLocation() {
        FacesMessage msg;
        if (city != null && country != null) {
            msg = new FacesMessage("Selected", city + " of " + country);
        } else {
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid", "City is not selected.");
        }

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

重要:

メタデータ、viewParam および viewAction タグを取り出し、init() メソッドを @PostConstruct でマークすると、サンプルは完全に機能しますが、ページ パラメーターが必要です。

私はそれを解決するためにあなたの助けを求めます.

4

2 に答える 2

1

私は に不信感を持ってjavax.faces.view.ViewScopedいます。怠惰すぎて問題を報告できませんでした。1 つには、そのタイプのViewScoped yetを使用する必要はありません。の最終的な非推奨につながるために導入されましたjavax.faces.bean.ViewScoped。現在と同様に、それは依然として非常に有効であり、適用可能です。

その注釈に関する唯一の制限は、CDI Bean には適用できず、@Inject注釈を許可する唯一のビュースコープであることです。あなたがそれを必要としているようには見えないので、安全に削除できるはずです.


@PostConstruct古い学校から GET パラメータを引き出すことができます:

@PostConstruct
public void init(){
   FacesContext ctxt = FacesContext.getCurrentInstance();
   Map<String,String> requestParameters =  ctxt.getExternalContext().getRequestParameterMap();

   int id = Integer.parseInt(requestParameters.get("id")); 


}
于 2014-07-19T18:38:26.967 に答える
0

私はあなたと同じ問題を抱えていました。

タグを置き換える

<f:viewParam name="id" value="#{dropdownViewBean.id}" required="true" />

<f:viewParam name="id" value="#{dropdownViewBean.id}"/>

残念ながら、このバグについての説明はありません

于 2015-06-26T14:31:17.897 に答える