users@javaserverfaces.java.net

dataTable of map

From: Thufir <hawat.thufir_at_gmail.com>
Date: Mon, 12 Apr 2010 17:19:09 +0000 (UTC)

I'm having trouble getting the output I want. In km.xhtml I want to do
something like:

//pseudo code

dataTable value=data.map var var=map
columns=2
column1=map.keys
column2=map.values


Is that possible in JSF?





thufir_at_ARRAKIS:~/NetBeansProjects$
thufir_at_ARRAKIS:~/NetBeansProjects$
thufir_at_ARRAKIS:~/NetBeansProjects$ nl JSF/web/km.xhtml
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     3 <html xmlns="http://www.w3.org/1999/xhtml"
     4 xmlns:h="http://java.sun.com/jsf/html"
     5 xmlns:f="http://java.sun.com/jsf/core">
     6 <head><title>Kilometers</title></head>
     7 <body>
       
     8 #{data.units}
     9 #{data.value} #{data.unit}'s is
       
    10 <h:dataTable value="#{data.units}" var="item">
    11 <h:column>
    12 <f:facet name="header">
    13 <h:outputText value="unit" />
    14 </f:facet>
    15 <h:outputText value="#{data.km}" />
    16 </h:column>
       
    17 <h:column>
    18 <f:facet name="header">
    19 <h:outputText value="value" />
    20 </f:facet>
    21 <h:outputText value="#{item.miles}" />
    22 </h:column>
    23 </h:dataTable>
    24 </body>
    25 </html>
thufir_at_ARRAKIS:~/NetBeansProjects$
thufir_at_ARRAKIS:~/NetBeansProjects$ nl JSF/src/java/controller/
DataEntry.java
     1 /*
     2 * To change this template, choose Tools | Templates
     3 * and open the template in the editor.
     4 */
       
     5 package controller;
       
     6 import java.util.Arrays;
     7 import java.util.List;
     8 import java.util.Locale;
     9 import javax.faces.bean.ManagedBean;
    10 import javax.faces.bean.RequestScoped;
    11 import model.Unit;
       
       
    12 /**
    13 *
    14 * @author thufir
    15 */
    16 @ManagedBean(name = "data")
    17 @RequestScoped
    18 public class DataEntry {
       
    19 private Unit unit = Unit.KM;
    20 private double value = 1.0;
    21 private Distance distance;
       
    22 /** Creates a new instance of DataEntry */
    23 public DataEntry() {
    24 }
       
    25 /**
    26 * @return the unit
    27 */
    28 public Unit getUnit() {
    29 return unit;
    30 }
       
    31 /**
    32 * @param unit the unit to set
    33 */
    34 public void setUnit(Unit unit) {
    35 this.unit = unit;
    36 }
       
    37 /**
    38 * @return the value
    39 */
    40 public double getValue() {
    41 return value;
    42 }
       
    43 /**
    44 * @param value the value to set
    45 */
    46 public void setValue(double value) {
    47 this.value = value;
    48 }
       
    49 //questionable
    50 public List<Unit> getUnits() {
    51 return Arrays.asList( Unit.values());
    52 }
       
       
       
    53 public String convert() {
    54 distance = new Distance(unit,value);
    55 return getUnit().toString().toLowerCase(Locale.US);
    56 }
       
    57 }
thufir_at_ARRAKIS:~/NetBeansProjects$
thufir_at_ARRAKIS:~/NetBeansProjects$ nl JSF/src/java/model/Unit.java
     1 /*
     2 * To change this template, choose Tools | Templates
     3 * and open the template in the editor.
     4 */
     5 package model;
       
     6 import java.util.ArrayList;
     7 import java.util.List;
       
     8 /**
     9 *
    10 * @author a00720398
    11 */
    12 public enum Unit {
       
    13 KM(1.0), Miles(1.609344);
    14 private double cf = 0;
    15 private double km = 5.0, miles = 9.0;
       
    16 Unit(double conversionFactor) {
    17 this.cf = conversionFactor;
    18 }
       
    19 public List<Unit> getUnits() {
       
    20 List<Unit> list = new ArrayList<Unit>();
    21 for (Unit someType : Unit.values()) {
    22 list.add(someType);
    23 }
    24 return list;
    25 }
       
    26 /**
    27 * @return the km
    28 */
    29 public double getKm() {
    30 return km*cf;
    31 }
       
    32 /**
    33 * @param km the km to set
    34 */
    35 public void setKm(double km) {
    36 this.km = km;
    37 }
       
    38 /**
    39 * @return the miles
    40 */
    41 public double getMiles() {
    42 return miles*cf;
    43 }
       
    44 /**
    45 * @param miles the miles to set
    46 */
    47 public void setMiles(double miles) {
    48 this.miles = miles;
    49 }
    50 }
thufir_at_ARRAKIS:~/NetBeansProjects$


thanks,

Thufir