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

📄 logiccontroller.java

📁 Spring simplespring basic,java
💻 JAVA
字号:
package com.fyyk.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * 公共逻辑控制器类,继承AbstractController,分析请求,调用相应的逻辑接口类方法<br>
 *
 * @author ZhangQiang
 * @version 2007-3-14 15:29:10
 */
public class LogicController extends AbstractController
{
    private final Log logger = LogFactory.getLog(getClass());
    private Properties uriMethodMapping;       //请求动作与逻辑方法映射
    private BasicLogic logicClass;            //逻辑接口

    public Properties getUriMethodMapping()
    {
        return uriMethodMapping;
    }

    public void setUriMethodMapping(Properties uriMethodMapping)
    {
        this.uriMethodMapping = uriMethodMapping;
    }

    public BasicLogic getLogicClass()
    {
        return logicClass;
    }

    public void setLogicClass(BasicLogic logicClass)
    {
        this.logicClass = logicClass;
    }

    /**
     * 实现handleRequestInternal方法
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @return ModelAndView
     * @throws Exception 异常
     */
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        try
        {
            //获取URL请求的相对路径信息URI,形如:/servlet/test.do
            String uri = ((request.getPathInfo() == null) ? request.getServletPath() : request.getPathInfo());
            //去掉路径的根字符,形如:servlet/test.do
            uri = uri.substring(1);
            //将URI的后缀去掉,如:servlet/test
            int index = uri.indexOf(".");
            String preUri = (index < 0) ? uri : uri.substring(0, index);

            Class objClass = logicClass.getClass();
            //从servlet-xxx.xml配置文件的uriMethodMapping中读取URI对应的逻辑接口类方法名称
            String logicMethod;
            if (uriMethodMapping != null && uriMethodMapping.containsKey(preUri))
                logicMethod = uriMethodMapping.getProperty(preUri);
            else
                logicMethod = preUri;

            logger.info("logicClass: " + logicClass.toString() + "   logicMethod: " + logicMethod);
            //通过反映射调用逻辑类中的方法,
            Method method = objClass.getMethod(logicMethod, HttpServletRequest.class, HttpServletResponse.class, ServletContext.class);
            return (ModelAndView) method.invoke(logicClass, request, response, this.getServletContext());
        } catch (Exception e)
        {
            //记录并抛出异常
            logger.error(e, e.getCause());
            throw new Exception(e.getCause());
        }
    }
}

⌨️ 快捷键说明

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