Play Framework flash 变量添加逗号以形成数据
我正在 Play 框架(java)中验证表单,并且它在提交后向我的表单值添加逗号。
这是表单:
#{form @doCreate()}
<input type="text" name="session.sessionName" value="${flash['session.sessionName']}"/>
<input type="text" name="session.jumpDate"value="${flash['session.jumpDate']}"/>
<select id="jumpers" multiple="multiple" name="jumpers[]" title="Click to Select a Jumper">
..several options here
</select>
#{/form}
这是表单处理程序:
public static void doCreate(JumpSession session, String[] jumpers) {
validation.required(session.getSessionName()).message("sessionName.required");
validation.required(session.getJumpDate()).message("jumpDate.required");
validation.required(jumpers).message("jumpers.required");
if (validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
validation.keep(); // keep the errors for the next request
create();
}
render();
}
create()
方法呈现与刚刚显示的表单相同的表单:
public static void create() {
boolean isAuthorized = Security.isConnected();
// Get the user details
String userid = Security.connected();
render(isAuthorized, userid);
}
验证一切正常,只是每个字段中的值都添加了一个 ',' 字符验证后结束。如果我提交一个完全空白的表单,它会在每个字段中返回一个逗号(',')字符。如果我仅在一个字段中输入一个值,则返回该值时会在末尾粘贴一个逗号。更重要的是,如果我再次提交,之前的每一个逗号现在都会多出 2 个逗号!
编辑: 我还尝试使用 ${flash['session.jumpTime']?:""}
来确保变量已实际定义。
编辑: 它发生在 params.flash() 方法中。 System.out.println(params.allSimple())
显示没有逗号,但在 params.flash() 之后存储在 flash 中的值,如 System.out.println(flash) 所示, 显示逗号。当我手动将值添加到 flash 时,逗号永远不会出现。
for (Map.Entry<String, String> entry : temp.entrySet())
{
flash.put(entry.getKey(), entry.getValue());
}
这个 hack 解决了我的问题,但我仍然想知道为什么我不能只使用 params.flash()。
I'm validating a form in Play framework(java), and it is adding commas to my form values after submission.
Here's the form:
#{form @doCreate()}
<input type="text" name="session.sessionName" value="${flash['session.sessionName']}"/>
<input type="text" name="session.jumpDate"value="${flash['session.jumpDate']}"/>
<select id="jumpers" multiple="multiple" name="jumpers[]" title="Click to Select a Jumper">
..several options here
</select>
#{/form}
Here's the form handler:
public static void doCreate(JumpSession session, String[] jumpers) {
validation.required(session.getSessionName()).message("sessionName.required");
validation.required(session.getJumpDate()).message("jumpDate.required");
validation.required(jumpers).message("jumpers.required");
if (validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
validation.keep(); // keep the errors for the next request
create();
}
render();
}
The method create()
renders the same form that was just displayed:
public static void create() {
boolean isAuthorized = Security.isConnected();
// Get the user details
String userid = Security.connected();
render(isAuthorized, userid);
}
The validation all works fine except the value in each field has a ',' character added to the end after validation. If I submit a completely blank form, it comes back with a comma(',') character in every field. If I enter a value in only one field, that value comes back with a comma pasted on the end. What's more, if I submit again, for every single comma before there are now 2 more commas!
EDIT:
I've also tried using ${flash['session.jumpTime']?:""}
to make sure the variable is actually defined.
EDIT:
It's happening in the params.flash() method. System.out.println(params.allSimple())
reveals that there are no commas, but the values stored in flash after params.flash(), as revealed by System.out.println(flash), show commas. When I add the values to flash by hand, the commas never show up.
for (Map.Entry<String, String> entry : temp.entrySet())
{
flash.put(entry.getKey(), entry.getValue());
}
This hack solves my issue, but I'd still like to know why I can't just use params.flash().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经发现这个问题了,毕竟和Play无关。我使用的 javascript 库(jQuery 工具)在我不知情的情况下克隆了页面上的元素,从而创建了双重表单提交。由于每个表单元素都加倍,因此值之间用逗号分隔。这在 params 变量中是无法检测到的,因为它只在 .flash() 方法中插入了逗号。
噗。
I've found the issue, and it had nothing to do with Play after all. A javascript library I was using (jQuery tools) was cloning the elements on the page without me knowing, thus creating double form submission. Since every form element was doubled, the values were separated by a comma. This was undetectable in the params variable, because it only inserted the comma in the .flash() method.
Pshaw.