Author: mrdon
Date: Sat Jan  6 01:15:15 2007
New Revision: 493436

URL: http://svn.apache.org/viewvc?view=rev&rev=493436
Log:
Better handling of nulls for select tag, injected object factory into bean 
component
WW-1567

Added:
    
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-9.txt
Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Bean.java
    struts/struts2/trunk/core/src/main/resources/template/simple/select.ftl
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/SelectTest.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Bean.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Bean.java?view=diff&rev=493436&r1=493435&r2=493436
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Bean.java 
(original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/Bean.java 
Sat Jan  6 01:15:15 2007
@@ -25,6 +25,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import com.opensymphony.xwork2.inject.Inject;
 import com.opensymphony.xwork2.util.ClassLoaderUtil;
 import com.opensymphony.xwork2.ObjectFactory;
 import com.opensymphony.xwork2.util.OgnlUtil;
@@ -98,10 +99,16 @@
 
     protected Object bean;
     protected String name;
+    protected ObjectFactory objectFactory;
 
     public Bean(ValueStack stack) {
         super(stack);
     }
+    
+    @Inject
+    public void setObjectFactory(ObjectFactory objectFactory) {
+        this.objectFactory = objectFactory;
+    }
 
     public boolean start(Writer writer) {
         boolean result = super.start(writer);
@@ -110,7 +117,7 @@
 
         try {
             String beanName = findString(name, "name", "Bean name is required. 
Example: com.acme.FooBean");
-            bean = 
ObjectFactory.getObjectFactory().buildBean(ClassLoaderUtil.loadClass(beanName, 
getClass()), stack.getContext());
+            bean = objectFactory.buildBean(ClassLoaderUtil.loadClass(beanName, 
getClass()), stack.getContext());
         } catch (Exception e) {
             log.error("Could not instantiate bean", e);
 

Modified: 
struts/struts2/trunk/core/src/main/resources/template/simple/select.ftl
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/simple/select.ftl?view=diff&rev=493436&r1=493435&r2=493436
==============================================================================
--- struts/struts2/trunk/core/src/main/resources/template/simple/select.ftl 
(original)
+++ struts/struts2/trunk/core/src/main/resources/template/simple/select.ftl Sat 
Jan  6 01:15:15 2007
@@ -39,17 +39,24 @@
 </#if>
 <@s.iterator value="parameters.list">
         <#if parameters.listKey?exists>
-            <#assign itemKey = stack.findValue(parameters.listKey)/>
+            <#if stack.findString(parameters.listKey)?exists>
+              <#assign itemKey = 
stack.findString(parameters.listKey).toString()/>
+            <#else>
+              <#assign itemKey = ''/>
+            </#if>
         <#else>
-            <#assign itemKey = stack.findValue('top')/>
+            <#assign itemKey = stack.findValue('top').toString()/>
         </#if>
-        <#assign itemKeyStr = itemKey.toString() />
         <#if parameters.listValue?exists>
-            <#assign itemValue = stack.findString(parameters.listValue)/>
+            <#if stack.findString(parameters.listValue)?exists>
+              <#assign itemValue = stack.findString(parameters.listValue)/>
+            <#else>
+              <#assign itemValue = ''/>
+            </#if>
         <#else>
             <#assign itemValue = stack.findString('top')/>
         </#if>
-    <option value="${itemKeyStr?html}"<#rt/>
+    <option value="${itemKey?html}"<#rt/>
         <#if tag.contains(parameters.nameValue, itemKey) == true>
  selected="selected"<#rt/>
         </#if>

Modified: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/SelectTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/SelectTest.java?view=diff&rev=493436&r1=493435&r2=493436
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/SelectTest.java
 (original)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/SelectTest.java
 Sat Jan  6 01:15:15 2007
@@ -208,6 +208,36 @@
 
         verify(SelectTag.class.getResource("Select-1.txt"));
     }
+    
+    public void testSimpleWithNulls() throws Exception {
+        TestAction testAction = (TestAction) action;
+        testAction.setFoo("hello");
+        testAction.setList(new String[][]{
+                {"hello", null},
+                {null, "bar"}
+        });
+
+        SelectTag tag = new SelectTag();
+        tag.setPageContext(pageContext);
+        tag.setEmptyOption("true");
+        tag.setLabel("mylabel");
+        tag.setName("foo");
+        tag.setList("list");
+        tag.setListKey("top[0]");
+        tag.setListValue("top[1]");
+
+        // header stuff
+        tag.setHeaderKey("headerKey");
+        tag.setHeaderValue("headerValue");
+
+        // empty option
+        tag.setEmptyOption("true");
+
+        tag.doStartTag();
+        tag.doEndTag();
+
+        verify(SelectTag.class.getResource("Select-9.txt"));
+    }
 
     public void testExtended() throws Exception {
         TestAction testAction = (TestAction) action;

Added: 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-9.txt
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-9.txt?view=auto&rev=493436
==============================================================================
--- 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-9.txt
 (added)
+++ 
struts/struts2/trunk/core/src/test/resources/org/apache/struts2/views/jsp/ui/Select-9.txt
 Sat Jan  6 01:15:15 2007
@@ -0,0 +1,9 @@
+<tr>
+    <td class="tdLabel"><label for="foo" class="label">mylabel:</label></td>
+    <td><select name="foo" id="foo">
+    <option value="headerKey">headerValue</option>
+    <option value=""></option>
+    <option value="hello" selected="selected"></option>
+    <option value="">bar</option>
+</select></td>
+</tr>


Reply via email to