You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2009/04/15 18:13:55 UTC

svn commit: r765257 - in /myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main: java/org/apache/myfaces/tobago/example/demo/ java/org/apache/myfaces/tobago/example/demo/bestpractice/ webapp/WEB-INF/ webapp/best-practice/

Author: lofwyr
Date: Wed Apr 15 16:13:53 2009
New Revision: 765257

URL: http://svn.apache.org/viewvc?rev=765257&view=rev
Log:
Simple demo to show how to use c:forEach. But with some disadvantages... (No Tag-Files, Scriptlets required, will not work in future versions)

Added:
    myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/Bird.java
    myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/best-practice/for-each.jsp
Modified:
    myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/Navigation.java
    myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/BestPracticeController.java
    myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/WEB-INF/faces-config.xml

Modified: myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/Navigation.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/Navigation.java?rev=765257&r1=765256&r2=765257&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/Navigation.java (original)
+++ myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/Navigation.java Wed Apr 15 16:13:53 2009
@@ -70,6 +70,7 @@
     bestPractice.add(new DefaultMutableTreeNode(new Node("theme", "best-practice/theme")));
     bestPractice.add(new DefaultMutableTreeNode(new Node("transition", "best-practice/transition")));
     bestPractice.add(new DefaultMutableTreeNode(new Node("nonFacesResponse", "best-practice/non-faces-response")));
+    bestPractice.add(new DefaultMutableTreeNode(new Node("forEach", "best-practice/for-each")));
     tree.add(bestPractice);
 
     DefaultMutableTreeNode reference = new DefaultMutableTreeNode(new Node("reference_intro", "reference/intro"));

Modified: myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/BestPracticeController.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/BestPracticeController.java?rev=765257&r1=765256&r2=765257&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/BestPracticeController.java (original)
+++ myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/BestPracticeController.java Wed Apr 15 16:13:53 2009
@@ -23,14 +23,25 @@
 
 import javax.faces.context.FacesContext;
 import javax.servlet.http.HttpServletResponse;
-import java.io.InputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.List;
 
 
 public class BestPracticeController {
 
   private static final Log LOG = LogFactory.getLog(BestPracticeController.class);
 
+  private List<Bird> birds = Arrays.asList(
+      new Bird("Amsel", 25),
+      new Bird("Drossel", 25),
+      new Bird("Fink", 9),
+      new Bird("Star", 19)
+  );
+
+  private String status;
+
   public String throwException() {
     throw new RuntimeException("This exception is forced by the user.");
   }
@@ -68,4 +79,16 @@
     facesContext.responseComplete();
     return null;
   }
+
+  public List<Bird> getBirds() {
+    return birds;
+  }
+
+  public String getStatus() {
+    return status;
+  }
+
+  public void setStatus(String status) {
+    this.status = status;
+  }
 }

Added: myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/Bird.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/Bird.java?rev=765257&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/Bird.java (added)
+++ myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/java/org/apache/myfaces/tobago/example/demo/bestpractice/Bird.java Wed Apr 15 16:13:53 2009
@@ -0,0 +1,61 @@
+package org.apache.myfaces.tobago.example.demo.bestpractice;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.tobago.util.VariableResolverUtil;
+
+import javax.faces.context.FacesContext;
+
+public class Bird {
+
+  private static final Log LOG = LogFactory.getLog(Bird.class);
+
+  private String name;
+  private int size;
+
+  public Bird(String name, int size) {
+    this.name = name;
+    this.size = size;
+  }
+
+  public String select() {
+    LOG.info("Select Bird: name=" + name + " size=" + size);
+    BestPracticeController controller = (BestPracticeController)
+        VariableResolverUtil.resolveVariable(FacesContext.getCurrentInstance(), "bestPracticeController");
+    controller.setStatus("Selected bird is " + name);
+    return null;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public int getSize() {
+    return size;
+  }
+
+  public void setSize(int size) {
+    this.size = size;
+  }
+}

Modified: myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/WEB-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/WEB-INF/faces-config.xml?rev=765257&r1=765256&r2=765257&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/WEB-INF/faces-config.xml (original)
+++ myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/WEB-INF/faces-config.xml Wed Apr 15 16:13:53 2009
@@ -229,6 +229,10 @@
       <from-outcome>best-practice/non-faces-response</from-outcome>
       <to-view-id>/best-practice/non-faces-response.jsp</to-view-id>
     </navigation-case>
+    <navigation-case>
+      <from-outcome>best-practice/for-each</from-outcome>
+      <to-view-id>/best-practice/for-each.jsp</to-view-id>
+    </navigation-case>
   </navigation-rule>
 
   <navigation-rule>

Added: myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/best-practice/for-each.jsp
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/best-practice/for-each.jsp?rev=765257&view=auto
==============================================================================
--- myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/best-practice/for-each.jsp (added)
+++ myfaces/tobago/branches/tobago-1.0.x/example/demo/src/main/webapp/best-practice/for-each.jsp Wed Apr 15 16:13:53 2009
@@ -0,0 +1,79 @@
+<%--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+--%>
+
+<%@ page import="org.apache.myfaces.tobago.example.demo.bestpractice.BestPracticeController" %>
+<%@ page import="org.apache.myfaces.tobago.util.VariableResolverUtil" %>
+<%@ page import="javax.faces.context.FacesContext" %>
+<%@ page import="javax.servlet.jsp.jstl.core.LoopTagStatus" %>
+
+<%@ taglib uri="http://myfaces.apache.org/tobago/extension" prefix="tx" %>
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
+<%@ taglib uri="http://myfaces.apache.org/tobago/component" prefix="tc" %>
+<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
+<%@ taglib tagdir="/WEB-INF/tags/layout" prefix="layout" %>
+
+<%
+  // load bean before expression in foreach
+  BestPracticeController controller
+      = (BestPracticeController) VariableResolverUtil.resolveVariable(
+      FacesContext.getCurrentInstance(), "bestPracticeController");
+%>
+<%!
+  private String expression(String list, String status, String field, PageContext pageContext) {
+    int index = ((LoopTagStatus) pageContext.getAttribute(status)).getIndex();
+    return "#{" + list + "[" + index + "]." + field + "}";
+  }
+  private String fixed(int count) {
+    StringBuffer buffer = new StringBuffer();
+    for (int i = 0; i < count; i++) {
+      buffer.append("fixed;");
+    }
+    return buffer.toString();
+  }
+%>
+<f:view locale="#{clientConfigController.locale}">
+  <tc:loadBundle basename="overview" var="overviewBundle"/>
+  <tc:page applicationIcon="icon/favicon.ico" id="page" width="600px" height="400px">
+    <tc:box label="Best Practice - For Each">
+      <f:facet name="layout">
+        <tc:gridLayout rows="<%= "100px;" + fixed(controller.getBirds().size()) + "100px;*"%>" columns="2*;*"/>
+      </f:facet>
+
+      <tc:cell spanX="2">
+        <tc:out escape="false"
+                value="Is is possible to use the c:forEach tag of the JSTL to iterate over a list. <br/><b>Warning:</b> <br/>This example is a workaround. <br/>It is not compatible with further releases! <br/>This example works not with JSP-Tag-Files!"/>
+      </tc:cell>
+
+      <c:forEach items="${bestPracticeController.birds}" varStatus="status">
+        <%
+          String label = expression("bestPracticeController.birds", "status", "name", pageContext);
+          String value = expression("bestPracticeController.birds", "status", "size", pageContext);
+          String action = expression("bestPracticeController.birds", "status", "select", pageContext);
+        %>
+        <tx:in value="<%=value%>" label="<%=label%>"/>
+        <tc:button label="Select" action="<%=action%>"/>
+      </c:forEach>
+
+      <tc:cell spanX="2">
+        <tc:out value="#{bestPracticeController.status}"/>
+      </tc:cell>
+
+      <tc:cell spanX="2"/>
+
+    </tc:box>
+  </tc:page>
+</f:view>