Search in sources :

Example 46 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class RestWorkflowInterceptor method doIntercept.

/**
 * Intercept {@link ActionInvocation} and processes the errors using the {@link org.apache.struts2.rest.handler.ContentTypeHandler}
 * appropriate for the request.
 *
 * @return String result name
 */
protected String doIntercept(ActionInvocation invocation) throws Exception {
    Object action = invocation.getAction();
    if (action instanceof ValidationAware) {
        ValidationAware validationAwareAction = (ValidationAware) action;
        if (validationAwareAction.hasErrors()) {
            LOG.debug("Errors on action {}, returning result name 'input'", validationAwareAction);
            ActionMapping mapping = ActionContext.getContext().getActionMapping();
            String method = inputResultName;
            if (postMethodName.equals(mapping.getMethod())) {
                method = newMethodName;
            } else if (putMethodName.equals(mapping.getMethod())) {
                method = editMethodName;
            }
            HttpHeaders info = new DefaultHttpHeaders().disableCaching().renderResult(method).withStatus(validationFailureStatusCode);
            Map<String, Object> errors = new HashMap<>();
            errors.put("actionErrors", validationAwareAction.getActionErrors());
            errors.put("fieldErrors", validationAwareAction.getFieldErrors());
            return manager.handleResult(invocation, info, errors);
        }
    }
    return invocation.invoke();
}
Also used : ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) HashMap(java.util.HashMap) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 47 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testSimpleFieldsJPAAnnotations.

public void testSimpleFieldsJPAAnnotations() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldsJPA", null, null);
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(1, fieldErrors.size());
    assertValue(fieldErrors, "firstName", Collections.singletonList("firstName cannot be null"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 48 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testSimpleFieldsInheritedXML.

public void testSimpleFieldsInheritedXML() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldsXMLChild", null, null);
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(3, fieldErrors.size());
    assertValue(fieldErrors, "firstName", Collections.singletonList("firstName cannot be null"));
    assertValue(fieldErrors, "lastName", Collections.singletonList("lastName cannot be null"));
    assertValue(fieldErrors, "middleName", Collections.singletonList("middleName cannot be null"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 49 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testProgrammaticValidation.

public void testProgrammaticValidation() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleField", null, null);
    SimpleField action = (SimpleField) baseActionProxy.getAction();
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(1, fieldErrors.size());
    assertValue(fieldErrors, "name", Collections.singletonList("name cannot be null"));
    assertTrue(action.isValidateCalled());
    assertTrue(action.isValidateExecuteCalled());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 50 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class OValValidationInterceptorTest method testModelDrivenAction.

public void testModelDrivenAction() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName(null);
    action.getModel().setEmail(null);
    action.getModel().getAddress().setStreet("short");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(3, fieldErrors.size());
    assertValue(fieldErrors, "person.name", Collections.singletonList("person.name cannot be null"));
    assertValue(fieldErrors, "person.email", Collections.singletonList("person.email cannot be null"));
    assertValue(fieldErrors, "person.address.street", Collections.singletonList("person.address.street cannot be shorter than 7 characters"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Aggregations

ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)42 ActionProxy (com.opensymphony.xwork2.ActionProxy)38 List (java.util.List)28 HashMap (java.util.HashMap)23 Map (java.util.Map)8 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 Collection (java.util.Collection)5 ModelDrivenAction (org.apache.struts.beanvalidation.actions.ModelDrivenAction)5 ActionContext (com.opensymphony.xwork2.ActionContext)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ClearableValueStack (com.opensymphony.xwork2.util.ClearableValueStack)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ValidateGroupAction (org.apache.struts.beanvalidation.actions.ValidateGroupAction)2 HttpParameters (org.apache.struts2.dispatcher.HttpParameters)2 Parameter (org.apache.struts2.dispatcher.Parameter)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)1