所以我正在使用 Play 进行编辑配置文件功能!框架(2.2.0);
我有这个代码
public static Result doEditProfile(){
final User localUser = getLocalUser(session());
Form<User> formData = editProfileForm.bindFromRequest();
if (formData.hasErrors()) {
return badRequest(views.html.editprofile.render(localUser, editProfileForm));
} else {
localUser.firstName = formData.field("firstName").value();
localUser.lastName = formData.field("lastName").value();
localUser.locale = formData.field("locale").value();
localUser.gender = formData.field("gender").value();
localUser.country = formData.field("country").value();
localUser.save();
}
return redirect("/profile/edit");
}
有用。但我想知道有没有更好的方法来做到这一点?
我试过这些东西:
1)
public static Result doEditProfile(){
final User localUser = getLocalUser(session());
Form<User> formData = editProfileForm.bindFromRequest();
if (formData.hasErrors()) {
return badRequest(views.html.editprofile.render(localUser, editProfileForm));
} else {
User localUser = formData.get();
localUser.save();
}
return redirect("/profile/edit");
}
但这表示变量 localUser 已经定义。
2)另外,我试过这个
public static Result doEditProfile(){
final User localUser = getLocalUser(session());
Form<User> formData = editProfileForm.bindFromRequest();
if (formData.hasErrors()) {
return badRequest(views.html.editprofile.render(localUser, editProfileForm));
} else {
User updatedUser = formData.get();
updatedUser.save();
}
return redirect("/profile/edit");
}
但这段代码正在数据库中创建一个新用户。
我是 Play 新手,所以我正在等待任何建议。谢谢和对不起我的英语