Author: wesw Date: Mon Sep 21 17:29:44 2009 New Revision: 817333 URL: http://svn.apache.org/viewvc?rev=817333&view=rev Log: ww-3215 ActionComponent will now swallow exceptions by default, but if you want to see them, you can specify rethrowException=true
Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/AnotherActionComponentTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestAction.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java?rev=817333&r1=817332&r2=817333&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/components/ActionComponent.java Mon Sep 21 17:29:44 2009 @@ -133,6 +133,7 @@ protected boolean executeResult; protected boolean ignoreContextParams; protected boolean flush = true; + protected boolean rethrowException; public ActionComponent(ValueStack stack, HttpServletRequest req, HttpServletResponse res) { super(stack); @@ -253,7 +254,7 @@ * * @see org.apache.struts2.views.jsp.TagUtils#buildNamespace */ - private void executeAction() { + protected void executeAction() { String actualName = findString(name, "name", "Action name is required. Example: updatePerson"); if (actualName == null) { @@ -290,6 +291,9 @@ } catch (Exception e) { String message = "Could not execute action: " + namespace + "/" + actualName; LOG.error(message, e); + if (rethrowException) { + throw new StrutsException(message, e); + } } finally { // set the old stack back on the request req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack); @@ -327,4 +331,9 @@ public void setFlush(boolean flush) { this.flush = flush; } + + @StrutsTagAttribute(description="Whether an exception should be rethrown, if the target action throws an exception", type="Boolean", defaultValue="false") + public void setRethrowException(boolean rethrowException) { + this.rethrowException = rethrowException; + } } Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestAction.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestAction.java?rev=817333&r1=817332&r2=817333&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestAction.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/TestAction.java Mon Sep 21 17:29:44 2009 @@ -177,6 +177,10 @@ return result; } + public String executeThrowsException() throws Exception { + throw new StrutsException("something went wrong!"); + } + public String doInput() throws Exception { return INPUT; } Added: struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/AnotherActionComponentTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/AnotherActionComponentTest.java?rev=817333&view=auto ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/AnotherActionComponentTest.java (added) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/components/AnotherActionComponentTest.java Mon Sep 21 17:29:44 2009 @@ -0,0 +1,53 @@ +package org.apache.struts2.components; + +import org.apache.struts2.views.jsp.AbstractTagTest; +import org.apache.struts2.TestConfigurationProvider; +import org.apache.struts2.StrutsException; + +/** + * Describe your class here + * + * @author $Author$ + * <p/> + * $Id$ + */ +public class AnotherActionComponentTest extends AbstractTagTest { + + public void testRethrowException() throws Exception { + request.setupGetServletPath(TestConfigurationProvider.TEST_NAMESPACE + "/" + + "foo.action" ); + ActionComponent ac = new ActionComponent(stack, request, response) ; + container.inject(ac); + ac.setNamespace(TestConfigurationProvider.TEST_NAMESPACE); + ac.setName(TestConfigurationProvider.TEST_ACTION_NAME + "!executeThrowsException"); + ac.setRethrowException(true); + boolean exceptionCaught = false; + try { + ac.executeAction(); + } + catch (Exception e) { + if (e instanceof StrutsException) + exceptionCaught = true; + } + assertTrue(exceptionCaught); + } + + public void testDoesNotThrowException() throws Exception { + request.setupGetServletPath(TestConfigurationProvider.TEST_NAMESPACE + "/" + + "foo.action" ); + ActionComponent ac = new ActionComponent(stack, request, response) ; + container.inject(ac); + ac.setNamespace(TestConfigurationProvider.TEST_NAMESPACE); + ac.setName(TestConfigurationProvider.TEST_ACTION_NAME+ "!executeThrowsException"); + ac.setRethrowException(false); + boolean exceptionCaught = false; + try { + ac.executeAction(); + } + catch (Exception e) { + if (e instanceof StrutsException) + exceptionCaught = true; + } + assertTrue(! exceptionCaught); + } +}