Commits

Anonymous committed e1b5ce3

Comments (0)

Files changed (2)

src/java/com/opensymphony/webwork/views/jsp/PropertyTag.java

 
     //~ Instance fields ////////////////////////////////////////////////////////
 
+    private String defaultValue;
     private String value;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    public void setDefault(String defaultValue) {
+        this.defaultValue = defaultValue;
+    }
+
     public void setValue(String value) {
         this.value = value;
     }
     public int doStartTag() throws JspException {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
 
-        if (stack != null) {
-            try {
+        try {
+            if ((stack != null) && (stack.size() > 0)) {
+                Object actualValue = null;
+
                 if (value == null) {
-                    pageContext.getOut().print(stack.getRoot().peek());
+                    actualValue = stack.getRoot().peek();
                 } else {
-                    pageContext.getOut().print(stack.findValue(value, String.class));
+                    actualValue = stack.findValue(value, String.class);
+                }
+
+                if (actualValue != null) {
+                    pageContext.getOut().print(actualValue);
+                } else if (defaultValue != null) {
+                    pageContext.getOut().print(defaultValue);
                 }
-            } catch (IOException e) {
-                log.info("Could not print out value '" + value + "': " + e.getMessage());
+            } else if (defaultValue != null) {
+                pageContext.getOut().print(defaultValue);
             }
+        } catch (IOException e) {
+            log.info("Could not print out value '" + value + "': " + e.getMessage());
         }
 
         return EVAL_PAGE;
     public void release() {
         super.release();
         this.value = null;
+        this.defaultValue = null;
     }
 }

src/test/com/opensymphony/webwork/views/jsp/PropertyTagTest.java

 public class PropertyTagTest extends TestCase {
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    public void testDefaultValue() {
+        PropertyTag tag = new PropertyTag();
+
+        Foo foo = new Foo();
+
+        OgnlValueStack stack = new OgnlValueStack();
+        stack.push(foo);
+
+        MockHttpServletRequest request = new MockHttpServletRequest();
+        ActionContext.getContext().setValueStack(stack);
+
+        MockJspWriter jspWriter = new MockJspWriter();
+        jspWriter.setExpectedData("TEST");
+
+        MockPageContext pageContext = new MockPageContext();
+        pageContext.setJspWriter(jspWriter);
+        pageContext.setRequest(request);
+
+        tag.setPageContext(pageContext);
+        tag.setValue("title");
+        tag.setDefault("TEST");
+
+        try {
+            tag.doStartTag();
+        } catch (JspException e) {
+            e.printStackTrace();
+            fail();
+        }
+
+        request.verify();
+        jspWriter.verify();
+        pageContext.verify();
+    }
+
+    public void testNull() {
+        PropertyTag tag = new PropertyTag();
+
+        Foo foo = new Foo();
+
+        OgnlValueStack stack = new OgnlValueStack();
+        stack.push(foo);
+
+        MockHttpServletRequest request = new MockHttpServletRequest();
+        ActionContext.getContext().setValueStack(stack);
+
+        MockJspWriter jspWriter = new MockJspWriter();
+        jspWriter.setExpectedData("");
+
+        MockPageContext pageContext = new MockPageContext();
+        pageContext.setJspWriter(jspWriter);
+        pageContext.setRequest(request);
+
+        tag.setPageContext(pageContext);
+        tag.setValue("title");
+
+        try {
+            tag.doStartTag();
+        } catch (JspException e) {
+            e.printStackTrace();
+            fail();
+        }
+
+        request.verify();
+        jspWriter.verify();
+        pageContext.verify();
+    }
+
     public void testSimple() {
         PropertyTag tag = new PropertyTag();