Search in sources :

Example 1 with ResultConfigBean

use of org.eweb4j.mvc.config.bean.ResultConfigBean in project eweb4j-framework by laiweiwei.

the class ActionExecution method handleResult.

private void handleResult() throws Exception {
    this.exeActionLog();
    if (retn == null)
        return;
    String baseUrl = (String) this.context.getServletContext().getAttribute(MVCConfigConstant.BASE_URL_KEY);
    if (File.class.isAssignableFrom(retn.getClass())) {
        File file = (File) retn;
        this.handleDownload(file);
        return;
    } else if (File[].class.isAssignableFrom(retn.getClass())) {
        File[] files = (File[]) retn;
        String fileName = CommonUtil.getNowTime("yyyyMMddHHmmss") + "_" + "download.zip";
        HttpServletResponse resp = this.context.getResponse();
        resp.reset();
        resp.setContentType("application/zip");
        resp.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        ServletOutputStream outputStream = resp.getOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (File file : files) {
            byte[] b = new byte[1024];
            int len;
            zip.putNextEntry(new ZipEntry(file.getName()));
            FileInputStream fis = new FileInputStream(file);
            while ((len = fis.read(b)) != -1) {
                zip.write(b, 0, len);
            }
            fis.close();
        }
        zip.flush();
        zip.close();
        outputStream.flush();
        return;
    }
    if (!String.class.isAssignableFrom(retn.getClass())) {
        String mimeType = null;
        boolean hasProduces = JAXWSUtil.hasProduces(method);
        if (hasProduces) {
            String[] mimeTypes = ProducesUtil.getProducesValue(method);
            if (mimeTypes != null && mimeTypes.length > 0)
                mimeType = mimeTypes[0];
        }
        if (mimeType == null || mimeType.trim().length() == 0)
            mimeType = this.context.getRequest().getParameter(MVCConfigConstant.HTTP_HEADER_ACCEPT_PARAM);
        if (mimeType == null || mimeType.trim().length() == 0) {
            String contentType = this.context.getRequest().getContentType();
            if (contentType != null) {
                this.context.getResponse().setContentType(contentType);
                mimeType = contentType.split(";")[0];
            }
        }
        if (this.context.getWriter() == null)
            this.context.setWriter(this.context.getResponse().getWriter());
        if (MIMEType.JSON.equals(mimeType) || "json".equalsIgnoreCase(mimeType)) {
            this.context.getResponse().setContentType(MIMEType.JSON);
            this.context.getWriter().print(CommonUtil.toJson(retn));
        } else if (MIMEType.XML.equals(mimeType) || "xml".equalsIgnoreCase(mimeType)) {
            Class<?> cls = retn.getClass();
            if (Collection.class.isAssignableFrom(cls)) {
                Class<?> _cls = ClassUtil.getPojoClass(this.method);
                if (_cls != null)
                    cls = _cls;
            }
            XMLWriter writer = BeanXMLUtil.getBeanXMLWriter(retn);
            writer.setCheckStatck(true);
            writer.setSubNameAuto(true);
            writer.setClass(cls);
            writer.setRootElementName(null);
            this.context.getResponse().setContentType(MIMEType.XML);
            this.context.getWriter().print(writer.toXml());
        } else {
            //默认都用json
            this.context.getResponse().setContentType(MIMEType.JSON);
            this.context.getWriter().print(CommonUtil.toJson(retn));
        }
        return;
    }
    List<String> produces = this.context.getActionConfigBean().getProduces();
    if (produces != null && produces.size() > 0)
        for (String produce : produces) {
            this.context.getResponse().setContentType(produce);
            break;
        }
    String re = String.valueOf(retn);
    //model driven
    for (Field f : fields) {
        Method getter = ru.getGetter(f.getName());
        if (getter == null)
            continue;
        String name = f.getName();
        if (this.context.getModel().containsKey(name))
            continue;
        this.context.getModel().put(name, getter.invoke(actionObject));
    }
    this.context.getModel().put(MVCConfigConstant.BASE_URL_KEY, this.context.getServletContext().getAttribute(MVCConfigConstant.BASE_URL_KEY));
    this.context.getModel().put(MVCConfigConstant.APPLICATION_SCOPE_KEY, new ServletContextProxy(this.context.getServletContext()).attrs());
    this.context.getModel().put(MVCConfigConstant.SESSION_SCOPE_KEY, new HttpSessionProxy(this.context.getSession()).attrs());
    this.context.getModel().put(MVCConfigConstant.COOKIE_SCOPE_KEY, new CookieProxy(this.context.getRequest().getCookies()).attrs());
    this.context.getModel().put(MVCConfigConstant.REQ_PARAM_SCOPE_KEY, this.context.getQueryParamMap());
    // 客户端重定向
    if (re.startsWith(RenderType.REDIRECT + ":")) {
        String url = re.substring((RenderType.REDIRECT + ":").length());
        String location = url;
        this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));
        return;
    } else if (re.startsWith(RenderType.ACTION + ":")) {
        String path = re.substring((RenderType.ACTION + ":").length());
        // ACTION 重定向
        handleActionRedirect(context, path, baseUrl);
        return;
    } else if (re.startsWith(RenderType.OUT + ":")) {
        String location = re.substring((RenderType.OUT + ":").length());
        this.context.getWriter().print(location);
        return;
    } else if (re.startsWith(RenderType.FORWARD + ":") || re.startsWith(RenderType.JSP + ":") || re.endsWith("." + RenderType.JSP)) {
        String[] str = re.split("@");
        re = str[0];
        String location = re;
        if (re.startsWith(RenderType.FORWARD + ":"))
            location = re.substring((RenderType.FORWARD + ":").length());
        else if (re.startsWith(RenderType.JSP + ":"))
            location = re.substring((RenderType.JSP + ":").length());
        //渲染JSP
        JSPRendererImpl render = new JSPRendererImpl();
        render.setContext(context);
        if (str.length > 1)
            render.layout(str[1]);
        render.target(location).render(context.getWriter(), context.getModel());
        return;
    } else if (re.startsWith(RenderType.FREEMARKER + ":") || re.startsWith(RenderType.FREEMARKER2 + ":") || re.endsWith("." + RenderType.FREEMARKER2)) {
        String[] str = re.split("@");
        re = str[0];
        String location = re;
        if (re.startsWith(RenderType.FREEMARKER + ":"))
            location = re.substring((RenderType.FREEMARKER + ":").length());
        else if (re.startsWith(RenderType.FREEMARKER2 + ":"))
            location = re.substring((RenderType.FREEMARKER2 + ":").length());
        //渲染Freemarker
        Renderer render = RenderFactory.create(RenderType.FREEMARKER).target(location);
        if (str.length > 1)
            render.layout(str[1]);
        render.render(context.getWriter(), context.getModel());
        //	        this.context.getWriter().flush();
        return;
    } else if (re.startsWith(RenderType.VELOCITY + ":") || re.startsWith(RenderType.VELOCITY2 + ":") || re.endsWith("." + RenderType.VELOCITY2)) {
        String[] str = re.split("@");
        re = str[0];
        String location = re;
        if (re.startsWith(RenderType.VELOCITY + ":"))
            location = re.substring((RenderType.VELOCITY + ":").length());
        else if (re.startsWith(RenderType.VELOCITY2 + ":"))
            location = re.substring((RenderType.VELOCITY2 + ":").length());
        //渲染Velocity
        Renderer render = RenderFactory.create(RenderType.VELOCITY).target(location);
        if (str.length > 1)
            render.layout(str[1]);
        render.render(context.getWriter(), context.getModel());
        //	        this.context.getWriter().flush();
        return;
    } else {
        List<ResultConfigBean> results = this.context.getActionConfigBean().getResult();
        if (results == null || results.size() == 0) {
            this.context.getWriter().print(retn);
            //				this.context.getWriter().flush();
            return;
        }
        boolean isOut = true;
        for (ResultConfigBean r : results) {
            if (!"_props_".equals(r.getName()) && !r.getName().equals(re) && !"".equals(re)) {
                continue;
            }
            isOut = false;
            String type = r.getType();
            String location = r.getLocation();
            if (RenderType.REDIRECT.equalsIgnoreCase(type)) {
                this.context.getResponse().sendRedirect(CommonUtil.replaceChinese2Utf8(location));
                return;
            } else if (RenderType.FORWARD.equalsIgnoreCase(type) || RenderType.JSP.equalsIgnoreCase(type)) {
                //渲染JSP
                String[] str = location.split("@");
                JSPRendererImpl render = new JSPRendererImpl();
                render.setContext(context);
                if (str.length > 1)
                    render.layout(str[1]);
                render.target(str[0]).render(context.getWriter(), context.getModel());
                return;
            } else if (RenderType.FREEMARKER.equalsIgnoreCase(type) || RenderType.FREEMARKER2.equalsIgnoreCase(type)) {
                //渲染Freemarker
                String[] str = location.split("@");
                Renderer render = RenderFactory.create(RenderType.FREEMARKER).target(str[0]);
                if (str.length > 1)
                    render.layout(str[1]);
                render.render(context.getWriter(), context.getModel());
                return;
            } else if (RenderType.VELOCITY.equalsIgnoreCase(type) || RenderType.VELOCITY2.equalsIgnoreCase(type)) {
                //渲染Velocity
                String[] str = location.split("@");
                Renderer render = RenderFactory.create(RenderType.VELOCITY).target(str[0]);
                if (str.length > 1)
                    render.layout(str[1]);
                render.render(context.getWriter(), context.getModel());
                return;
            } else if (RenderType.ACTION.equalsIgnoreCase(type)) {
                // ACTION 重定向
                handleActionRedirect(context, location, baseUrl);
                return;
            } else if (RenderType.OUT.equalsIgnoreCase(type) || location.trim().length() == 0) {
                this.context.getWriter().print(location);
                return;
            }
        }
        if (isOut) {
            this.context.getWriter().print(retn);
        //				this.context.getWriter().flush();
        }
    }
}
Also used : ServletContextProxy(org.eweb4j.mvc.ServletContextProxy) ServletOutputStream(javax.servlet.ServletOutputStream) ZipEntry(java.util.zip.ZipEntry) HttpServletResponse(javax.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) CookieProxy(org.eweb4j.mvc.CookieProxy) XMLWriter(org.eweb4j.util.xml.XMLWriter) FileInputStream(java.io.FileInputStream) Field(java.lang.reflect.Field) HttpSessionProxy(org.eweb4j.mvc.HttpSessionProxy) ZipOutputStream(java.util.zip.ZipOutputStream) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) JSPRendererImpl(org.eweb4j.mvc.view.JSPRendererImpl) Renderer(org.eweb4j.mvc.view.Renderer) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) UploadFile(org.eweb4j.mvc.upload.UploadFile) File(java.io.File)

Example 2 with ResultConfigBean

use of org.eweb4j.mvc.config.bean.ResultConfigBean in project eweb4j-framework by laiweiwei.

the class CheckConfigBean method checkMVCResultPart.

/**
	 * Check the Result part of MVC components configuration
	 * 
	 * @param rList
	 * @return
	 */
public static String checkMVCResultPart(List<ResultConfigBean> rList, String beanID, String xmlFile) {
    String error = null;
    ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
    if ("true".equalsIgnoreCase(cb.getMvc().getOpen()) || "1".equals(cb.getMvc().getOpen())) {
        StringBuilder sb = new StringBuilder();
        for (Iterator<ResultConfigBean> it = rList.iterator(); it.hasNext(); ) {
            ResultConfigBean r = it.next();
            if (!"forward".equalsIgnoreCase(r.getType()) && !"redirect".equalsIgnoreCase(r.getType()) && !"out".equalsIgnoreCase(r.getType()) && !"action".equalsIgnoreCase(r.getType()) && !"".equals(r.getType())) {
                sb.append("当前您填写的:( type=").append(r.getType()).append(" )是错误的!它只能填写为:forward|redirect|out|action|留空  中的一种 ;").append("\n");
            }
        }
        if (!"".equals(sb.toString())) {
            error = "\n<br /><b>" + xmlFile + "[bean name=" + beanID + "][result]:</b>\n" + sb.toString();
        }
    }
    return error;
}
Also used : ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) DBInfoConfigBean(org.eweb4j.orm.dao.config.bean.DBInfoConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) IOCConfigBean(org.eweb4j.ioc.config.bean.IOCConfigBean) LogConfigBean(org.eweb4j.config.bean.LogConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean) ConfigBean(org.eweb4j.config.bean.ConfigBean) InterConfigBean(org.eweb4j.mvc.config.bean.InterConfigBean) ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean)

Example 3 with ResultConfigBean

use of org.eweb4j.mvc.config.bean.ResultConfigBean in project eweb4j-framework by laiweiwei.

the class ActionAnnotationConfig method parseDefaultActionConfig.

/**
	 * 解析默认的Action配置
	 * 
	 * @param methodReqMapVal
	 * @param moduleName
	 * @return
	 */
private static ActionConfigBean parseDefaultActionConfig(String methodName, String modelName) {
    String moduleName = null;
    if (modelName.equals("/")) {
        moduleName = "";
    } else {
        moduleName = modelName.endsWith("/") ? modelName.substring(0, modelName.lastIndexOf("/")) : modelName;
    }
    String uriMapping = null;
    String httpMethod = null;
    ActionConfigBean acb = new ActionConfigBean();
    if (ActionMethod.INDEX.equals(methodName)) {
        uriMapping = "/";
        httpMethod = Http.Method.GET;
        ResultConfigBean rcb = new ResultConfigBean();
        rcb.setName("jsp");
        rcb.setLocation(moduleName + "/view/index.jsp");
        acb.getResult().add(rcb);
        ResultConfigBean rcb2 = new ResultConfigBean();
        rcb2.setName("html");
        rcb2.setType(RenderType.FREEMARKER);
        rcb2.setLocation(moduleName + "/view/index.html");
        acb.getResult().add(rcb2);
    } else if (ActionMethod.CREATE.equals(methodName)) {
        uriMapping = "/";
        httpMethod = Http.Method.POST;
        ResultConfigBean rcb = new ResultConfigBean();
        rcb.setName(ActionMethod.INDEX);
        rcb.setLocation(moduleName);
        rcb.setType(RenderType.ACTION);
        acb.getResult().add(rcb);
    } else if (ActionMethod.UPDATE.equals(methodName)) {
        uriMapping = "/{id}";
        httpMethod = Http.Method.PUT;
        ResultConfigBean rcb = new ResultConfigBean();
        rcb.setName(ActionMethod.INDEX);
        rcb.setLocation(moduleName);
        rcb.setType(RenderType.ACTION);
        acb.getResult().add(rcb);
    } else if (ActionMethod.SHOW.equals(methodName)) {
        uriMapping = "/{id}";
        httpMethod = Http.Method.GET;
        ResultConfigBean rcb = new ResultConfigBean();
        rcb.setName("jsp");
        rcb.setLocation(moduleName + "/view/show.jsp");
        acb.getResult().add(rcb);
        ResultConfigBean rcb2 = new ResultConfigBean();
        rcb2.setName("html");
        rcb2.setType(RenderType.FREEMARKER);
        rcb2.setLocation(moduleName + "/view/show.html");
        acb.getResult().add(rcb2);
    } else if (ActionMethod.EDIT.equals(methodName)) {
        uriMapping = "/{id}/edit";
        httpMethod = Http.Method.GET;
        ResultConfigBean rcb = new ResultConfigBean();
        rcb.setName("jsp");
        rcb.setLocation(moduleName + "/view/edit.jsp");
        acb.getResult().add(rcb);
        ResultConfigBean rcb2 = new ResultConfigBean();
        rcb2.setName("html");
        rcb2.setType(RenderType.FREEMARKER);
        rcb2.setLocation(moduleName + "/view/edit.html");
        acb.getResult().add(rcb2);
    } else if (ActionMethod.DESTROY.equals(methodName)) {
        uriMapping = "/{id}";
        httpMethod = Http.Method.DELETE;
        ResultConfigBean rcb = new ResultConfigBean();
        rcb.setName(ActionMethod.INDEX);
        rcb.setLocation(moduleName);
        rcb.setType(RenderType.ACTION);
        acb.getResult().add(rcb);
    } else if (ActionMethod.NEW.equals(methodName)) {
        uriMapping = "/new";
        httpMethod = Http.Method.GET;
        ResultConfigBean rcb = new ResultConfigBean();
        rcb.setName("jsp");
        rcb.setLocation(moduleName + "/view/new.jsp");
        acb.getResult().add(rcb);
        ResultConfigBean rcb2 = new ResultConfigBean();
        rcb2.setName("html");
        rcb2.setType(RenderType.FREEMARKER);
        rcb2.setLocation(moduleName + "/view/new.html");
        acb.getResult().add(rcb2);
    } else {
        acb = null;
    }
    if (acb != null) {
        acb.setHttpMethod(httpMethod);
        acb.setUriMapping(uriMapping);
    }
    return acb;
}
Also used : ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean)

Example 4 with ResultConfigBean

use of org.eweb4j.mvc.config.bean.ResultConfigBean in project eweb4j-framework by laiweiwei.

the class MVCConfigBeanCreator method getActionBean.

public static ActionConfigBean getActionBean() {
    ActionConfigBean mvcBean = null;
    mvcBean = new ActionConfigBean();
    List<ResultConfigBean> rlist = new ArrayList<ResultConfigBean>();
    ResultConfigBean result = new ResultConfigBean();
    rlist.add(result);
    mvcBean.setResult(rlist);
    List<ValidatorConfigBean> vlist = new ArrayList<ValidatorConfigBean>();
    ValidatorConfigBean validator = new ValidatorConfigBean();
    List<FieldConfigBean> fieldList = new ArrayList<FieldConfigBean>();
    FieldConfigBean field = new FieldConfigBean();
    List<ParamConfigBean> paramList = new ArrayList<ParamConfigBean>();
    ParamConfigBean param = new ParamConfigBean();
    paramList.add(param);
    field.setParam(paramList);
    fieldList.add(field);
    validator.setField(fieldList);
    vlist.add(validator);
    mvcBean.setValidator(vlist);
    mvcBean.setParam(paramList);
    return mvcBean;
}
Also used : ValidatorConfigBean(org.eweb4j.mvc.config.bean.ValidatorConfigBean) ParamConfigBean(org.eweb4j.mvc.config.bean.ParamConfigBean) FieldConfigBean(org.eweb4j.mvc.config.bean.FieldConfigBean) ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) ArrayList(java.util.ArrayList) ActionConfigBean(org.eweb4j.mvc.config.bean.ActionConfigBean)

Example 5 with ResultConfigBean

use of org.eweb4j.mvc.config.bean.ResultConfigBean in project eweb4j-framework by laiweiwei.

the class ResultAnnUtil method readResultAnn.

/**
	 * 读@Result注解的信息到配置Cache中
	 * 
	 * @param resultAnn
	 * @return
	 */
public static List<ResultConfigBean> readResultAnn(Result resultAnn) {
    List<ResultConfigBean> rList = new ArrayList<ResultConfigBean>();
    String[] name = resultAnn.name();
    String[] type = resultAnn.type();
    String[] location = resultAnn.value();
    for (int a = 0; a < name.length; ++a) {
        ResultConfigBean r = new ResultConfigBean();
        r.setName(CommonUtil.parsePropValue(name[a]));
        r.setType(CommonUtil.parsePropValue(type[a]));
        r.setLocation(CommonUtil.parsePropValue(location[a]));
        rList.add(r);
    }
    return rList;
}
Also used : ResultConfigBean(org.eweb4j.mvc.config.bean.ResultConfigBean) ArrayList(java.util.ArrayList)

Aggregations

ResultConfigBean (org.eweb4j.mvc.config.bean.ResultConfigBean)5 ArrayList (java.util.ArrayList)3 ActionConfigBean (org.eweb4j.mvc.config.bean.ActionConfigBean)3 FieldConfigBean (org.eweb4j.mvc.config.bean.FieldConfigBean)2 ParamConfigBean (org.eweb4j.mvc.config.bean.ParamConfigBean)2 ValidatorConfigBean (org.eweb4j.mvc.config.bean.ValidatorConfigBean)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 List (java.util.List)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ConfigBean (org.eweb4j.config.bean.ConfigBean)1 LogConfigBean (org.eweb4j.config.bean.LogConfigBean)1 IOCConfigBean (org.eweb4j.ioc.config.bean.IOCConfigBean)1 CookieProxy (org.eweb4j.mvc.CookieProxy)1