Search in sources :

Example 1 with ActionExecution

use of org.eweb4j.mvc.action.ActionExecution in project eweb4j-framework by laiweiwei.

the class EWebServlet method doGet.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Context context = null;
    try {
        // 2
        String err = EWeb4JConfig.start(ConfigConstant.START_FILE_NAME);
        // 启动eweb4j
        if (err != null) {
            this.printHtml(err, response.getWriter());
            return;
        }
        // 1 初始化环境
        context = this.initContext(request, response);
        // 最主要的还是提供给 org.eweb4j.i18n.Lang.java 类使用
        MVC.getThreadLocal().set(context);
        // 设置国际化语言
        Lang.change(request.getLocale());
        // 3.URI解析
        String uri = this.parseURL(request);
        context.setUri(uri);
        // 拿到BaseURL
        parseBaseUrl(context);
        // HTTP Method 解析
        String reqMethod = this.parseMethod(request);
        context.setHttpMethod(reqMethod);
        // 4.外部前置拦截器
        InterExecution before_interExe = new InterExecution("before", context);
        if (before_interExe.findAndExecuteInter()) {
            before_interExe.showErr();
            return;
        }
        // method + uri,用来判断是否有Action与之绑定
        ActionExecution actionExe = new ActionExecution(uri, reqMethod, context);
        if (actionExe.findAction()) {
            // 5.execute the action
            actionExe.execute();
            return;
        }
        // log
        this.normalReqLog(uri);
    } catch (Exception e) {
        e.printStackTrace();
        String info = CommonUtil.getExceptionString(e);
        LogFactory.getMVCLogger(EWebFilter.class).error(info);
        this.printHtml(info, response.getWriter());
    } finally {
        // 清空临时文件
        if (context != null && !context.getUploadMap().isEmpty())
            for (Iterator<Entry<String, List<UploadFile>>> it = context.getUploadMap().entrySet().iterator(); it.hasNext(); ) {
                Entry<String, List<UploadFile>> en = it.next();
                if (en.getValue() == null)
                    continue;
                for (UploadFile f : en.getValue()) {
                    FileUtil.deleteFile(f.getTmpFile());
                }
            }
    }
}
Also used : ServletContext(javax.servlet.ServletContext) Entry(java.util.Map.Entry) UploadFile(org.eweb4j.mvc.upload.UploadFile) InterExecution(org.eweb4j.mvc.interceptor.InterExecution) ActionExecution(org.eweb4j.mvc.action.ActionExecution) Iterator(java.util.Iterator) List(java.util.List) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with ActionExecution

use of org.eweb4j.mvc.action.ActionExecution in project eweb4j-framework by laiweiwei.

the class EWebFilter method doFilter.

/**
	 * 执行Filter
	 */
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    Context context = null;
    try {
        // 2
        String err = EWeb4JConfig.start(ConfigConstant.START_FILE_NAME);
        // 启动eweb4j
        if (err != null) {
            this.printHtml(err, res.getWriter());
            return;
        }
        // 1 初始化环境
        context = this.initContext(request, response);
        // 最主要的还是提供给 org.eweb4j.i18n.Lang.java 类使用
        MVC.getThreadLocal().set(context);
        // 设置国际化语言
        Lang.change(request.getLocale());
        // 3.URI解析
        String uri = this.parseURL(request);
        context.setUri(uri);
        // 拿到BaseURL
        parseBaseUrl(context);
        // HTTP Method 解析
        String reqMethod = this.parseMethod(request);
        context.setHttpMethod(reqMethod);
        // 4.外部前置拦截器
        InterExecution before_interExe = new InterExecution("before", context);
        if (before_interExe.findAndExecuteInter()) {
            before_interExe.showErr();
            return;
        }
        // method + uri,用来判断是否有Action与之绑定
        ActionExecution actionExe = new ActionExecution(uri, reqMethod, context);
        if (actionExe.findAction()) {
            // 5.execute the action
            actionExe.execute();
            return;
        }
        // log
        this.normalReqLog(uri);
        // chain
        chain.doFilter(req, res);
    } catch (Exception e) {
        e.printStackTrace();
        String info = CommonUtil.getExceptionString(e);
        LogFactory.getMVCLogger(EWebFilter.class).error(info);
        this.printHtml(info, res.getWriter());
    } finally {
        // 清空临时文件
        if (context != null && !context.getUploadMap().isEmpty())
            for (Iterator<Entry<String, List<UploadFile>>> it = context.getUploadMap().entrySet().iterator(); it.hasNext(); ) {
                Entry<String, List<UploadFile>> en = it.next();
                if (en.getValue() == null)
                    continue;
                for (UploadFile f : en.getValue()) {
                    FileUtil.deleteFile(f.getTmpFile());
                }
            }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) Entry(java.util.Map.Entry) UploadFile(org.eweb4j.mvc.upload.UploadFile) InterExecution(org.eweb4j.mvc.interceptor.InterExecution) ActionExecution(org.eweb4j.mvc.action.ActionExecution) Iterator(java.util.Iterator) HttpServletResponse(javax.servlet.http.HttpServletResponse) List(java.util.List) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 ActionExecution (org.eweb4j.mvc.action.ActionExecution)2 InterExecution (org.eweb4j.mvc.interceptor.InterExecution)2 UploadFile (org.eweb4j.mvc.upload.UploadFile)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1