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

时间:2015-11-15 21:43:17

标签: scala playframework

任何人都可以帮我解决为什么我的选择组件没有绑定我尝试保存时选择的项目。我的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)

以下是我的Subhead Controller:

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异常。 任何人都可以弄明白我做得不对吗? 感谢

1 个答案:

答案 0 :(得分:1)

欢迎。其中一个问题似乎是你有两个id为&#34; code&#34;的字段,即:

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

所以在我看来会有不可预知的结果,可能取决于浏览器。