【问题标题】:selectOneMenu produces bulleted list [duplicate]selectOneMenu 生成项目符号列表[重复]
【发布时间】:2016-08-15 21:21:00
【问题描述】:

我正在尝试构建一个包含下拉菜单的 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>

我的 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>

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    将 HTML &lt;body&gt; 标记替换为 JSF &lt;h:body&gt; 标记。可能更重要的是,添加一个&lt;h:head /&gt; 行。它可能是空的,但它很重要,因为这是添加 JavaScript 和 CSS 文件的地方。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      相关资源
      最近更新 更多