webtier@glassfish.java.net

Re: Composite component with

From: <forums_at_java.net>
Date: Thu, 11 Nov 2010 03:43:56 -0800

Code belonging to proof of concept/failure

Facelet content:

<html xmlns="http://www.w3.org/1999/xhtml"    
xmlns:ui="http://java.sun.com/jsf/facelets"    
xmlns:f="http://java.sun.com/jsf/core"    
xmlns:c="http://java.sun.com/jstl/core"    
xmlns:h="http://java.sun.com/jsf/html"    
xmlns:own="http://java.sun.com/jsf/composite/custom/">        
<h:head>          <title>Composite Component Test</title>       
</h:head>     <h:body>         <h:form>           
 <own:listBox source="#{orderPage.availableLines}"
target="#{orderPage.order.lines}" converterId="lineConverter"/>       
     <h:commandButton value="try with cc" action="#{orderPage.persist}"/>
        </h:form>                  <h:form>       
     <h:selectManyListbox value="#{orderPage.order.lines}">       
         <f:converter converterId="lineConverter" />       
         <f:selectItems var="item" value="#{orderPage.availableLines}"
itemLabel="#{item.name}"/>             </h:selectManyListbox>    
        <h:commandButton value="try without cc"
action="#{orderPage.persist}"/>         </h:form>     </h:body>
</html>
Composite component content:

<html xmlns="http://www.w3.org/1999/xhtml"    
xmlns:h="http://java.sun.com/jsf/html"    
xmlns:f="http://java.sun.com/jsf/core"    
xmlns:ui="http://java.sun.com/jsf/facelets"    
xmlns:composite="http://java.sun.com/jsf/composite">       
<composite:interface>         <composite:attribute name="source"
required="true"/>         <composite:attribute name="target"
required="true"/>         <composite:attribute name="converterId"/>
    </composite:interface>     <composite:implementation>       
 <h:selectManyListbox value="#{cc.attrs.target}">           
 <f:converter converterId="#{cc.attrs.converterId}"
rendered="#{cc.attrs.converterId != null}" />           
 <f:selectItems var="item" value="#{cc.attrs.source}"
itemLabel="#{item.name}"/>         </h:selectManyListbox>        
         </composite:implementation> </html>
OrderPage Managed Bean

package cc; import java.util.List; import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped;
@ManagedBean(name="orderPage") @ViewScoped public class OrderPage {    
private Order order;         private List<Line> availableLines;    
    @PostConstruct     protected void init() {         order =
new Order(1);                  availableLines =
LineStore.getAvailableLines();     }         public void persist() {
        //here we would normally call persistence logic       
 System.out.println("Persiting order" + order);         if
(order.getLines() != null && order.getNrOflines() > 0) {           
 System.out.println("Order has following lines:");             for
(Line line : order.getLines()) {               
 System.out.println(line);             }         } else {
            System.out.println("Order has no lines");       
 }     }         public Order getOrder() {         return
order;     }         public void setOrder(Order order) {       
 this.order = order;     }         public List<Line>
getAvailableLines() {         return availableLines;     } }
Order

package cc; import java.util.List; public class Order { private int id;
private List<Line> lines; public Order(int id) { this.id = id; } @Override
public boolean equals(Object obj) { if (obj instanceof Order) { return
getId() == ((Order) obj).getId(); } return false; } @Override public int
hashCode() { return id; } @Override public String toString() { return
String.format("Order [id=%s] [number of lines: %s]", id, getNrOflines()); }
public int getNrOflines() { if (lines != null) { return lines.size(); } else
{ return 0; } } public int getId() { return id; } public List<Line>
getLines() { return lines; } public void setLines(List<Line> lines) {
this.lines = lines; } }
Line

package cc; public class Line {         private int id;     private
String name;             public Line(int id, String name) {    
    this.id = id;         this.name = name;     }        
@Override     public boolean equals(Object obj) {         if (obj
instanceof Line) {             return getId() == ((Line)
obj).getId();         }         return false;     }    
    @Override     public int hashCode() {         return id;
    }         @Override     public String toString() {    
    return String.format("Line [id=%s][name=%s]", id, name);     }
        public int getId() {         return id;     }    
    public String getName() {         return name;     }    
    public void setName(String name) {         this.name = name;
    } }
LineStore

package cc; import java.util.ArrayList; import java.util.List; public class
LineStore {     private static List<Line> availableLines;        
static {         availableLines = new ArrayList<Line>();         
        availableLines.add(new Line(1, "line 1"));       
 availableLines.add(new Line(2, "line 2"));       
 availableLines.add(new Line(3, "line 3"));       
 availableLines.add(new Line(4, "line 4"));     }         public
static List<Line> getAvailableLines() {         return availableLines;
    }         public static Line findLine(int id) {       
 for (Line line : availableLines) {             if (line.getId() ==
id) {                 return line;             }    
    }         return null;     }     }
LineConverter

package cc; import javax.faces.component.UIComponent; import
javax.faces.context.FacesContext; import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter(forClass=LineConverter.class, value="lineConverter") public
class LineConverter implements Converter { @Override public Object
getAsObject(FacesContext fc, UIComponent comp, String str) { return
LineStore.findLine(Integer.valueOf(str)); } @Override public String
getAsString(FacesContext fc, UIComponent comp, Object obj) { return
Integer.toString(((Line) obj).getId()); } }

--
[Message sent by forum member 'vdweij']
View Post: http://forums.java.net/node/715263