“约定大于配置”是WebX的一大设计思想,今天我们就来看看WebX中有哪些约定。
从一张表单看起
timemanage.vm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| $page.setTitle("TimeManage")
<form action="$app1Link.setTarget("form/timemanage")" method="post"> $csrfToken.hiddenField <input type="hidden" name="action" value="insert_time_record_action" />
#set($group = $form.timeRecord.defaultInstance)
#if($!group) <p>$!group</p> #end
<p>请插入一条时间记录</p>
<p> beginTime: <select name="$group.beginTimePicker.key" required> #foreach(${i} in [0..23]) <option value="${i}">${i}:00</option> <option value="${i}">${i}:30</option> #end </select> endTime: <select name="$group.endTimePicker.key" required> #foreach(${i} in [0..23]) <option value="${i}">${i}:00</option> <option value="${i}">${i}:30</option> #end </select> action desc: <input type="text" name="$group.actionDesc.key" placeholder="简要描述(必填)" required="required"/> action type: <select name="$group.actionType.key" required> <option value="1">Enjoy play</option> <option value="2">Rest</option> <option value="3">Quality work</option> <option value="4">Mandatory</option> <option value="5">Procrastination</option> </select> <input type="submit" name="event_submit_do_insert" /> </p> </form>
|
这是一张简单的Velocity表单。如何使用后端接口处理呢?
首先是包名的约定,看表单中的隐藏类型input标签,标识了name为action,这代表是action包下创建类。
其次是类名的约定,要求和.vm文件以及input标签中的value同名,即 InsertTimeRecordAction.java
。
再是处理请求的方法名,观察提交标签为event_submit_do_insert
,前面是固定前缀event_submit_do
,那么方法名就是do后面的单词,即insert
。
然后方法入参,使用FormGroup注解接收表单,如 @FormGroup("timeRecord")
,此时在vm中定义表单还不够,需要在WEB-INF/app1/form.xml
下定义表单组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <group name="timeRecord" extends="csrfCheck"> <field name="beginTimePicker" displayName="开始时间"> <fm-validators:required-validator> <message>必须填写 ${beginTimePicker}</message> </fm-validators:required-validator> </field> <field name="endTimePicker" displayName="结束时间"> <fm-validators:required-validator> <message>必须填写 ${endTimePicker}</message> </fm-validators:required-validator> </field> <field name="actionDesc" displayName="做了什么事"> <fm-validators:required-validator> <message>必须填写 ${actionDesc}</message> </fm-validators:required-validator> </field> <field name="actionType" displayName="做事类型"> <fm-validators:required-validator> <message>必须填写 ${actionType}</message> </fm-validators:required-validator> </field> </group>
|
应用配置
在WEB-INF文件夹下新建webx-你的应用名.xml
评论
shortname
for Disqus. Please set it in_config.yml
.