Author: musachy Date: Mon Aug 10 22:35:06 2009 New Revision: 802947 URL: http://svn.apache.org/viewvc?rev=802947&view=rev Log: WW-3188 Don't render action errors/messages when have null message
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionError.java struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionMessage.java struts/struts2/trunk/core/src/main/resources/template/simple/actionerror.ftl struts/struts2/trunk/core/src/main/resources/template/simple/actionmessage.ftl struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionErrorTagTest.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionMessageTagTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionError.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionError.java?rev=802947&r1=802946&r2=802947&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionError.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionError.java Mon Aug 10 22:35:06 2009 @@ -25,14 +25,18 @@ import javax.servlet.http.HttpServletResponse; import org.apache.struts2.views.annotations.StrutsTag; +import org.apache.commons.lang.xwork.StringUtils; import com.opensymphony.xwork2.util.ValueStack; +import java.util.Collection; +import java.util.List; + /** * <!-- START SNIPPET: javadoc --> * * Render action errors if they exists the specific layout of the rendering depends on - * the theme itself. + * the theme itself. Empty (null or blank string) errors will not be printed. * * <!-- END SNIPPET: javadoc --> * @@ -64,4 +68,18 @@ return TEMPLATE; } + protected void evaluateExtraParams() { + boolean isEmptyList = true; + Collection<String> actionMessages = (List) findValue("actionErrors"); + if (actionMessages != null) { + for (String message : actionMessages) { + if (StringUtils.isNotBlank(message)) { + isEmptyList = false; + break; + } + } + } + + addParameter("isEmptyList", isEmptyList); + } } Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionMessage.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionMessage.java?rev=802947&r1=802946&r2=802947&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionMessage.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionMessage.java Mon Aug 10 22:35:06 2009 @@ -25,14 +25,18 @@ import javax.servlet.http.HttpServletResponse; import org.apache.struts2.views.annotations.StrutsTag; +import org.apache.commons.lang.xwork.StringUtils; import com.opensymphony.xwork2.util.ValueStack; +import java.util.List; +import java.util.Collection; + /** * <!-- START SNIPPET: javadoc --> * * Render action messages if they exists, specific rendering layout depends on the - * theme itself. + * theme itself. Empty (null or blank string) messages will not be printed. * * <!-- END SNIPPET: javadoc --> * @@ -60,4 +64,19 @@ protected String getDefaultTemplate() { return TEMPLATE; } + + protected void evaluateExtraParams() { + boolean isEmptyList = true; + Collection<String> actionMessages = (List) findValue("actionMessages"); + if (actionMessages != null) { + for (String message : actionMessages) { + if (StringUtils.isNotBlank(message)) { + isEmptyList = false; + break; + } + } + } + + addParameter("isEmptyList", isEmptyList); + } } Modified: struts/struts2/trunk/core/src/main/resources/template/simple/actionerror.ftl URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/simple/actionerror.ftl?rev=802947&r1=802946&r2=802947&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/template/simple/actionerror.ftl (original) +++ struts/struts2/trunk/core/src/main/resources/template/simple/actionerror.ftl Mon Aug 10 22:35:06 2009 @@ -35,7 +35,9 @@ </#if> > <#list actionErrors as error> - <li><span>${error!}</span></li> + <#if error?if_exists != ""> + <li><span>${error!}</span></li> + </#if> </#list> </ul> </#if> \ No newline at end of file Modified: struts/struts2/trunk/core/src/main/resources/template/simple/actionmessage.ftl URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/simple/actionmessage.ftl?rev=802947&r1=802946&r2=802947&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/resources/template/simple/actionmessage.ftl (original) +++ struts/struts2/trunk/core/src/main/resources/template/simple/actionmessage.ftl Mon Aug 10 22:35:06 2009 @@ -20,7 +20,7 @@ * under the License. */ --> -<#if (actionMessages?? && actionMessages?size > 0)> +<#if (actionMessages?? && actionMessages?size > 0 && !parameters.isEmptyList)> <ul<#rt/> <#if parameters.id?if_exists != ""> id="${parameters.id?html}"<#rt/> @@ -35,7 +35,9 @@ </#if> > <#list actionMessages as message> - <li><span>${message!}</span></li> + <#if message?if_exists != ""> + <li><span>${message!}</span></li> + </#if> </#list> </ul> </#if> \ No newline at end of file Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionErrorTagTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionErrorTagTest.java?rev=802947&r1=802946&r2=802947&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionErrorTagTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionErrorTagTest.java Mon Aug 10 22:35:06 2009 @@ -21,12 +21,10 @@ package org.apache.struts2.views.jsp.ui; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; import org.apache.struts2.views.jsp.AbstractUITagTest; +import org.apache.commons.lang.xwork.StringUtils; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; @@ -75,6 +73,19 @@ verify(ActionErrorTagTest.class.getResource("actionerror-2.txt")); } + public void testEmptyErrorList() throws Exception { + + ActionErrorTag tag = new ActionErrorTag(); + tag.setId("someid"); + ((InternalActionSupport)action).setHasActionErrors(true); + ((InternalActionSupport)action).setJustNullElement(true); + tag.setPageContext(pageContext); + tag.doStartTag(); + tag.doEndTag(); + + assertTrue(StringUtils.isBlank(writer.toString())); + } + public Action getAction() { return new InternalActionSupport(); @@ -86,6 +97,11 @@ private static final long serialVersionUID = -4777466640658557661L; private boolean yesActionErrors; + private boolean justNullElement; + + public void setJustNullElement(boolean justNullElement) { + this.justNullElement = justNullElement; + } public void setHasActionErrors(boolean aYesActionErrors) { yesActionErrors = aYesActionErrors; @@ -96,7 +112,9 @@ } public Collection getActionErrors() { - if (yesActionErrors) { + if (justNullElement) { + return Arrays.asList(null); + } else if (yesActionErrors) { List errors = new ArrayList(); errors.add("action error number 1"); errors.add("action error number 2"); Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionMessageTagTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionMessageTagTest.java?rev=802947&r1=802946&r2=802947&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionMessageTagTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/ActionMessageTagTest.java Mon Aug 10 22:35:06 2009 @@ -21,12 +21,10 @@ package org.apache.struts2.views.jsp.ui; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; import org.apache.struts2.views.jsp.AbstractUITagTest; +import org.apache.commons.lang.xwork.StringUtils; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; @@ -60,6 +58,19 @@ verify(ActionMessageTagTest.class.getResource("actionmessage-2.txt")); } + public void testYesActionMessagesWithEmptyMessages() throws Exception { + + ActionMessageTag tag = new ActionMessageTag(); + tag.setId("someid"); + InternalActionSupport internalActionSupport = (InternalActionSupport) action; + internalActionSupport.setJustNullElement(true); + tag.setPageContext(pageContext); + tag.doStartTag(); + tag.doEndTag(); + + assertTrue(StringUtils.isBlank(writer.toString())); + } + public void testNullMessage() throws Exception { ActionMessageTag tag = new ActionMessageTag(); @@ -87,13 +98,20 @@ private static final long serialVersionUID = -3230043189352453629L; private boolean canHaveActionMessage; + private boolean justNullElement; public void setHasActionMessage(boolean canHaveActionMessage) { this.canHaveActionMessage = canHaveActionMessage; } + public void setJustNullElement(boolean justNullElement) { + this.justNullElement = justNullElement; + } + public Collection getActionMessages() { - if (canHaveActionMessage) { + if (justNullElement) { + return Arrays.asList(null); + } else if (canHaveActionMessage) { List messages = new ArrayList(); messages.add("action message number 1"); messages.add("action message number 2");