Fre*_*ind 8 trim playframework
Play会将请求中的参数分配给操作参数,例如:
public static void createArticle(String title, String content) {
}
Run Code Online (Sandbox Code Playgroud)
但它不会修剪它们,所以我们通常在动作中使用这样的代码:
public static void createArticle(String title, String content) {
if(title!=null) title = title.trim();
if(content!=null) content = content.trim();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让游戏自动修剪?
使用自定义绑定器有多种方法可以实现此目的.一种做法是这样的:
注释要修剪的每个参数 @As(binder=TrimmedString.class)
public class Application extends Controller {
public static class TrimmedString implements TypeBinder<String> {
@Override
public Object bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception {
if(value != null) {
value = value.trim();
}
return value;
}
}
public static void index(
@As(binder=TrimmedString.class) String s1,
@As(binder=TrimmedString.class) String s2,
@As(binder=TrimmedString.class) String s3) {
render(s1, s2, s3);
}
}
Run Code Online (Sandbox Code Playgroud)如果这对您来说过于冗长,只需使用@GlobalString 的binder来检查自定义@Trim或@As('trimmed')注释.TypeBinder已经提供了所有注释,因此这应该非常容易实现.
所有这些都可以在自定义绑定下的文档中找到.
| 归档时间: |
|
| 查看次数: |
2305 次 |
| 最近记录: |