NutzCN Logo
问答 nutz 用来接收form 表单的Adaptor 是哪个 想处理from 表单的数据
发布于 1601天前 作者 qq_5a88e4e5 1407 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz 用来接收form 表单的Adaptor 是哪个 想处理from 表单的数据
其中有空格的去掉

5 回复

获取form 的数据并转成POJO 对象的类是那个呢

不知道为什么 没法进入 MyAdaptor

  @At
    @POST
    @RequiresPermissions("product.brand.contract.add")
    @Desc("产品品牌签约.添加")
    @Ok(HtmlViewMaker.VIEW_JSON)
    @AdaptBy(type = MyAdaptor.class)
    public Object add(@Param("..") ProductBrandContract brand_contract, @Attr("_user_id") int user_id) throws AlertException {
        System.out.println(Json.toJson(brand_contract));
        if (dao.count(ProductBrandContract.class, Cnd.where(ProductBrandContract.BRAND_NAME, "=", brand_contract.getBrand_name())) > 0 ||
                dao.count(ProductBrand.class, Cnd.where(ProductBrand.BRAND_NAME, "=", brand_contract.getBrand_name())) > 0
                ) {
            throw new AlertException("品牌名称不可重复(忽略空格)");
        }

        brand_contract.setCreate_time(SysUtils.current());
        brand_contract.setContract_status(ProductBrandContractStatus.STANDBY.toString());
        brand_contract.setPartner_id(sysUserService.getUser(user_id).getAccount());
        dao.insert(brand_contract);
        sysMsgService.send(RoleEnum.XYD.toString().toLowerCase(), brand_contract.getPartner_id() + "申请签约"
                , brand_contract.getPartner_id() + ":" + "签约  详情" + brand_contract.getContract_desc(), SysMsgLevel.INFO
        );
        return Result.ok();
    }
package com.yixin.project.xyd.module.adaptor;

import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.adaptor.AbstractAdaptor;
import org.nutz.mvc.adaptor.ParamInjector;
import org.nutz.mvc.adaptor.Params;
import org.nutz.mvc.adaptor.injector.NameInjector;
import org.nutz.mvc.annotation.Param;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


public class MyAdaptor  extends AbstractAdaptor {
    private static final Log log = Logs.get();
    protected ParamInjector evalInjectorBy(Type type, Param param) {
        Class<?> clazz = Lang.getTypeClass(type);
        if (clazz == null) {
            if (log.isWarnEnabled())
                log.warnf("!!Fail to get Type Class : type=%s , param=%s", type, param);
            return null;
        }

        Type[] paramTypes = null;
        if (type instanceof ParameterizedType)
            paramTypes = ((ParameterizedType) type).getActualTypeArguments();

        if (null == param)
            return null;

        String defaultValue = null;
        if (param.df() != null && !Params.ParamDefaultTag.equals(param.df()))
            defaultValue = param.df();
        String pm = param.value();
        String datefmt = param.dfmt();
        // POJO
        if ("..".equals(pm)) {
            System.out.println("...................");
            return new MyObjectPairInjector(null, type);


        }
        return getNameInjector(pm, datefmt, type, paramTypes, defaultValue);
    }

    protected ParamInjector getNameInjector(String pm,
                                            String datefmt,
                                            Type type,
                                            Type[] paramTypes,
                                            String defaultValue) {

        return new NameInjector(pm, datefmt, type, paramTypes, defaultValue);
    }

}

package com.yixin.project.xyd.module.adaptor;

import org.nutz.json.Json;
import org.nutz.lang.Mirror;
import org.nutz.lang.Strings;
import org.nutz.lang.born.Borning;
import org.nutz.lang.inject.Injecting;
import org.nutz.mvc.adaptor.ParamConvertor;
import org.nutz.mvc.adaptor.ParamExtractor;
import org.nutz.mvc.adaptor.ParamInjector;
import org.nutz.mvc.adaptor.Params;
import org.nutz.mvc.annotation.Param;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
 * @author yushun
 */
public class MyObjectPairInjector implements ParamInjector {
    protected Injecting[] injs;
    protected String[] names;
    protected Mirror<?> mirror;
    protected Field[] fields;
    protected ParamConvertor[] converters;
    protected Borning<?> borning;
    protected String[] defaultValues;

    public MyObjectPairInjector(String prefix, Type type) {
        prefix = Strings.isBlank(prefix) ? "" : Strings.trim(prefix);
        this.mirror = Mirror.me(type);
        this.borning = mirror.getBorning();
        fields = mirror.getFields();
        this.injs = new Injecting[fields.length];
        this.names = new String[fields.length];
        this.converters = new ParamConvertor[fields.length];

        for (int i = 0; i < fields.length; i++) {
            Field f = fields[i];
            this.injs[i] = mirror.getInjecting(f.getName());
            Param param = f.getAnnotation(Param.class);
            String nm = null == param ? f.getName() : param.value();
            String datefmt = null == param ? null : param.dfmt();
            String locale = null == param ? null : param.locale();
            this.names[i] = prefix + nm;
            this.converters[i] = Params.makeParamConvertor(f.getType(), datefmt, locale);
            if (param != null && !Params.ParamDefaultTag.equals(param.df())) {
                if (defaultValues == null)
                    defaultValues = new String[fields.length];
                defaultValues[i] = param.df();
            }
        }
    }

    public Object get(ServletContext sc,
                      HttpServletRequest req,
                      HttpServletResponse resp,
                      Object refer) {
        ParamExtractor pe = Params.makeParamExtractor(req, refer);
        Object obj = borning.born();
        for (int i = 0; i < injs.length; i++) {
            Object param = converters[i].convert(pe.extractor(names[i]));
            if (param == null && defaultValues != null && defaultValues[i] != null)
                param = defaultValues[i].trim();
            if (null != param)
                injs[i].inject(obj, param);
        }
        System.out.println(Json.toJson(obj));
        return obj;


    }
}

添加回复
请先登陆
回到顶部