Play2 Framework初学者。从<select>获取所有选定的元素

时间:2017-02-10 14:28:54

标签: java playframework playframework-2.0

我已经在我的网页上添加了select2 mutliple选择框。 https://select2.github.io/examples.html#multiple 看起来像这样 如何从这个选择字段中获取所有选定的元素以将它们添加到数组?(我将选择的数量限制为5)谢谢! 这是我的代码(我正在使用neo4j图数据库): Html表格: &lt; select class =&#34; form-control select2-multi&#34;命名=&#34;兴趣&#34;多个=#&34;多个&#34;&GT;     @for(兴趣&lt; - interest){         &lt; option value =&#34; @ interest.interestId&#34;&gt; @ interest.interestName&lt; / option&gt;     } &LT; /选择&GT; BusyUser型号: @实体 公共类BusyUser { 公共长途; @ID @ Constraints.Required(message =&#34; *&#34;) @ Constraints.Email(message =&#34;?&#34;) public String用户; @ Constraints.Required(message =&#34; *&#34;) @Column(独特=真) public String visibleUsername; //@短暂的 public String密码; //@Constraints.Required(message =&#34; *&#34;) public String name; //@Constraints.Required(message =&#34; *&#34;) @ Constraints.Email(message =&#34;?&#34;) @Column(独特=真) public String mail; public Boolean active; @Formats.DateTime(pattern =&#34; yyyy-MM-dd HH:mm:ss&#34;) 公共日期lastLogin; public String authToken; public String语言; public BusyUser(){} public BusyUser(节点节点) {     this.id = node.getId();     this.password =(字符串)node.getProperty(ModelGraphProperty.BusyUser.Password,&#34;&#34);     this.mail =(字符串)node.getProperty(ModelGraphProperty.BusyUser.Mail,&#34;&#34);     this.user =(字符串)node.getProperty(ModelGraphProperty.BusyUser.User,&#34;&#34);     this.visibleUsername =(字符串)node.getProperty(ModelGraphProperty.BusyUser.VisibleUsername,&#34;&#34);     this.user =(字符串)node.getProperty(ModelGraphProperty.BusyUser.UserName,&#34;&#34);     this.language =(字符串)node.getProperty(ModelGraphProperty.BusyUser.Language,&#34;&#34);     this.authToken =(字符串)node.getProperty(ModelGraphProperty.BusyUser.Token,&#34;&#34); } Users.class我抓取表单数据: private static final Form&lt; BusyUser&gt; userForm = Form.form(BusyUser.class); //我的保存方法 public static Result save() {     //我从数据库中获取的兴趣列表     列表与LT;利息和GT; interests = NeoDataProvider.getInterests(play.api.i18n.Lang.defaultLang()。language());     Http.MultipartFormData body = request()。body()。asMultipartFormData();     尝试{         形式&LT; BusyUser&GT; boundForm = userForm.bindFromRequest();         if(boundForm.hasErrors()){             flash(&#34;错误&#34;,&#34;!&#34;);             return badRequest(createuser.render(boundForm,interests));         }         BusyUser user = boundForm.get();         user.mail = user.user;         user.active = TRUE;         user.lastLogin = new Date();         user.password的= BusyUser.bytesToHex(BusyUser.getSha512(user.password的));         Node nodeEvent = null;         GraphDatabaseService db = Neo4JHelper.getDatabase();         try(事务tx = db.beginTx())         {             索引&LT;节点&GT; userIndex = db.index()。forNodes(ModelIndex.UsersSecurity);             nodeEvent = db.createNode();             nodeEvent.setProperty(ModelGraphProperty.BusyUser.Active,true);             nodeEvent.setProperty(ModelGraphProperty.BusyUser.Mail,user.mail);             nodeEvent.setProperty(ModelGraphProperty.BusyUser.Password,user.password);             nodeEvent.setProperty(ModelGraphProperty.BusyUser.User,user.mail);             nodeEvent.setProperty(ModelGraphProperty.BusyUser.VisibleUsername,user.visibleUsername);             Node res = userIndex.putIfAbsent(nodeEvent,ModelGraphProperty.BusyUser.Mail,nodeEvent.getProperty(ModelGraphProperty.BusyUser.Mail));             userIndex.add(nodeEvent,ModelGraphProperty.BusyUser.Password,nodeEvent.getProperty(ModelGraphProperty.BusyUser.Password));             userIndex.add(nodeEvent,ModelGraphProperty.BusyUser.User,nodeEvent.getProperty(ModelGraphProperty.BusyUser.User));             //userIndex.putIfAbsent(nodeEvent,ModelGraphProperty.BusyUser.Mail,user.mail);             tx.success();         }         db.shutdown();         闪光灯(&#34;成功&#34 ;,                 String.format(&#34;成功添加用户%s&#34;,user.user));         return redirect(routes.Application.login());     } catch(Exception ex){         flash(&#34;错误&#34;,&#34;!&#34;);         return badRequest(createuser.render(userForm,interests));     } }

1 个答案:

答案 0 :(得分:1)

您只需在表单中写入所有Multiselect数据而不是name =“interestedIn”,然后在您的BusyUser.java中声明name="intrestedIn[]",之后您可以遍历列表并获取数据像

List<Long> intrestedIn= new ArrayList<Long>();