All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.struts2.interceptor.StrutsConversionErrorInterceptor Maven / Gradle / Ivy

There is a newer version: 6.3.0.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.struts2.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 
 * 

* This interceptor extends {@link ConversionErrorInterceptor} but only adds conversion errors from the ActionContext to * the field errors of the action if the field value is not null, "", or {""} (a size 1 String array with only an empty * String). See {@link ConversionErrorInterceptor} for more information, as well as the Type Conversion documentation. *

* * *

Interceptor parameters:

* * * *
    * *
  • None
  • * *
* * * *

Extending the interceptor:

* * *

* There are no known extension points for this interceptor. *

* * *
 * 
 * <action name="someAction" class="com.examples.SomeAction">
 *     <interceptor-ref name="params"/>
 *     <interceptor-ref name="conversionError"/>
 *     <result name="success">good_result.ftl</result>
 * </action>
 * 
 * 
* * @see com.opensymphony.xwork2.ActionContext#getConversionErrors() * @see ConversionErrorInterceptor */ public class StrutsConversionErrorInterceptor extends ConversionErrorInterceptor { private static final long serialVersionUID = 2759744840082921602L; protected Object getOverrideExpr(ActionInvocation invocation, Object value) { ValueStack stack = invocation.getStack(); try { stack.push(value); return escape(stack.findString("top")); } finally { stack.pop(); } } /** * Returns false if the value is null, "", or {""} (array of size 1 with a blank element). Returns * true otherwise. * * @param propertyName the name of the property to check. * @param value the value to error check. * @return false if the value is null, "", or {""}, true otherwise. */ protected boolean shouldAddError(String propertyName, Object value) { if (value == null) { return false; } if ("".equals(value)) { return false; } if (value instanceof String[]) { String[] array = (String[]) value; if (array.length == 0) { return false; } if (array.length > 1) { return true; } String str = array[0]; if ("".equals(str)) { return false; } } return true; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy