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

📄 scripthelper.java

📁 欢迎使用 FastJsp 开发框架! 编译说明: * 若要生成Api Javadoc文档
💻 JAVA
字号:
// Copyright 2005-2007 onetsoft.com
//
// Licensed 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 com.onetsoft.fastjsp.util;

import com.onetsoft.fastjsp.Form;
import com.onetsoft.fastjsp.valid.FieldValidator;
import com.onetsoft.fastjsp.valid.ValidationMessageProvider;
import com.onetsoft.fastjsp.valid.Validator;

import java.text.MessageFormat;

/**
 * @author <a href="mailto:hgw@onetsoft.com">hgw</a>
 */
public class ScriptHelper {

    private static String FastJsp_JS = "<script type=\"text/javascript\" language=\"javascript\" src=\"{0}" +
            StringUtils.RESOURCE_JS + "FastJsp.js\"></script>";

    private final static String SCRIPT_START = "<script language=\"JavaScript\" type=\"text/javascript\"><!--" + StringUtils.LINE_SEPARATOR;
    private final static String SCRIPT_END = "// --></script>";

    private ValidationMessageProvider provider = null;
    private Form form = null;

    public ScriptHelper(ValidationMessageProvider provider, Form form) {
        this.provider = provider;
        this.form = form;
    }

    /**
     * 取得指定表单的JavaScript输入验证
     *
     * @param provider
     * @param form
     * @return
     */
    public static String getFormScripts(ValidationMessageProvider provider, Form form) {
        return new ScriptHelper(provider, form).getScripts();
    }

    private String getScripts() {
        StringBuffer buf = new StringBuffer(1024).append(SCRIPT_START);
        buf.append("FastJsp.register_form('").append(form.getName()).append("');").append(StringUtils.LINE_SEPARATOR);
        if (form.isClientValidationEnabled())
            renderFormValidationScripts(buf);
        if (form.isFocus())
            renderFormFocus(buf, form);
        buf.append(SCRIPT_END);
        return buf.toString();
    }

    private void renderFormFocus(StringBuffer buf, Form form) {
        FieldValidator[] a = form.getFieldValidators();
        if (a == null)
            return;
        FieldValidator f = null;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != null) {
                f = a[i];
                break;
            }
        }
        if (f != null)
            buf.append("FastJsp.dofocus('").append(form.getName()).append("','").append(f.getField()).append("');");
    }

    private void renderFormValidationScripts(StringBuffer buf) {
        FieldValidator[] a = form.getFieldValidators();
        if (a != null) {
            for (int j = 0; j < a.length; j++) {
                if (a[j] != null)
                    renderFieldValidationScripts(buf, a[j]);
            }
        }
    }

    private void renderFieldValidationScripts(StringBuffer buf, FieldValidator fieldValidator) {
        Validator[] a = fieldValidator.getValidators();
        for (int i = 0; i < a.length; i++) {
                renderValidatorScripts(buf, fieldValidator, a[i]);
        }
    }

    private void renderValidatorScripts(StringBuffer buf, FieldValidator fieldValidator, Validator validator) {
        buf.append("FastJsp.onsubmit('").append(form).append("',function(event) { FastJsp.").append(validator.getJSFunction()).append("(event, '")
                .append(form).append("','").append(fieldValidator.getField()).append('\'');

        String limit = validator.getLimit();
        if (limit.length() > 0) {
            buf.append(',').append(validator.getLimit());
        }

        buf.append(", '").append(MessageFormat.format(validator.getMessagePattern(form.getPage().getLocale(), provider), new Object[]{fieldValidator.getDisplayName()}))
                .append("'); });").append(StringUtils.LINE_SEPARATOR);

    }

    public static String getFastJspScripts(String servletPath) {
        return MessageFormat.format(FastJsp_JS, new Object[]{servletPath});
    }


}

⌨️ 快捷键说明

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