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

📄 abstractpageservice.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

package com.onetsoft.fastjsp;

import com.onetsoft.fastjsp.request.PageData;
import com.onetsoft.fastjsp.request.multipart.DefaultMultipartDecoder;
import com.onetsoft.fastjsp.request.multipart.MultipartDecoder;
import com.onetsoft.fastjsp.util.StringUtils;
import com.onetsoft.fastjsp.util.Util;
import com.onetsoft.fastjsp.util.Constants;

import java.io.UnsupportedEncodingException;


/**
 * 自定义页面服务应继承此类
 *
 * @author <a href="mailto:hgw@onetsoft.com">hgw</a>
 */
public abstract class AbstractPageService implements PageService {

    AbstractPageCycle cycle = null;

    ActionsManager actionsManager = null;
    ComponentsManager componentsManager = null;
    LayoutManager layoutManager = null;

    AbstractPage page = null;

    AbstractServicer servicer = null;

    MultipartDecoder decoder = null;

    LocaleStateManager localeStateManager = null;

    PageData data = null;

    boolean formRequest = false;

    boolean started = false;

    ValidationDelegateComboImpl delegateCombo = new ValidationDelegateComboImpl();

    protected AbstractPageService(Servicer servicer) {
        this.servicer = (AbstractServicer) servicer;
        attach();
    }


    private void attach() {
        layoutManager = servicer.module.layoutManagerFactory.getLayoutManager(servicer.pageParams);

        /*  try {
            servicer.request.setCharacterEncoding(servicer.module.charset);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/

        /**
         * 此设置经测试只对表单提交有效,而url的参数(静态参数和动态查询参数)的decoding、encoding均以module.charset为准。
         * 注意:这里不强行设置,而留给其他显示设置,如:filter中设置request.setCharacterEncoding("utf-8");满足用户需要“完全统一”编码的情形!
         */
        //    Debug.debug("AbstractServicer.init() Line:88....request.getCharacterEncoding():" + request.getCharacterEncoding());
        // Debug.debug("AbstractServicer.init() Line:90....module.charset:" + module.charset);
        if (servicer.request.getCharacterEncoding() == null) {
            try {
                servicer.request.setCharacterEncoding(Constants.UTF_8);  //utf-8是最安全稳妥的编码
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        //Parse request type.
        // Note:When submitting a form ,the form "method" should be "post",otherwise the "contentType" from request will be null!!!
        String contentType = servicer.request.getContentType();
        if (StringUtils.METHOD_POST.equalsIgnoreCase(servicer.getRequest().getMethod()) && contentType != null &&
                (contentType.startsWith(StringUtils.CONTENTTYPE_FORM_NORMAL)
                        || contentType.startsWith(StringUtils.CONTENTTYPE_FORM_MULTIPART))) {
            formRequest = true;
        }
    }

    public void initialize() {
        cycle = createCycle(this);
        localeStateManager = servicer.module.localeStateManagerFactory.getLocaleStateManager();
        localeStateManager.init(cycle);
    }

    final void start() {
        if (started) return;
        componentsManager = layoutManager.getComponentsManager(servicer.pageParams.pageClass);
        if (!servicer.pageParams.defaultPageUsed) {
            actionsManager = layoutManager.getActionsManager(servicer.pageParams.pageClass);
        }
        try {
            decodeRequest();
        } finally {
            data = new PageDataImpl(servicer, decoder);
            servicer.stateURL.decodeAfterDataBuild(data);
            componentsManager.instanceComponents(cycle);
        }
        try {
            beginRequest();
            if (!servicer.pageParams.defaultPageUsed)
                actionsManager.execute(this);
            componentsManager.initComponentsFields(cycle);
        } finally {
            endRequest();
            started = true;
        }
    }


    /**
     * 启动页面服务会调用此方法
     * 可在这里执行自定义服务过程
     */
    protected void beginRequest() {
    }

    /**
     * 用户请求处理
     *
     * @since 3.0
     */
    protected void endRequest() {
    }

    /**
     * 停止页面服务
     * 一般用于清除当前请求状态以及同步用户状态等
     * 注:每次页面请求执行完毕都必须执行此方法
     *
     * @throws java.io.IOException
     */
    public void end() {
        localeStateManager.commitChange(cycle);
    }

    /**
     * 解析请求参数
     * 所解析的参数包括:标准的url请求参数、表单参数、上传表单参数
     * 注:此方法执行前已经对静态url作了参数解析
     *
     * @see PageParamsImpl
     * @see AbstractServicer
     */
    private void decodeRequest() {
        String contentType = servicer.request.getContentType();
        try {
            if (contentType != null && contentType.startsWith(StringUtils.CONTENTTYPE_FORM_MULTIPART)) {
                decoder = new DefaultMultipartDecoder(servicer.pageParams.parameterMap, servicer.module.getUploadConfig());
                servicer.request = decoder.decode(servicer.request);
            } else {
                ParamsHelper.decodeRequestParameterMap(servicer.request, servicer.pageParams.parameterMap, servicer.module.encoding);
            }
        } finally {
            if (decoder == null)
                decoder = Util.EMPTY_DECODER;
        }
    }


    public PageCycle getCycle() {
        return cycle;
    }


    /**
     * 创建page cycle
     * 所有自定义page cycle应继承{@link AbstractPageCycle}
     *
     * @param pageService
     * @return
     */
    protected abstract AbstractPageCycle createCycle(AbstractPageService pageService);

}

⌨️ 快捷键说明

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