⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strutsvalidateaction.java

📁 这是一个关于J2EE的开源包common里的许多组件的示例应用程序,可以借鉴.
💻 JAVA
字号:
/**
 * Title : Base Dict Class
 * Description : here Description is the function of class, here maybe multirows    
 * @author        kevin
 * @Version       1.0 
 */

package validator;

import java.text.MessageFormat;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.Form;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorException;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.ValidatorResult;
import org.apache.commons.validator.ValidatorResults;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
//import org.apache.struts.validator.ValidatorForm;

//import org.apache.struts.action.DynaActionForm;

/**
 * Class description goes here.
 * @version 1.0  2005-11-22 
 * @author kevin
 */
public class StrutsValidateAction extends Action
{
	private static ResourceBundle apps = ResourceBundle.getBundle("ApplicationResources");

	/**
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)
	{
		String action = request.getParameter("doAction");
		if(action != null && action.equals("save"))
		{
			StrutsValidatorForm form = (StrutsValidatorForm)actionForm;
			//validate(form);
			System.out.println("=========do validate start=========");
			ActionMessages errors = form.validate(actionMapping, request);
			System.out.println("=========do validate end=========");
			saveErrors(request, errors);
			Iterator iterator = errors.get();
			while(iterator.hasNext())
			{
				ActionMessage error = (ActionMessage)iterator.next();
				System.out.println(error.toString());
			}
			//errors.add("message", new ActionMessage("message.validate.test"));
			//saveErrors(request, errors);
			return actionMapping.findForward("result");
		}
		return actionMapping.findForward("validate");
	}

	public void validate(StrutsValidatorForm form)
	{
		//org.apache.struts.validator.
		//InputStream dtdStream = this.servlet .getServletContext().g;
		ValidatorResources resources = new ValidatorResources();
		Validator validator = new Validator(resources, "validator.ValidateTestForm");

		// Tell the validator which bean to validate against.
		validator.setParameter(Validator.BEAN_PARAM, form);

		ValidatorResults results = null;
		try
		{
			results = validator.validate();
		}
		catch(ValidatorException e)
		{
			System.out.println("validator exception==" + e);
		}
		printResults(form, results, resources);
	}

	/**
	 * Dumps out the Bean in question and the results of validating it.
	 */
	public static void printResults(StrutsValidatorForm bean, ValidatorResults results, ValidatorResources resources)
	{

		boolean success = true;

		// Start by getting the form for the current locale and Bean.
		Form form = resources.getForm(Locale.getDefault(), "StrutsValidatorForm");

		System.out.println("\n\nValidating:");
		System.out.println(bean);

		// Iterate over each of the properties of the Bean which had messages.
		Iterator propertyNames = results.getPropertyNames().iterator();
		while(propertyNames.hasNext())
		{
			String propertyName = (String)propertyNames.next();

			// Get the Field associated with that property in the Form
			Field field = form.getField(propertyName);

			// Look up the formatted name of the field from the Field arg0
			String prettyFieldName = apps.getString(field.getArg(0).getKey());

			// Get the result of validating the property.
			ValidatorResult result = results.getValidatorResult(propertyName);

			// Get all the actions run against the property, and iterate over their names.
			Map actionMap = result.getActionMap();
			Iterator keys = actionMap.keySet().iterator();
			while(keys.hasNext())
			{
				String actName = (String)keys.next();

				// Get the Action for that name.
				ValidatorAction action = resources.getValidatorAction(actName);

				// If the result is valid, print PASSED, otherwise print FAILED
				System.out.println(propertyName + "[" + actName + "] (" + (result.isValid(actName) ? "PASSED" : "FAILED") + ")");

				//If the result failed, format the Action's message against the formatted field name
				if(!result.isValid(actName))
				{
					success = false;
					String message = apps.getString(action.getMsg());
					Object[] args = {prettyFieldName};
					System.out.println("     Error message will be: " + MessageFormat.format(message, args));

				}
			}
		}
		if(success)
		{
			System.out.println("FORM VALIDATION PASSED");
		}
		else
		{
			System.out.println("FORM VALIDATION FAILED");
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -