Search in sources :

Example 21 with ValidationAware

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

the class BeanValidationInterceptorTest method testModelDrivenActionEmailField.

public void testModelDrivenActionEmailField() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName("name");
    action.getModel().setEmail("notamail");
    action.getModel().getAddress().setStreet("street");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(1, fieldErrors.size());
    assertTrue(fieldErrors.get("email").size() > 0);
    assertEquals(fieldErrors.get("email").get(0), "emailNotValid");
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) ModelDrivenAction(org.apache.struts.beanvalidation.actions.ModelDrivenAction)

Example 22 with ValidationAware

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

the class BeanValidationInterceptorTest method testModelDrivenActionSkipValidationByInterface.

public void testModelDrivenActionSkipValidationByInterface() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenActionSkipValidationByInterface", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName(null);
    action.getModel().setEmail(null);
    action.getModel().getAddress().setStreet(null);
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(0, fieldErrors.size());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) ModelDrivenAction(org.apache.struts.beanvalidation.actions.ModelDrivenAction)

Example 23 with ValidationAware

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

the class BeanValidationInterceptorTest method testModelDrivenActionSuccess.

public void testModelDrivenActionSuccess() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName("name");
    action.getModel().setEmail("jogep@apache.org");
    action.getModel().getAddress().setStreet("street");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(0, fieldErrors.size());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) ModelDrivenAction(org.apache.struts.beanvalidation.actions.ModelDrivenAction)

Example 24 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project entando-core by entando.

the class RedirectMessageInterceptor method after.

/**
 * If the result is a redirect then store error and messages in the session.
 * @param invocation
 * @param validationAware
 * @throws java.lang.Exception
 */
protected void after(ActionInvocation invocation, ValidationAware validationAware) throws Exception {
    Result result = invocation.getResult();
    if (result != null && (result instanceof ServletRedirectResult || result instanceof ServletActionRedirectResult || result instanceof FrontServletActionRedirectResult)) {
        HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);
        HttpSession session = request.getSession();
        Collection<String> actionErrors = validationAware.getActionErrors();
        if (actionErrors != null && actionErrors.size() > 0) {
            session.setAttribute(ACTION_ERRORS_KEY, actionErrors);
        }
        Collection<String> actionMessages = validationAware.getActionMessages();
        if (actionMessages != null && actionMessages.size() > 0) {
            session.setAttribute(ACTION_MESSAGES_KEY, actionMessages);
        }
        Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
        if (fieldErrors != null && fieldErrors.size() > 0) {
            session.setAttribute(FIELD_ERRORS_KEY, fieldErrors);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRedirectResult(org.apache.struts2.result.ServletRedirectResult) FrontServletActionRedirectResult(org.entando.entando.aps.internalservlet.system.dispatcher.FrontServletActionRedirectResult) HttpSession(javax.servlet.http.HttpSession) ServletActionRedirectResult(org.apache.struts2.result.ServletActionRedirectResult) FrontServletActionRedirectResult(org.entando.entando.aps.internalservlet.system.dispatcher.FrontServletActionRedirectResult) List(java.util.List) Result(com.opensymphony.xwork2.Result) ServletRedirectResult(org.apache.struts2.result.ServletRedirectResult) ServletActionRedirectResult(org.apache.struts2.result.ServletActionRedirectResult) FrontServletActionRedirectResult(org.entando.entando.aps.internalservlet.system.dispatcher.FrontServletActionRedirectResult)

Example 25 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project qi4j-sdk by Qi4j.

the class ConstraintViolationInterceptor method intercept.

@Override
public String intercept(ActionInvocation invocation) throws Exception {
    ActionContext invocationContext = invocation.getInvocationContext();
    ValueStack stack = invocationContext.getValueStack();
    Object action = invocation.getAction();
    if (action instanceof ValidationAware) {
        ValidationAware va = (ValidationAware) action;
        HashMap<Object, Object> propertyOverrides = new HashMap<Object, Object>();
        for (Map.Entry<String, FieldConstraintViolations> fieldViolations : fieldConstraintViolations(invocationContext).entrySet()) {
            addConstraintViolationFieldErrors(stack, va, fieldViolations.getKey(), fieldViolations.getValue());
            propertyOverrides.put(fieldViolations.getKey(), getOverrideExpr(invocation, fieldViolations.getValue()));
        }
        // if there were some errors, put the original (fake) values in place right before the result
        if (!propertyOverrides.isEmpty()) {
            overrideActionValues(invocation, stack, propertyOverrides);
        }
    }
    return invocation.invoke();
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) ValidationAware(com.opensymphony.xwork2.ValidationAware) ActionContext(com.opensymphony.xwork2.ActionContext) Collections.emptyMap(java.util.Collections.emptyMap) HashMap(java.util.HashMap) Map(java.util.Map)

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