如何使用struts,在客户端的jsp里面有类似javascript的alert功能?

02-11-13 startunix

如何在使用struts这个framework时,在web页上像javascript一样在客户端有验证功能?比如当密码输入为空时,直接弹出窗口警告密码域不能为空?? thanks.........

jxb8901
2002-11-14 20:30

我在最近的项目中实现了你想要的功能, 我的做法是修改Struts的errors

标签, 具体做法可参考Struts的errors标签的源代码, 只要将其中的显示

内容改为一段JavaScript脚本.

另外在使用过程中我发现这种显示消息的方式有一点不好的就是, 当用户回退

网页时, 会再次弹出alert对话框.

以下是我写的标签的源代码, 请大家指教!

import java.io.IOException;
import java.util.Iterator;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
import org.apache.struts.taglib.Constants;

/**
 * Check for a valid User logged on in the current session.  If there is no
 * such user, forward control to the login page.
 */

public final class MyErrorsTag extends TagSupport {

    protected String name = Action.ERROR_KEY;
    protected static MessageResources messages =
            MessageResources.getMessageResources(Constants.Package +
            ".LocalStrings");
    protected String locale = Action.LOCALE_KEY;
    protected static Locale defaultLocale = Locale.getDefault();
    protected String bundle = Action.MESSAGES_KEY;


    public int doStartTag() throws JspException {
        // Were any error messages specified?
        ActionErrors errors = new ActionErrors();
        try {
            Object value = pageContext.getAttribute
            (name, PageContext.REQUEST_SCOPE);
            if (value == null) {
                ;
            } else if (value instanceof String) {
                errors.add(ActionErrors.GLOBAL_ERROR,
                           new ActionError((String) value));
            } else if (value instanceof String[]) {
                String keys[] = (String[]) value;
                for (int i = 0; i < keys.length; i++)
                    errors.add(ActionErrors.GLOBAL_ERROR,
                               new ActionError(keys[i]));
            /*} else if (value instanceof ErrorMessages) {
                String keys[] = ((ErrorMessages) value).getErrors();
                if (keys == null)
                    keys = new String[0];
                for (int i = 0; i < keys.length; i++)
                    errors.add(ActionErrors.GLOBAL_ERROR,
                               new ActionError(keys[i]));
            */} else if (value instanceof ActionErrors) {
                errors = (ActionErrors) value;
            } else {
                JspException e = new JspException
                (messages.getMessage("errorsTag.errors",
                value.getClass().getName()));
                RequestUtils.saveException(pageContext, e);
                throw e;
            }
        } catch (Exception e) {
            ;
        }
        if (errors.empty())
            return (EVAL_BODY_INCLUDE);

        // Check for presence of header and footer message keys
        boolean headerPresent =
                RequestUtils.present(pageContext, bundle, locale, "errors.header");
        boolean footerPresent =
                RequestUtils.present(pageContext, bundle, locale, "errors.footer");

        // Render the error messages appropriately
        StringBuffer results = new StringBuffer();
        results.append("<script language=\"JavaScript\" type=\"text/javascript\">\r\n<!--\r\n");
        results.append("window.alert(\"");

        String message = null;
        Iterator reports = errors.get();

        while (reports.hasNext()) {
            ActionError report = (ActionError) reports.next();
            message = RequestUtils.message(pageContext, bundle,
                    locale, report.getKey(),
                    report.getValues());
            if (message != null) {
                results.append(message);
                results.append("\\r\\n");
            }
        }

        //results.delete(results.length()-2, 2); //"\r\n"
        results.append("\");\r\n");
        results.append("// -->\r\n</script>");

        // Print the results to our output writer
        ResponseUtils.write(pageContext, results.toString());

        // Continue processing this page
        return (EVAL_BODY_INCLUDE);

    }
}

jxb8901
2002-11-14 20:31

对不起, 我没有去掉错误的注解!

wait4friend
2002-11-18 20:46

Struts1.1的ValidatorFramework提供了server-end和client-end的双重验证机制,可以同时使用(java and js),也可以单独使用(java or js)。

jxb8901
2002-11-23 20:11

真是太好了, 我现在的项目中很多数据验证的功能都没有做, 这下可省去好多功夫.

现在愈来愈感觉"框架"的强大!!!