JSF: 联动菜单局部刷新

select.jsp

 

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<f:view>
<html>
<body>
			<br>
			<br>
			<br>
			<br>
			<br>
<a4j:form>
      <h:selectOneMenu value="#{testBean.selectedOne}"
                               valueChangeListener="#{testBean.selectedOneChange}">
	<f:selectItems value="#{testBean.LMList}" />
	<a4j:support event="onchange" limitToList="true"
	action="#{testBean.change}"  reRender="two"  ajaxSingle="true">
                </a4j:support>
      </h:selectOneMenu>

      <h:selectOneMenu value="#{testBean.selectedTwo}" id="two"
                valueChangeListener="#{testBean.selectedTwoChange}"
	immediate="true">
         <f:selectItems value="#{testBean.currentMCList}" />
    </h:selectOneMenu>
     <a4j:commandButton type="submit" value="OK"
                             action="#{testBean.saveCondition}" />
</a4j:form>

</body>
</html>
</f:view>

  

 

 MySelect.java

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

public class MySelect {

	public List<SelectItem> LMList = new LinkedList<SelectItem>();;

	public Vector<List<SelectItem>> MCList = new Vector<List<SelectItem>>();

	public List<SelectItem> currentMCList = new LinkedList<SelectItem>();

	public String selectedOne = new String();

	public String selectedTwo = new String();

	public MySelect () throws Exception {

		// This is just a sample.
		// You colud add your DB connection here and INI you List from DataBase.
		LMList.add(new SelectItem("0_0", ""));
		LMList.add(new SelectItem("A_1", "A"));
		LMList.add(new SelectItem("B_2", "B"));
		LMList.add(new SelectItem("C_3", "C"));

		List<SelectItem> list0 = new LinkedList<SelectItem>();
		List<SelectItem> list1 = new LinkedList<SelectItem>();
		List<SelectItem> list2 = new LinkedList<SelectItem>();
		List<SelectItem> list3 = new LinkedList<SelectItem>();

		list0.add(new SelectItem("", ""));

		list1.add(new SelectItem("", ""));
		list1.add(new SelectItem("a1", "a1"));
		list1.add(new SelectItem("a2", "a2"));

		list2.add(new SelectItem("", ""));
		list2.add(new SelectItem("b1", "b1"));
		list2.add(new SelectItem("b2", "b2"));

		list3.add(new SelectItem("", ""));
		list3.add(new SelectItem("c1", "c1"));
		list3.add(new SelectItem("c2", "c2"));

		MCList.add(list0);
		MCList.add(list1);
		MCList.add(list2);
		MCList.add(list3);
	}

	public void selectedOneChange(ValueChangeEvent event) {

		// valueChangeEvent of the first SelectOneMeun
		// The action is about INI the list for the second SelectOneMeun
		String str = (String) event.getNewValue();
		// Like you see, in the first SelectItems we hava "A_1"
		String[] ss = str.split("_");
		// After this we got the index of the "A_1" and the index is "1"
		str = ss[1];

		int index = Integer.parseInt(str);
		System.out.println(index);
		currentMCList = MCList.get(index);
	}

	public void selectedTwoChange(ValueChangeEvent event) {
		// valueChangeEvent of the second SelectOneMeun
		// add action if you have
		System.out.println("selectedTwoChange");
	}

	public void saveCondition() {
		// add action if you have
		System.out.println("saveCondition");
	}

	public void change() {
		System.out.println(selectedOne+"---------");		
		System.out.println("I  was  changed! 哈哈");
	}
	public List<SelectItem> getLMList() {
		return LMList;
	}

	public void setLMList(List<SelectItem> list) {
		LMList = list;
	}

	public String getSelectedOne() {
		return selectedOne;
	}

	public void setSelectedOne(String selectedOne) {
		this.selectedOne = selectedOne;
	}

	public String getSelectedTwo() {
		return selectedTwo;
	}

	public void setSelectedTwo(String selectedTwo) {
		this.selectedTwo = selectedTwo;
	}

	public List<SelectItem> getCurrentMCList() {
		return currentMCList;
	}

	public void setCurrentMCList(List<SelectItem> currentMCList) {
		this.currentMCList = currentMCList;
	}

}

 

 

你可能感兴趣的:(html,JSF,Richfaces,F#,sun)