use of org.apache.struts2.StrutsException in project struts by apache.
the class ActionComponent method executeAction.
/**
* Execute the requested action. If no namespace is provided, we'll
* attempt to derive a namespace using buildNamespace(). The ActionProxy
* and the namespace will be saved into the instance variables proxy and
* namespace respectively.
*
* @see org.apache.struts2.views.jsp.TagUtils#buildNamespace
*/
protected void executeAction() {
String actualName = findString(name, "name", "Action name is required. Example: updatePerson");
if (actualName == null) {
throw new StrutsException("Unable to find value for name " + name);
}
// handle "name!method" convention.
final String actionName;
final String methodName;
ActionMapping mapping = actionMapper.getMappingFromActionName(actualName);
actionName = mapping.getName();
methodName = mapping.getMethod();
String namespace;
if (this.namespace == null) {
namespace = TagUtils.buildNamespace(actionMapper, getStack(), req);
} else {
namespace = findString(this.namespace);
}
// get the old value stack from the request
ValueStack stack = getStack();
// execute at this point, after params have been set
ActionInvocation inv = ActionContext.getContext().getActionInvocation();
try {
proxy = actionProxyFactory.createActionProxy(namespace, actionName, methodName, createExtraContext(), executeResult, true);
// set the new stack into the request for the taglib to use
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
req.setAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION, Boolean.TRUE);
proxy.execute();
} catch (Exception e) {
String message = "Could not execute action: " + namespace + "/" + actualName;
LOG.error(message, e);
if (rethrowException) {
throw new StrutsException(message, e);
}
} finally {
req.removeAttribute(StrutsStatics.STRUTS_ACTION_TAG_INVOCATION);
// set the old stack back on the request
req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
if (inv != null) {
ActionContext.getContext().withActionInvocation(inv);
}
}
if ((getVar() != null) && (proxy != null)) {
putInContext(proxy.getAction());
}
}
use of org.apache.struts2.StrutsException in project struts by apache.
the class ComponentTest method testComponent_coverageTest.
/**
* Attempt some code coverage tests for {@link Component} that can be achieved without
* too much difficulty.
*/
public void testComponent_coverageTest() {
HashMap<String, Object> propertyMap = new HashMap<>();
Exception exception = new Exception("Generic exception");
Property property = new Property(stack);
ActionComponent actionComponent = new ActionComponent(stack, request, response);
try {
actionComponent.setName("componentName");
// Simulate component attribute with a hyphen in its name.
propertyMap.put("hyphen-keyname-for-coverage", "hyphen-keyname-for-coverage-value");
propertyMap.put("someKeyName", "someKeyValue");
actionComponent.copyParams(propertyMap);
actionComponent.addAllParameters(propertyMap);
try {
actionComponent.findString(null, "fieldName", "errorMessage");
fail("null expr parameter should cause a StrutsException");
} catch (StrutsException se) {
// expected
}
assertNull("completeExpression of a null expression returned non-null result ?", actionComponent.completeExpression(null));
try {
actionComponent.findValue(null, "fieldName", "errorMessage");
fail("null expr parameter should cause a StrutsException");
} catch (StrutsException se) {
// expected
}
try {
actionComponent.findValue("", "fieldName", "errorMessage");
fail("empty expr parameter should cause a StrutsException due to finding a null value");
} catch (StrutsException se) {
// expected
}
assertNotNull("the toString() method with Exception parameter returned null result ?", actionComponent.toString(exception));
assertFalse("Initial performClearTagStateForTagPoolingServers not false ?", actionComponent.getPerformClearTagStateForTagPoolingServers());
actionComponent.setPerformClearTagStateForTagPoolingServers(true);
assertTrue("performClearTagStateForTagPoolingServers false after setting to true ?", actionComponent.getPerformClearTagStateForTagPoolingServers());
} finally {
property.getComponentStack().pop();
}
}
use of org.apache.struts2.StrutsException in project beangle3 by beangle.
the class Component method findValue.
/**
* Evaluates the OGNL stack to find an Object value.
* <p/>
* Function just like <code>findValue(String)</code> except that if the given expression is
* <tt>null</tt/> a error is logged and a <code>RuntimeException</code> is thrown constructed with
* a messaged based on the given field and errorMsg paramter.
*
* @param expr
* OGNL expression.
* @param field
* field name used when throwing <code>RuntimeException</code>.
* @param errorMsg
* error message used when throwing <code>RuntimeException</code> .
* @return the Object found, is never <tt>null</tt>.
* @throws StrutsException
* is thrown in case of not found in the OGNL stack, or
* expression is <tt>null</tt>.
*/
protected Object findValue(String expr, String field, String errorMsg) {
if (expr == null) {
throw fieldError(field, errorMsg, null);
} else {
Object value = null;
Exception problem = null;
try {
value = findValue(expr);
} catch (Exception e) {
problem = e;
}
if (value == null) {
throw fieldError(field, errorMsg, problem);
}
return value;
}
}
Aggregations