Java 玩吧!框架 - 无法使用现有值填充表单
Java Play! Framework - Unable to populate form with existing value
我正在使用 Java Play framework 2.3.6。我无法使用现有值填充表单。
代码片段如下。
Application.java
public class Application extends Controller{
static Form<Roles> rolesForm=Form.form(Roles.class);
public static Result getRoleById(Long id){
Roles r = RolesDao.getRoleById(id);
//rolesForm.fill(RolesDao.getRoleById(id));
rolesForm.fill(r);
return ok(updateRole.render(rolesForm));
}}
updateRole.scala
@(roleForm : Form[models.Roles])
@import helper._
@main("Update Role Details"){
@form(routes.Application.updateRole(Long Id)){
@inputText(roleForm("role_short_description"))
@inputText(roleForm("role_long_description"))
@inputRadioGroup(roleForm("active_yn"))
<br>
<input type="submit" value="Update">
}
}
Roles.java
public class Roles extends Model{
@Id
@SequenceGenerator(schema=Roles.SCHEMA,name="gen", sequenceName="role_id_seq",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="gen")
public Long role_id;
@Required
public String role_short_description=null;
@Required
public String role_long_description=null;
public String active_yn=null;
RolesDAO.java
public class RolesDao {
public static Finder<Long,Roles> find = new Finder<Long, Roles>(
Long.class, Roles.class
);
public static Roles getRoleById(Long id) {
return find.where().eq("role_id", id).findUnique();
}}
试试这个,
public class Application extends Controller{
public static Result getRoleById(Long id){
Roles role = RolesDao.getRoleById(id);
Form<Roles> rolesForm = Form.form(Roles.class).fill(role);
return ok(updateRole.render(rolesForm));
}
}
始终将表单保存在 Action 方法中,而不是全局保存。
我正在使用 Java Play framework 2.3.6。我无法使用现有值填充表单。
代码片段如下。
Application.java
public class Application extends Controller{
static Form<Roles> rolesForm=Form.form(Roles.class);
public static Result getRoleById(Long id){
Roles r = RolesDao.getRoleById(id);
//rolesForm.fill(RolesDao.getRoleById(id));
rolesForm.fill(r);
return ok(updateRole.render(rolesForm));
}}
updateRole.scala
@(roleForm : Form[models.Roles])
@import helper._
@main("Update Role Details"){
@form(routes.Application.updateRole(Long Id)){
@inputText(roleForm("role_short_description"))
@inputText(roleForm("role_long_description"))
@inputRadioGroup(roleForm("active_yn"))
<br>
<input type="submit" value="Update">
}
}
Roles.java
public class Roles extends Model{
@Id
@SequenceGenerator(schema=Roles.SCHEMA,name="gen", sequenceName="role_id_seq",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="gen")
public Long role_id;
@Required
public String role_short_description=null;
@Required
public String role_long_description=null;
public String active_yn=null;
RolesDAO.java
public class RolesDao {
public static Finder<Long,Roles> find = new Finder<Long, Roles>(
Long.class, Roles.class
);
public static Roles getRoleById(Long id) {
return find.where().eq("role_id", id).findUnique();
}}
试试这个,
public class Application extends Controller{
public static Result getRoleById(Long id){
Roles role = RolesDao.getRoleById(id);
Form<Roles> rolesForm = Form.form(Roles.class).fill(role);
return ok(updateRole.render(rolesForm));
}
}
始终将表单保存在 Action 方法中,而不是全局保存。