显示按字段分组的页面上的列表

时间:2013-09-20 22:46:45

标签: jsf

如何显示/显示某些数据,例如(制造商,颜色,类型),类似于以下数据:

List<Car> list = new ArrayList<>();          
list.add(new Car("ford", "red", "focus"));    
list.add(new Car("opel", "red", "astra"));    
list.add(new Car("opel", "blue", "astra"));    
list.add(new Car("toyota", "red", "camry"));    
list.add(new Car("honda", "blue", "civic"));    
list.add(new Car("BMW", "green", "3"));

在JSF页面上,如:

[red]
ford focus
opel astra
toyota camry

[blue]
opel astra
honda civic

[green]
BMW 3

我需要更详细的解释。我不知道数据库中会出现的颜色。它们是未知的,它们基于用户输入,并且未定义它们。这只是一个例子。

更新

class Car {

private String name;
private String color;
private String model;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getModel() {
    return model;
}

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

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}
}

我的观点:

<?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://xmlns.jcp.org/jsf/html"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    Hello from Facelets
    <br/>
    <br/>
    <br/>
    <ui:repeat value="#{carBean.groups}" var="entry">
        <h:outputText value ="#{entry}"/>

        <br/>
        <ui:repeat value="asdasdas" var="second">
            #{second} <br/>
            <br/>
        </ui:repeat>


    </ui:repeat>

    <ui:repeat value="#{carBean.carsGroupedByColor.entrySet().toArray()}" var="entry">
        <div>
            Color: #{entry.key}
            <br />
            <ui:repeat value="#{entry.value}" var="car">
                Car name: #{car}
                <br />
            </ui:repeat>
        </div>
    </ui:repeat>



</h:body>
</html>

我的豆子:

@Named
@ViewScoped
public class CarBean implements Serializable {

private Map<String, List<Car>> carsGroupedByColor;
private List<String> groups;
private List<Car> temp;
private List<Car> carsByColor;

@PostConstruct
public void init() {

    List<Car> list = new ArrayList<>();
    Car car = new Car();
    car.setColor("red");
    car.setModel("camry");
    car.setName("redcar1");
    list.add(car);
    Car car1 = new Car();
    car1.setColor("bue");
    car1.setModel("golf");
    car1.setName("bluecar1");
    list.add(car1);
    Car car2 = new Car();
    car2.setColor("red");
    car2.setModel("astra");
    car2.setName("redcar2");
    list.add(car2);

    System.out.println(list);


    groups = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        //         System.out.println(list.get(i).getName());
        //         System.out.println(list.get(i).getColor());
        //        System.out.println(list.get(i).getModel());
        groups.add(list.get(i).getColor());

    }

    Set<String> set = new HashSet<>(groups);
    groups.clear();
    groups.addAll(set);
    carsGroupedByColor = new LinkedHashMap<>();

    Car carx;

    for (int i = 0; i < groups.size(); i++) {
        temp = new ArrayList<>();
        for (int j = 0; j < list.size(); j++) {

            if (groups.get(i).equals(list.get(j).getColor())) {
                carx = new Car();
                carx.setColor(list.get(j).getColor());
                carx.setName(list.get(j).getName());
                carx.setModel(list.get(j).getModel());

                System.out.println("Color is " + groups.get(i) + "=" + list.get(j).getColor());
                System.out.println(list.get(j).getName());
                temp.add(carx);
            }
        }
        System.out.println(temp);
        carsGroupedByColor.put(groups.get(i), temp);
        System.out.println("carsGroupedByColorcars is " + carsGroupedByColor.get(groups.get(i)));

        System.out.println("temp is clear: " + temp);

    }

    for (int i = 0; i < groups.size(); i++) {

        System.out.println(groups.get(i) + "   " + carsGroupedByColor.get(groups.get(i)));



    }
}

public Map<String, List<Car>> getCarsGroupedByColor() {
    return carsGroupedByColor;
}

public void setCarsGroupedByColor(Map<String, List<Car>> carsGroupedByColor) {
    this.carsGroupedByColor = carsGroupedByColor;
}

public List<String> getGroups() {
    return groups;
}

public void setGroups(List<String> groups) {
    this.groups = groups;
}
}

2 个答案:

答案 0 :(得分:3)

您可以拥有Map<String, List<Car>>并迭代主List<Car> allCars以在地图中相应地分配汽车(我假设Car#colorString,否则请使用正确键入地图键。)

Java代码:

@ManagedBean
@ViewScoped
public class CarBean {

    private Map<String, List<Car>> carsGroupedByColor;

    @PostConstruct
    public void init() {
        List<Car> allCars = ...; //this can be replaced to retrieve the list the way you want/need
        Map<String, List<Car>> carsGroupedByColor = new LinkedHashMap<String, List<Car>>();
        for (Car car : allCars) {
            List<Car> carsByColor = carsGroupedByColor.get(car.getColor());
            if (carsByColor == null) {
                carsByColor = new ArrayList<Car>();
            }
            carsByColor.add(car);
        }
    }

    public Map<String, List<Car>> getCarsGroupedByColor() {
        return carsGroupedByColor;
    }
}

Facelets代码:

<ui:repeat value="#{carBean.carsGroupedByColor}" var="entry">
    <div>
        Color: #{entry.key}
        <br />
        <ui:repeat value="#{entry.value}" var="car">
            Car name: #{car.name}
            <br />
        </ui:repeat>
    </div>
</ui:repeat>

答案 1 :(得分:0)

HashMap<String, List> cars = new HashMap<String, List>();

List<Car> red = new ArrayList<>();
List<Car> blue = new ArrayList<>();
List<Car> green = new ArrayList<>();          
red .add(new Car("ford", "red", "focus"));    
red .add(new Car("opel", "red", "astra"));    
blue.add(new Car("opel", "blue", "astra"));    
red .add(new Car("toyota", "red", "camry"));    
blue.add(new Car("honda", "blue", "civic"));    
green.add(new Car("BMW", "green", "3"));

cars.put("red",red);
cars.put("blue",blue);
cars.put("green",green);

//now it's easy to print them by color ;)
System.out.println("[red]");
for(Car car:(List)cars.get("red"))
    System.out.println(car.getColor());
//etc