现在我有这样的观点有效。
#{form @UserCreate.create()}
#{field 'user.email'}
#{text field /}
#{/field}
#{/form}
定义了相应的文本标签然后使用该字段(它比这更复杂,只需煮沸到必需品)
<input type="text" id="${_arg.id}" name="${_arg.name}" value="${_arg.value}">
理想情况下,我宁愿拥有它,所以我不必在视图中声明该字段,所以它看起来像这样。
#{form @UserCreate.create()}
#{text 'user.email' /}
#{/form}
标签看起来像这样。
#{field 'user.email'}
<input type="text" id="${field.id}" name="${field.name}" value="${field.value}">
#{/field}
但是field.value总是返回null,因为用户不在标签的模板绑定中。我现在还不确定如何处理事情。建议?
答案 0 :(得分:1)
嘿,我有一个有趣的解决方法: 只需编写自定义标记的第一行
即可 %{user = _user}%
#{field 'user.email'}
<input type="text" id="${field.id}" name="${field.name}" value="${field.value}">
#{/field}
这对我有用:)
答案 1 :(得分:0)
由于#{field}标记的工作方式,我不相信你能做到你想做的事(参见代码here)。
最好的方法可能是根据Field标签的代码创建一系列快速标签,而不需要渲染主体的部分(因为您不需要它),即使它可能意味着一些代码重复。完成后,您可以重构以提取一些常见部分并减少重复。
答案 2 :(得分:0)
我自己修改了字段标签。我仍然必须这样做才能使用它:( :(
#{xfield objectId:'groupDbo.name',label:'Group Name', help:'Some help message', length:'50'}
#{text field/}
#{/xfield}
但我不必至少重复这个HTML ......
<div class="entry">
<div class="spacer"></div>
<span class="label ${_arg.errorClass}">${_arg.label}</span>
<span class="input ${_arg.errorClass}">
<input type="text" class="inputtext" name="${_arg.name}" value="${_arg.value}" maxlength="${_arg.length}">
<a id="newinfo" class="help" title="${_arg.help}">${_arg.help}</a>
<span class="error">${_arg.error}</span>
</span>
<div style="clear: both;"></div>
</div>
这也解决了他们在字段标记中的错误(在1.2.x中),其中没有得到非常烦人的私有字段....(并添加了更多字段,应该存在而不是)
public class TagHelp extends FastTags {
private static final Logger log = LoggerFactory.getLogger(TagHelp.class);
public static void _xfield(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
Map<String,Object> field = new HashMap<String,Object>();
Object objId = args.get("objectId");
if(objId == null || !(objId instanceof String))
throw new IllegalArgumentException("'objectId' param must be supplied to tag xfield as a String");
Object label = args.get("label");
if(label == null || !(label instanceof String))
throw new IllegalArgumentException("'label' param must be supplied to tag xfield as a String");
Object length = args.get("length");
if(length == null || !(length instanceof String))
throw new IllegalArgumentException("'length' param must be supplied to tag xfield as a String");
Object help = args.get("help");
if("".equals(help))
help = null;
String _arg = objId.toString();
Object id = _arg.replace('.','_');
Object flashObj = Flash.current().get(_arg);
Object flashArray = new String[0];
if(flashObj != null && !StringUtils.isEmpty(flashObj.toString()))
flashArray = field.get("flash").toString().split(",");
Object error = Validation.error(_arg);
Object errorClass = error != null ? "hasError" : "";
field.put("name", _arg);
field.put("label", label);
field.put("length", length);
field.put("help", help);
field.put("id", id);
field.put("flash", flashObj);
field.put("flashArray", flashArray);
field.put("error", error);
field.put("errorClass", errorClass);
String[] pieces = _arg.split("\\.");
Object obj = body.getProperty(pieces[0]);
if(obj != null){
if(pieces.length > 1){
for(int i = 1; i < pieces.length; i++){
try{
Field f = getDeclaredField(obj.getClass(), obj, pieces[i]);
f.setAccessible(true);
if(i == (pieces.length-1)){
try{
Method getter = obj.getClass().getMethod("get"+JavaExtensions.capFirst(f.getName()));
field.put("value", getter.invoke(obj, new Object[0]));
}catch(NoSuchMethodException e){
field.put("value",f.get(obj).toString());
}
}else{
obj = f.get(obj);
}
}catch(Exception e){
log.warn("Exception processing tag on object type="+obj.getClass().getName(), e);
}
}
}else{
field.put("value", obj);
}
}
body.setProperty("field", field);
body.call();
}
private static Field getDeclaredField(Class clazz, Object obj, String fieldName) throws NoSuchFieldException {
if(clazz == Object.class)
throw new NoSuchFieldException("There is no such field="+fieldName+" on the object="+obj+" (obj is of class="+obj.getClass()+")");
try {
return clazz.getDeclaredField(fieldName);
} catch(NoSuchFieldException e) {
return getDeclaredField(clazz.getSuperclass(), obj, fieldName);
}
}
}