天天看點

基于注解的表單生成

package javacommon.util;

import java.lang.annotation.Documented;

import java.lang.annotation.Inherited;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.ElementType;

import java.lang.annotation.Target;

@Documented

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

public @interface ExtField {

    String xtype()default "textfield";

    String store()default "";

    String valueField()default "";

    String displayField()default "";

    String triggerAction()default "";

    String mode()default "";

    String width();

    boolean allowBlank();

    boolean readOnly();

}

Model中的使用方法

    @Column(name = "DJ", unique = false, nullable = true, insertable = true, updatable = true, length = 1)

    @ExtField(xtype="combo", store="gender",valueField = "valuefield",displayField="displayfield",mode="local", triggerAction="all", width="233", allowBlank = false, readOnly = true)

    public java.lang.String getGender() {

        return this.gender;

    }

根據注解輸出json的方法

public void extGenFormFieldByAnnotation() throws IOException

    {

        Users  currentUser = (Users)getRequest().getSession().getAttribute("currentUser");

        Kss kss = kssManager.getById(currentUser.getJsbh());

        List result = new ArrayList(); 

        Class c = Kss.class;

        JavaClass javaClass = new JavaClass(c);

        try

        {

            List list = javaClass.getFields();

            for (int i = 0; i < list.size(); i++)

            {

                JavaField javaField = (JavaField)list.get(i);

                if (javaField.getFieldName().startsWith("ALIAS"))

                {

                        String str = c.getDeclaredField(javaField.getFieldName()).get(null).toString();

                        if (!javaField.getFieldName().substring(6).toLowerCase().equals("id"))

                        {

                            Method method = c.getMethod("get"+StringHelper.makeAllWordFirstLetterUpperCase(javaField.getFieldName().substring(6).toLowerCase()));

                             Map map = new HashMap();

                             if(method.isAnnotationPresent(ExtField.class)) {

                                 ExtField extField = method.getAnnotation(ExtField.class);

                                   if (!extField.xtype().equals("textfield")) {map.put("xtype",extField.xtype());}

                                   if (!extField.store().equals("")) {

                                       map.put("store",ComboStoreUtil.getMapList(extField.store()));

                                   }

                                   if (!extField.valueField().equals("")) {map.put("valueField",extField.valueField());}

                                   if (!extField.displayField().equals("")) {map.put("displayField",extField.displayField());}

                                   if (!extField.mode().equals("")) {map.put("mode",extField.mode());}

                                   if (!extField.triggerAction().equals("")) {map.put("triggerAction",extField.triggerAction());}

                                    map.put("fieldLabel",str);

                                    map.put("name",str);

                                    map.put("width",extField.width());

                                    map.put("allowBlank", extField.allowBlank());

                                    map.put("readOnly", extField.readOnly());

                                    map.put("value",method.invoke(kss));

                             }

                             else {

                                    map.put("width",288);

                            result.add(map);

                        }

                }

            }

        }

        catch (Exception e)

            e.printStackTrace();

        outJsonArray(result);

測試的傳回list方法

import java.sql.Array;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class ComboStoreUtil {

    public static List getMapList(String maptype){

        Map map =  new HashMap();

        map.put("valuefield","m");

        map.put("displayfield","男");

        Map map2 =  new HashMap();

        map2.put("valuefield","f");

        map2.put("displayfield","女");

        result.add(map);

        result.add(map2);

        return result;

combo簡單store定義擴充:

Ext.ux.ComboBox = function(config) {

    if (Ext.isArray(config.store)) {

            var store = [];

            for (var i = 0, len = config.store.length; i < len; i++)

                store[i] = [config.store[i]];

            config.store = new Ext.data.JsonStore({

                        fields : ['valuefield', 'displayfield'],

                        data : config.store

                    });

            config.valueField = 'valuefield';

            config.displayField = 'displayfield';

    Ext.ux.ComboBox.superclass.constructor.call(this, config);

Ext.extend(Ext.ux.ComboBox, Ext.form.ComboBox, {

});

Ext.reg('combo', Ext.ux.ComboBox);

Ext界面代碼

Ext.onReady(function() {

    Ext.BLANK_IMAGE_URL = "http://www.cnblogs.com/widgets/ext-2.2.1/resources/images/default/s.gif";

    Ext.QuickTips.init();

    Ext.form.Field.prototype.msgTarget = 'qtip';

    var simple = new Ext.FormPanel({

                labelWidth : 100, 

                url : 'save-form.do',

                frame : true,

                id : 'dynaForm',

                labelAlign : 'right',

                title : '動态表單',

                bodyStyle : 'padding:5px 5px 0',

                style : 'margin-top:20px;margin-left:'

                        + ((document.body.clientWidth - 550) / 2)

                        + 'px;padding-bottom:20px;',

                width : 550,

                defaults : {

                    width : 230

                },

                defaultType : 'textfield',

                autoLoad : {

                    url : '../JsonAction/extGenFormByAnnotation.do',

                    renderer : {

                        render : function(el, response, updater, callback) {

                            var form;

                            if (form = Ext.getCmp('dynaForm')) {

                                form.add

                                        .apply(

                                                form,

                                                []

                                                        .concat(Ext

                                                                .decode(response.responseText)));

                                form.doLayout();

                            }

                            if (callback)

                                callback();

                    }

                doAutoLoad : function() {

                    var u = this.body.getUpdater();

                    if (this.autoLoad.renderer) {

                        u.showLoadIndicator = false; // important!

                        u.setRenderer(this.autoLoad.renderer);

                        delete this.autoLoad.renderer;

                    u.update(typeof this.autoLoad == 'object'

                                    ? this.autoLoad

                                    : {

                                        url : this.autoLoad

                                    });

                buttons : [{

                            text : '儲存'

                        }, {

                            text : '重置'

                        }]

            });

    simple.render(document.body);