我有以下代码,其中显示了两个 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;
}
}