-
Notifications
You must be signed in to change notification settings - Fork 64
ModelGenerator
Since 1.2.1
ExtDirectSpring contains a simple generator that inspects java classes and creates the corresponding Javascript code for Model objects. The generator is able to create code for Ext JS 4.x and Sencha Touch 2.x.
To add the generator to your application create a @Controller class and use ModelGenerator.writeModel to write the Javascript code into the response. The important part here is the url in the @RequestMapping annotation. If the application uses Ext.Loader to load classes on demand this has to match the path to the directory where the models are normally stored.
@Controller
public class ModelController {
@RequestMapping("/app/model/User.js")
public void user(HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelGenerator.writeModel(request, response, User.class, OutputFormat.EXTJS4);
//or for touch 2
//ModelGenerator.writeModel(request, response, User.class, OutputFormat.TOUCH2);
}
}
This code writes the generated Javascript on one line. To make debugging easier writeModel supports a fifth parameter (debug). If true the generator writes the code in pretty format:
ModelGenerator.writeModel(request, response, User.class, OutputFormat.EXTJS4, true);
Example
Provided with this class
package test;
public class User {
private Integer id;
private String firstName;
private String lastName;
private String email;
private String city;
//get and set methods
//....
}
the generator creates this Ext JS code
Ext.define('test.User', {
extend: 'Ext.data.Model',
fields: [ {
name: 'id',
type: 'int'
}, {
name: 'firstName',
type: 'string'
}, {
name: 'lastName',
type: 'string'
}, {
name: 'email',
type: 'string'
}, {
name: 'city',
type: 'string'
} ]
});
The fields of the class do not have to be annotated with a special annotation. The generator tries to read all public accessible properties. If a field should not be part of the generated code annotate it with @JsonIgnore. As default the generator takes the fully qualified name of the class as the name of the model object and the name of the property as the name of the field.
Datatypes are mapped according to this list if not overwritten with the @ModelField annotation (see below)
| Java | Ext JS/Sencha Touch |
|---|---|
| Byte | int |
| Short | int |
| Integer | int |
| Long | int |
| BigInteger | int |
| byte | int |
| short | int |
| int | int |
| long | int |
| Float | float |
| Double | float |
| BigDecimal | float |
| float | float |
| double | float |
| String | string |
| Date | date |
| java.sql.Date | date |
| Timestamp | date |
| org.joda.time.DateTime | date |
| org.joda.time.LocalDate | date |
| Boolean | boolean |
| boolean | boolean |
Part of the generator are the annotations @ModelField and @Model. With these two annotations the default behaviour can be overwritten and additional informations can be specified that are written to the model object source code.
@Model
| Attribute | Description |
|---|---|
| value | "Classname" of the model. See Ext.data.Model. If not present full qualified name of the class is used. |
| idProperty | Name of the id property. See Ext.data.Model#idProperty. If not present default value of 'id' is used. |
| paging | Set this to true if the read method returns an instance of ExtDirectStoreReadResult. This adds reader: { root: 'records' } to the proxy configuration |
| readMethod | Specifies the read method. This is a ExtDirect reference in the form action.methodName. See Ext.data.proxy.Direct#api. If only the readMethod is specified generator will write property directFn instead. |
| createMethod | Specifies the create method. This is a ExtDirect reference in the form action.methodName. See Ext.data.proxy.Direct#api. |
| updateMethod | Specifies the update method. This is a ExtDirect reference in the form action.methodName. See Ext.data.proxy.Direct#api. |
| destroyMethod | Specifies the destroy method. This is a ExtDirect reference in the form action.methodName. See Ext.data.proxy.Direct#api. |
@ModelField
| Attribute | Description |
|---|---|
| value | Name of the field. Property name. If not present the name of the property is used. |
| type | Type of the field. Property type. If not specified the library uses the list above to determine the type. |
| defaultValue | Default value. Property defaultValue. |
| dateFormat | Specifies the format of date. Property dateFormat. For a list of all supported formats see Sencha Doc: Ext.Date. Will be ignored if the field is not a date field. |
| useNull | If true null value is used if the value cannot be parsed. If false default values are used (0 for integer and float, "" for string and false for boolean). Property useNull.Only used if type of field is int, float, string or boolean. |
Example
Java class
@Model(value = "MyApp.Bean", idProperty = "myVerySpecialId",
paging = true, readMethod = "beanService.read",
createMethod = "beanService.create", updateMethod = "beanService.update",
destroyMethod = "beanService.destroy")
public class Bean {
@ModelField("myVerySpecialId")
private int id;
private String name;
@ModelField(dateFormat = "c")
private Date dateOfBirth;
@JsonIgnore
private String password;
@ModelField(type = ModelType.STRING, useNull = true)
private Long something;
@ModelField(value = "active", defaultValue = "true")
private boolean flag;
//get and set methods
//....
}
Sencha Touch 2.x model object
Ext.define('MyApp.Bean', {
extend : 'Ext.data.Model',
config : {
idProperty : 'myVerySpecialId',
fields : [ {
name : 'myVerySpecialId',
type : 'int'
}, {
name : 'name',
type : 'string'
}, {
name : 'dateOfBirth',
type : 'date',
dateFormat : 'c'
}, {
name : 'something',
type : 'string',
useNull : true
}, {
name : 'active',
type : 'boolean',
defaultValue : true
} ],
proxy : {
type : 'direct',
api : {
read : beanService.read,
create : beanService.create,
update : beanService.update,
destroy : beanService.destroy
},
reader : {
root : 'records'
}
}
}
});
In the current version the generator does not support associations.
- Introduction
- Changelog 1.7.x
- Setup Maven
- Configuration
- Server Methods
- Model Generator
- Model Generator APT
- Development
- Links