3 回答
Assuming you are putting the select into a HTML form, the way it is done in our shop is we add a Map to the Java enum and then use the select form helper provided by the framework:
enum:
public enum ContactType {
CLIENT(1),
CONTRACTOR(2),
SUPPLIER(3);
public final int id;
ContactType(int id) {
this.id = id;
}
public static Map<String, String> options(){
LinkedHashMap<String, String> vals = new LinkedHashMap<String, String>();
for (ContactTypecType cType: ContactType.values()) {
vals.put(cType.id(), cType.name());
}
return vals;
}
}
view:
@(contactForm: Form[models.Contact])
import helper._
<html><head></head><body>
@helper.form(routes.Contact.formHandlerMethod()){
@*
* Here contactForm("contactType") assumes that you want to map the
* ContactType selected to a field in a Contact class (which we've
* wrapped in play.data.Form<T> so that the fields in Contact become
* inputs in our form
*@
@select(contactForm("contactType"), options(ContactType.options())
}
</body></html>
Using this design pattern you can leverage the helper functions in the play.data.Form class to validate and bind your form data in your Contact Controller and you benefit from having less logic in your view, and putting the logic in a place where it can easily be re-used throughout your app.
If, on the other hand, you are using the select on it's own and driving actions in some other way, then Aerus' answer is probably the most straightforward way.
在您的模板中这样的东西应该可以工作:
<select name="contactType">
@for(cType <- ContactType.values()){
<option value="@cType.id">@cType.name()</option>
}
</select>
注意:使用toString()
而不是name()
. 如果您toString()
在枚举中覆盖,则可以返回 Contractor 而不是 CONTRACTOR。
注意 2:如果您的枚举不在models
包中,则需要在其前面加上正确的包名称,即:@for(cType <- com.my_company.enums.ContactType)
编辑:表单不应包含枚举的所有类路径,只包含枚举名称。所以在 ContactType 类上, put 方法必须替换为:
put(value.name(),name);
我无法将枚举绑定到类。在控制器中,我有类似的东西:
public Result saveUser() {
Form<User> userForm = form(User.class).bindFromRequest();
if(userForm.hasErrors()) {
return badRequest(createUser.render(userForm));
}
User user=userForm.get();
user.save();
Logger.info(user.contacts.toString());
flash("success", "User " + userForm.get().identity + " has been created");
return GO_ADMIN;
}
这一直在返回我“error.invalid”。POST 响应如下所示:
identity=asas&
status=1&
contacts[0].name=fasddf&
contacts[0].prename=afadfs&
contacts[0].email=ffdf@aa.com&
contacts[0].contactType=models.constats.ContactType.MANAGER
为了获得更多信息,我在这里提供了 User 和 Contact 类
用户
public class User extends Model{
@Constraints.Required(message = "Field is required")
public String identity;
public Integer status;
@Valid
public List<Contact> contacts;
}
接触
public class Contact extends Generic{
@Constraints.Required(message = "Field is required")
public String name;
@Constraints.Required(message = "Field is required")
public String prename;
@Constraints.Required(message = "Field is required")
@Constraints.Email
public String email;
public ContactType contactType;
}
联系人类型
public enum ContactType {
ADMIN,SUPPORT,MANAGER,DEVELOPER,DESIGNER,NO_INFORMATION;
}
public static final Map<String,Object> types=new HashMap<String,Object>()
{{
for (ContactType value : ContactType.values()) {
String name=value.name().toLowerCase();
name=Character.toUpperCase(name.charAt(0)) + name.substring(1).replaceAll("_", " ");
put(ContactType.class.getName() + "."+ value.name(),name);
}
}};