0

我有以下代码,其中显示了两个 SelectOneMenus。第一个菜单将语言作为值,第二个菜单告诉我选择“一个或两个”,如果是 En 作为语言,选择“uno 或 duo”,如果是西班牙语。

我还需要确定我是否选择了项目“two”,如果我现在将语言更改为“Sp”,那么“duo”必须是所选项目。

我怎样才能做到这一点?这是我所拥有的

索引.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://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
                           <h:form id="myForm">
                                    <h:panelGrid columns="4">
                                        <h:outputLabel value="Language:" />
                                        <h:selectOneMenu value="#{myBean.language}">
                                            <f:selectItems value="#{myBean.languages}" />
                                            <f:ajax listener="#{myBean.doUpdate}" render ="myForm" />
                                        </h:selectOneMenu>

                                        <h:outputLabel value="Items:" />
                                        <h:selectOneMenu value="#{myBean.item}">
                                            <f:selectItems value="#{myBean.items}" />
                                            <f:ajax listener="#{myBean.doUpdate}" render ="myForm" />
                                        </h:selectOneMenu>
                                         <h:panelGrid columns="1" id ="show" >
                                            <h:outputLabel value="#{myBean.language} #{myBean.item}"/>
                                        </h:panelGrid>

                                    </h:panelGrid>
                                </h:form>
    </h:body>
</html>

我的豆:

package bean;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;



@ManagedBean(name = "myBean")
//@Stateless
@Stateful
@ViewScoped
public class MyBean implements Serializable{


    private List<String> languages;
    private String language;
    private String item;
    private List<String> items;

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }


    /**
     * init() method for initializing the bean. Is called after constuction.
     */

    @PostConstruct
    private void init() {
    //public MyBean () {
        languages = getAllLanguages();
        language = "en"; //defaultLanguage 
        items = getItemsFromOverview();
    } 

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public List<String> getLanguages() {
        System.out.println("getLanguages, language " +language);
        return languages;
    }



    public String getLanguage() {
        System.out.println("getLanguage " +language);
        return language;
    }

    public void setLanguage(String language) {
        System.out.println("setLanguage " +language);
        this.language = language;
    }




    public List<String> getAllLanguages() {
        List<String> results = new ArrayList<String>();
        results.add("sp");
        results.add("en");

        if(results != null){
            System.out.println("getting all languages");
        }
        return results;
    }


    public void doUpdate() {

         System.out.println("doUpdate language " +language);
         System.out.println("doUpdate item " +item);

    }

    /**
     * Returns a list of all serialIDs present in the overview.
     * @return 
     */
    private List<String> getItemsFromOverview() {
        List<String> results = new ArrayList<String>();

        if(language.equalsIgnoreCase("en")){
            results.add("one");
            results.add("two");
        }
        else {
            results.add("uno");
            results.add("duo");

        }
                return results;
    }


}
4

1 回答 1

1

这很简单。您忘记更改第二个列表 ( items) 的当前值。做喜欢

<h:form id="myForm">
    <h:selectOneMenu id="list1" value="#{myBean.language}" actionListener="#{myBean.updateFiledsInList2}">
        <f:selectItems value="#{myBean.languages}" />
        <f:ajax execute="@this" render ="list2 lang" />
    </h:selectOneMenu>
    <h:selectOneMenu id="list2" value="#{myBean.item}" >
        <f:selectItems value="#{myBean.items}" />
        <f:ajax execute="@this" render="item" />
    </h:selectOneMenu>
    <h:outputText id="lang" value="#{myBean.language}"/>
    <h:outputText id="item" value="#{myBean.item}"/>
</h:form>

在 MyBean 中使用以下动作侦听器:

public void updateFiledsInList2(ActionEvent event) {
    if(language.equals("en")) {
        items = new ArrayList<String>();
        items.add("one");
        items.add("two");
    } else if(language.equals("es")) {
        items = new ArrayList<String>();
        items.add("uno");
        items.add("dos");
    } else {
        items = null;
    }
}

这就是说,您应该管理您的代码,通过删除@State...您的注释@ManagedBean(它不是 an @EJB)并更好地处理上面提供的示例中的选择(通过 an@EJB或在 a 中有简单的列表@SessionScoped @ManagedBean等)。

要保留项目的选定值,只需在动作侦听器代码中添加一些行(假设项目列表中的项目索引相同并且它们在两种语言中的大小相同):

public void updateFiledsInList2(ActionEvent event) {
    int index = (item == null) ? -1 : items.indexOf(item);
    if(language.equals("en")) {
        items = new ArrayList<String>();
        items.add("one");
        items.add("two");
    } else if(language.equals("es")) {
        items = new ArrayList<String>();
        items.add("uno");
        items.add("dos");
    } else {
        items = null;
    }
    if(index >= 0) {
        item = (items == null) ? null : items.get(index);
    } else {
        item = null;
    }
}
于 2013-02-18T13:16:12.940 回答