使用PlayFrameWork向Scala模板添加下拉列表



任何人都能帮我弄清楚为什么我的选择组件在我尝试保存时没有绑定所选项目吗。我的Scala模板代码如下:

    @(subheadform: play.data.Form[SubHead])(mode: String)(budgetAccountOptions: java.util.HashMap[String, String])}
@main("Service info") {
<div class="page-header">
<h2>SubHead Information</h2>
</div>
<fieldset>
<legend> Budget(@mode)</legend>
@b3.form(action = routes.SubHeadController.save(mode)) {
  <input type ="hidden" name="mode" value="@mode"/>
  @b3.select( subheadform("code"), options= options(budgetAccountOptions), '_label -> "Select Budget Control",'_default -> "Select an option" )
  @if(mode == "Adding") {
  } else {
    @b3.text(subheadform("id"), '_label -> "Id", 'readOnly -> "readOnly")
  }
  @b3.text(subheadform("code"), '_label -> "Code", 'placeholder -> "Code")
  @b3.text(subheadform("description"), '_label -> "Description", 'maxlength -> 50, 'placeholder -> "Description")
  <div class="form-group">
    <div class="col-md-offset-3">
      <input type="submit" class="btn btn-success" value="save"/>
      <button type="button" class="btn btn-warning" onclick= "window.location= '@routes.SubHeadController.listofSubHeads()';" value="cancel">
        Cancel</button>
    </div>
  </div>
}
</fieldset>
}

以下是我的路线:

#------------------------------------ BUDGET-SUBHEAD ROUTES----------------
GET     /budgetsubhead                         controllers.SubHeadController.listofSubHeads()
GET     /budgetsubhead/new               controllers.SubHeadController.addSubHead()
GET     /budgetsubhead/edit/:code        controllers.SubHeadController.editSubHead(code: String)
POST    /budgetsubhead/save/:mode        controllers.SubHeadController.save(mode: String)
DELETE  /budgetsubhead/del/:code          controllers.SubHeadController.delete(code: String)

下面是我的副标题控制器:

public class SubHeadController extends Controller {
private static Logger.ALogger logger = Logger.of(SubHeadController.class);

private HashMap getMap() {
    List<BudgetAccount> budgetAccounts = BudgetAccount.findAll();
    HashMap budgetAccountOptions = new HashMap<String, String>();
    for ( BudgetAccount budgetAccount : budgetAccounts ) {
        budgetAccountOptions.put(budgetAccount.id.toString(), budgetAccount.description);
    }
    return budgetAccountOptions;
}

public Result listofSubHeads() {
    List<SubHead> allSubheads = SubHead.findAll();
    return ok(listofsubheads.render(allSubheads));
    //return ok(Json.toJson(allServices));
}
public Result addSubHead() {
    Form<SubHead> newSubHeadForm = Form.form(SubHead.class);
    HashMap budgetAccountOptions = getMap();
    return ok(subheadpage.render(newSubHeadForm, ModeConst.ADD,budgetAccountOptions));
}


public Result save(String mode) {

    Form<SubHead> ourForm = Form.form(SubHead.class).bindFromRequest();
    HashMap budgetAccountOptions = getMap();
    //Logger.debug("Errors:" + ourForm.errors().toString());
    if (ourForm.hasErrors()) {
        logger.debug("Error");
        return badRequest(subheadpage.render(ourForm, mode,budgetAccountOptions));
    }
    SubHead subHead = ourForm.get();

    if (subHead != null) {
        if (ModeConst.ADD.equals(mode)) {
            if (SubHead.exists(subHead.code)) {
                flash("errorsFound", "SubHead year already exists");
                return badRequest(subheadpage.render(ourForm, mode,budgetAccountOptions));
            }
            subHead.save();
            return redirect(routes.SubHeadController.addSubHead());
        } else if (mode.startsWith(ModeConst.EDIT)) {
            subHead.update();
           return redirect(routes.SubHeadController.editSubHead(subHead.code));
        }
    }
    // flash("errorsFound","Please fix the errors on the page");
    return badRequest(subheadpage.render(ourForm, mode,budgetAccountOptions));
}
}

这是我的型号

@Entity
@Table(name = "sub_head")
public class SubHead extends Model {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
public Long id;
public String code;
public String description;

@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public BudgetAccount budgetAccount;

private static Model.Finder<String, SubHead> find = new Model.Finder<String, SubHead>(SubHead.class);
public static List<SubHead> findAll() {
    return find.orderBy("code").findList();
}
public static SubHead retrieve(String code) {
    return find.where()
            .eq("code", code)
            .findUnique();
}
public static boolean exists(String code) {
    return (find.where().eq("code", code).findRowCount() == 1) ? true : false;
}
}

当我尝试保存时,我会得到一个NullPointer异常。有人能找出我做得不对的地方吗?感谢

欢迎光临。其中一个问题似乎是您有两个id为"code"的字段,即:

@b3.select( subheadform("code"), options= options(budgetAccountOptions), ...
...
@b3.text(subheadform("code"), '_label -> "Code", 'placeholder -> "Code")

因此,在我看来,可能会有不可预测的结果,这取决于浏览器。

最新更新