0

我希望使用 java 文件中定义的 arrayList 填充丰富的树。数组列表是对象“carVO”的列表,具有 (String make,String model,int year) 树的根是字符串“Cars”,第二层是制造,第三层是模型,最后一层是年份。直到现在我已经建立了一个具有根节点和第二级的树,我不能再进一步了。

这是 carBB.java 类

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.richfaces.model.TreeNodeImpl;

public class CarBB {

private String make;
private String model;
private int year;
private int price;
private Date registrationDate; 
private ArrayList<Integer> yearList = new ArrayList<Integer>();
private ArrayList<CarVO> carList = new ArrayList<CarVO>();
private ArrayList<String> makeList = new ArrayList<String>();
private String makeSearch;
private String param="";

private TreeNodeImpl<String> carRoot = new TreeNodeImpl<String>();
private TreeNodeImpl<String> carNodes = new TreeNodeImpl<String>(); 
int i=0;



@javax.annotation.PostConstruct
public void initialize()
{
    try
    {   
        for (int i=1995;i<2015;i++)
        {
            yearList.add(i);
        }

        carRoot.setData("Car");
        carNodes.addChild(0, carRoot);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}


public boolean addNewCar()
{
    CarVO newCar = new CarVO();
    newCar.setModel(model.toLowerCase());   
    newCar.setPrice(price);
    newCar.setYear(year);
    newCar.setMake(make.toUpperCase());
    newCar.setRegistrationDate(registrationDate);
    carList.add(newCar);

    if(!(makeList.contains(make.toUpperCase())))
    {
        makeList.add(make.toUpperCase());
        TreeNodeImpl<String> child = new TreeNodeImpl<String>();
        child.setData(make.toUpperCase());
        carRoot.addChild(i, child);
        i++;
    }
    make="";
    price=0;
    year=0;
    model="";
    registrationDate = null;
    return true;    
}   

public String getParamMake() {
    if (getRequestParameter("make") != null) {
        param = getRequestParameter("make");
    }
    return param;
} 

public String getParamModel() {
    if (getRequestParameter("model") != null) {
        param = getRequestParameter("model");
    }
    return param;
} 

public String getParamYear() {
    if (getRequestParameter("year") != null) {
        param = getRequestParameter("year");
    }
    return param;
} 
public String getParamPrice() {
    if (getRequestParameter("price") != null) {
        param = getRequestParameter("price");
    }
    return param;
} 
public String getParamReg() {
    if (getRequestParameter("regDate") != null) {
        param = getRequestParameter("regDate");
    }
    return param;
} 

public String getParamView() {
    if (getRequestParameter("viewOnly") != null) {
        param = getRequestParameter("viewOnly");
    }
    return param;
} 

private String getRequestParameter(String name) {
    return ( (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getParameter(name);
}

public String welcomeAction(String action)
{
    return action;
}

public ArrayList<CarVO> getCarList() {
    return carList;
}


public void setCarList(ArrayList<CarVO> carList) {
    this.carList = carList;
}


public String getMake() {
    return make;
}

public void setMake(String make) {
    this.make = make;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

public Date getRegistrationDate() {
    return registrationDate;
}

public void setRegistrationDate(Date registrationDate) {
    this.registrationDate = registrationDate;
}


public ArrayList<Integer> getYearList() {
    return yearList;
}


public void setYearList(ArrayList<Integer> yearList) {
    this.yearList = yearList;
}

public String getMakeSearch() {
    return makeSearch;
}


public void setMakeSearch(String makeSearch) {
    this.makeSearch = makeSearch;
}

public ArrayList<String> getMakeList() {
    return makeList;
}


public void setMakeList(ArrayList<String> makeList) {
    this.makeList = makeList;
}



public String getParam() {
    return param;
}


public void setParam(String param) {
    this.param = param;
}


public TreeNodeImpl<String> getCarRoot() {
    return carRoot;
}


public void setCarRoot(TreeNodeImpl<String> carRoot) {
    this.carRoot = carRoot;
}


public TreeNodeImpl<String> getCarNodes() {
    return carNodes;
}


public void setCarNodes(TreeNodeImpl<String> carNodes) {
    this.carNodes = carNodes;
}

public Date toDate(String dateStr)
{
    Date date = new Date(); 
    SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    try
    {
        date = sf.parse(dateStr);
        return date;
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null;
}
  }

这是 carTree.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>
    <head>
    <title>Tree View</title>
    </head>
    <body>
        <f:view>
            <h:form id="treeForm">
                <rich:panel header="Tree View" >

            <rich:tree value="#{carBB.carNodes}" var="car">
               <rich:treeNode>
                  <h:outputText value="#{car}" />
               </rich:treeNode>
            </rich:tree>

                </rich:panel> 
            </h:form>
       </f:view>
    </body>
</html>
4

1 回答 1

0

请检查这个。它有一个工作样本。

于 2013-07-24T10:53:47.717 回答