selectOneMenu生成项目符号列表

时间:2016-08-15 21:21:01

标签: jsf primefaces

我正在尝试构建一个涉及下拉菜单的Web应用程序,该菜单在选择选项时会使用数据填充表格。我有问题,因此试图通过尝试重新创建following example from the Primefaces website.来解决问题我遇到了以下问题:

1)selectOneMenu在结果页面的正上方生成一个文本框。

2)seelctOneMenu还会生成选择菜单上已有选项的项目符号列表。

3)第一个选择菜单上的ajax监听器不会更新第二个菜单,并且似乎没有在DropdownView类中运行任何方法。

简而言之,输出是意外的,特别是因为我或多或少地复制/粘贴示例代码。

我在JDeveloper12c上使用Weblogic,JSF 2.2和Primefaces 6.0运行这一切。

这是我正在运行的代码,几乎所有代码都是从Primefaces网站复制/粘贴的

这是我的dropdown.xhtml:

<!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:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">

<body>
<h:form>
<h:messages errorStyle="color:red" />
<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="#{dropdownView.country}" style="width:150px">
            <p:ajax listener="#{dropdownView.onCountryChange()}" update="city" />
            <f:selectItem itemLabel="Select Country" itemValue="" noSelectionOption="true" />
            <f:selectItems value="#{dropdownView.countries}" />
        </p:selectOneMenu>

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

    <p:separator />

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

My DropdownView.java,与示例站点中的代码相同:

package test;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped
public class DropdownView 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;

@PostConstruct
public void init() {
    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 void setData(Map<String, Map<String, String>> data) {
    this.data = data;
}

public void setCountries(Map<String, String> countries) {
    this.countries = countries;
}

public void setCities(Map<String, String> cities) {
    this.cities = cities;
}

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() {
    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);  
}

}

这是我的web.xml:

<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
  <servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
  </servlet-mapping>

</web-app>

这是我的faces-config.xml:

    <faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">

</faces-config>

1 个答案:

答案 0 :(得分:0)

用JSF <body>标记替换HTML <h:body>标记。可能更重要的是,添加<h:head />行。它可能是空的,但它很重要,因为这是添加JavaScript和CSS文件的地方。

顺便说一下,您描述的问题表明这些文件丢失了。您将看到如何从HTML构建块构建selectOneMenu,但没有CSS和JavaScript粘合代码。

相关问题