繁体   English   中英

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

[英]Play2 Framework beginner. Get all the selected elements from <select>

我已将 select2 多选框添加到我的网页中。 https://select2.github.io/examples.html#multiple

看起来像这样

我怎样才能从这个选择字段中获取所有选定的元素以将它们添加到数组中?(我将选择数量限制为 5)谢谢!

这是我的代码(我使用的是 neo4j 图形数据库):

html格式:

<select class="form-control select2-multi" name="interest" multiple="multiple">
    @for(interest <- interests){
        <option value="@interest.interestId">@interest.interestName</option>
    }
</select>

忙碌用户模型:

@Entity
public class    BusyUser {
public long id;
@Id
@Constraints.Required(message = "*")
@Constraints.Email(message = "?")
public String user;
@Constraints.Required(message = "*")
@Column(unique=true)
public String visibleUsername;
//@Transient
public String password;

//@Constraints.Required(message = "*")
public String name;
//@Constraints.Required(message = "*")
@Constraints.Email(message = "?")
@Column(unique=true)
public String mail;

public Boolean active;
@Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:ss")
public Date lastLogin;

public String authToken;
public String language;

public BusyUser(){}

public BusyUser(Node node)
{
    this.id=node.getId();
    this.password=(String)node.getProperty(ModelGraphProperty.BusyUser.Password,"");
    this.mail=(String)node.getProperty(ModelGraphProperty.BusyUser.Mail,"");
    this.user=(String)node.getProperty(ModelGraphProperty.BusyUser.User,"");
    this.visibleUsername=(String)node.getProperty(ModelGraphProperty.BusyUser.VisibleUsername,"");
    this.user=(String)node.getProperty(ModelGraphProperty.BusyUser.UserName,"");
    this.language=(String)node.getProperty(ModelGraphProperty.BusyUser.Language,"");
    this.authToken=(String)node.getProperty(ModelGraphProperty.BusyUser.Token,"");
}

我正在抓取表单数据的 Users.class:

private static final Form<BusyUser> userForm = Form.form(BusyUser.class);


//my save method

public static Result save()
{
    //the list of interests that I am grabbing from the database 
    List<Interest> interests= NeoDataProvider.getInterests(play.api.i18n.Lang.defaultLang().language());

    Http.MultipartFormData body = request().body().asMultipartFormData();

    try {
        Form<BusyUser> boundForm = userForm.bindFromRequest();
        if(boundForm.hasErrors()) {
            flash("error", "!");
            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 ( Transaction tx = db.beginTx() )
        {
            Index<Node> 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();

        flash("success",
                String.format("Successfully added user %s", user.user));

        return redirect(routes.Application.login());
    }catch(Exception ex){
        flash("error", "!");
        return badRequest(createuser.render(userForm, interests));
    }

}

您可以简单地读取所有多选数据,只需在表单中代替 name="interestedIn" 写入name="intrestedIn[]"并声明List<Long> intrestedIn= new ArrayList<Long>(); 在您的 BusyUser.java 之后,您可以遍历列表并获取数据,例如

String value=null;
for(int i=0;i<intrestedIn.size();i++){
value=intrestedIn.get(i);
//do whatever you want to do with this value
System.out.println(value);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM