posts - 2, comments - 18, trackbacks - 0, articles - 0

2007年7月19日

刚开始接触JSF的时候到处找双联菜单,后来自己写了个,是链接数据库的。 不过这里作为一个演示把数据库部分删除了,可直接运用
希望可以供初学JSF的朋友使用。

select.jsp
<%@page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ page language="java"%>
<html>
    
<f:view>
        
<head>
            
<title>Search_Advance</title>
        
</head>
        
<body>
            
<h:form>
                
<h:selectOneMenu value="#{selectItem.selectedOne}"
                    onchange
="this.form.submit();"
                    style
="position:absolute; left:95; top: 90; width:100; height:35"
                    valueChangeListener
="#{selectItem.selectedOneChange}">
                    
<f:selectItems value="#{selectItem.LMList}" />
                
</h:selectOneMenu>

                
<h:selectOneMenu value="#{selectItem.selectedTwo}"
                    style
="position:absolute; left:95; top: 135; width:100; height:35"
                    onchange
="this.form.submit();"
                    valueChangeListener
="#{selectItem.selectedTwoChange}"
                    immediate
="true">
                    
<f:selectItems value="#{selectItem.currentMCList}" />
                
</h:selectOneMenu>

                
<h:commandButton type="submit" value="OK"
                    style
="position:absolute; left: 140; top: 220;"
                    action
="#{selectItem.saveCondition}"/>
            
</h:form>
        
</body>
    
</f:view>
</html>

MySelect.java
package mypackage;

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

    
//INI----------------------------------------------------------------------
    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);

        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 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;
    }
}

faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>
    
<managed-bean>
        
<managed-bean-name>selectItem</managed-bean-name>
        
<managed-bean-class>
            mypackage.MySelect
        
</managed-bean-class>
        
<managed-bean-scope>session</managed-bean-scope>
    
</managed-bean>
</faces-config>


posted @ 2007-08-15 21:48 一只小苹果 阅读(3701) | 评论 (15)编辑 收藏

     摘要: Dynamic DataTable, Fixed Header, Fixed NumColumn, Sortable Header自己写了段小代码, 希望可以供大家学习和参考。 代码里没有太多注释, 有时间的话我会补充上去。自己在写动态生成DataTable的时候也查阅了很多相关文章, 以及实现固定表头等等。在解决固定表头问题上我是用的两张表(加行标是3张表)实现的, 因为我发现如果用JSF1.1...  阅读全文

posted @ 2007-07-19 09:15 一只小苹果 阅读(5408) | 评论 (3)编辑 收藏