次のような (メーカー、色、タイプ) などのデータを表示/提示するにはどうすればよいですか。
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;
}
}