元组在PlayFramework中的作用 [英] The role of Tuple in PlayFramework
问题描述
现在我正在使用play2.2.1,并且我知道Java,但是不知道Scala. 而且,即使您只是学习一点PlayFramework,您都将知道Scala在框架中得到了一定程度的使用.这只是关于我的背景.问题是,尽管传递值的数量不是一个复数,为什么下面的代码中使用了Tuple2.
Now I'm using play2.2.1, and I know Java, but I don't know Scala. And if you learn PlayFramework even just a little bit, you will know Scala is used in some extent in the framework. This is just a background about me. The question is why is Tuple2 used in below code although the number of passing values is one not plural.
这是控制器的代码:
//Action for making a message
public static Result add() {
Form<Message> f = new Form(Message.class);
List<Member> mems = Member.find.select("name").findList();
List<Tuple2<String, String>> opts =
new ArrayList<Tuple2<String, String>>();
for(Member mem: mems) {
opts.add(new Tuple2(mem.name, mem.name));
}
return ok(add.render("fill-in form", f, opts));
}
这是模型的代码:
@Entity
public class Member extends Model {
@OneToMany(cascade = CascadeType.ALL)
public List<Message> messages = new ArrayList<Message>();
@Id
public Long id;
@Required(message = "This item is Required")
public String name;
@Email(message = "Please Fill in a message")
public String mail;
public String tel;
public static Finder<Long, Member> find = new Finder<Long, Member>(Long.class, Member.class);
@Override
public String toString() {
String ids = "{id:";
for (Message m: messages) {
ids += " " + m.id;
}
ids += "}";
return ("[id:" + id + ", message:" + ids + ", name:" + name + ", mail:" + mail + ", tel:" + tel + "]");
}
public static Member findByName(String input) {
return Member.find.where().eq("name", input).findList().get(0);
}
}
这是视图的代码:
@(msg:String, form1: Form[models.Message], opts: List[Tuple2[String, String]])
@main("Sample page") {
<h1>ADD MESSAGE</h1>
<p>@msg</p>
@helper.form(action = routes.Application.create) {
@(helper.select (
field = form1("name"),
options = opts
))
@(helper.textarea (
field = form1("message")
))
<input type="submit">
}
}
同样,为什么尽管其中一个值已从控制器传递到视图,但是仍使用包含两个String值的Tuple2呢? 如果我在此处显示的信息不够,请给我评论.
Again, why is Tuple2, which contains two String values, used despite one value passed from the controller to the view? If the information I showed here is not enough, please give me a comment.
推荐答案
元组的作用是在单个实例中提供多个值.也许有点像一个未命名的类?总结评论并给出一个清晰的示例,<select>
结构中<option>
标记的结构为
The role of a tuple is to provide multiple values in a single instance. Perhaps it's a bit like an unnamed class? To summarise the comments and give a clear example, the structure of the <option>
tag in a <select>
structure is
<option value="internal-value">shown-value</option>
我们经常在外键情况下使用它-外键值在内部值"中,而链接表中的名称或其他属性在显示值中.
We use it often for foreign key situations - the foreign key value is in "internal-value" and the name or other attribute from the linked table is in the shown-value.
如果使用播放助手@select生成它,则应提供一个元组2的Seq
-即每个选项两个字符串.第一个用于内部值,第二个用于显示的值.我使用defining
,但这只是做到这一点的一种方法.例如:
If you use the play helper @select to generate it, you should provide a Seq
of tuple2s - i.e. two strings for each option. The first for the internal value, and the second for the displayed value. I use a defining
but that is just one way to do it. For example:
@defining(for (a <- AppType.all()) yield (a.id.toString,a.name)) { appTypeList =>
@select(
apiForm("forApp"),
appTypeList,
'class -> "form-control",
'_default -> "-- None Selected --",
'_label -> "App Type"
)
}
如果您有一些特殊需要或不使用帮助程序的原因,则可以手动生成它(或者可以编写自己的帮助程序:)).例如:
You can generate it yourself manually if you have some special needs or reason not to use the helper (or you can write your own helper :) ). For example:
<select name="forApiType" id="forApiType">
@ApiType.all().map { a =>
<option value="@a.id" @if(a.id==apiType.id) {selected}>
@a.name
</option>
}
</select>
这篇关于元组在PlayFramework中的作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!