Playframework 播放框架1.2.4-积垢-内部对象列表

Playframework 播放框架1.2.4-积垢-内部对象列表,playframework,crud,Playframework,Crud,我有一个对象(管理器),其中包含另一个对象的列表(操作项)。对于这两个对象,我正在使用CRUD模块,从中我覆盖了Managers/show.html,但我希望在同一页面上显示属于管理器的所有操作项。ActionItems只能通过选择manager对象来使用。我该怎么办 public class Manager extends Model{ ... public List<ActionItem> actions; ... } public class ActionI

我有一个对象
(管理器)
,其中包含另一个对象的列表
(操作项)
。对于这两个对象,我正在使用CRUD模块,从中我覆盖了
Managers/show.html
,但我希望在同一页面上显示属于
管理器的所有操作项。
ActionItems
只能通过选择manager对象来使用。我该怎么办

public class Manager extends Model{
   ...
   public List<ActionItem> actions;
   ...
}

public class ActionItem extends Model{
 ...
}

public class Managers extends CRUD{

}
公共类管理器扩展模型{
...
公开名单行动;
...
}
公共类ActionItem扩展模型{
...
}
公共类管理器扩展了CRUD{
}

没有简单的方法可以做到这一点。更多的定制-更多的代码

Managers/show.html
#{扩展'CRUD/layout.html'/}
#{set title:messages.get('crud.show.title',type.modelName)/}
&{'crud.show.title',type.modelName}
#{表单操作:@save(object._key()),enctype:'multipart/form data'}
*{
*必须隐藏“操作”字段的crud.form的默认输出。
*只需指定管理器的所有字段,但“操作”除外。
}*
#{crud.form字段:['someField1','someField2','someField4']/}
*{
*现在,您可以绘制包含所有manager操作的列表。
}*
    #{列表项:'object.actions',如:'action'}
  • #{/list}
*{ *要添加新操作,您需要一个类似下面的链接。 *在这里,我们尝试在操作的空白处重新设置经理的id *然后我们可以尝试修改blank()并截取 *我们的目的。 }*

#{/form} #{form@delete(object.\u key())}

#{/form}
Actions.java
公共类操作扩展了CRUD{
// ...
/** 
*已修改的blank()方法,该方法将把managerId重设为
*Actions/blank.html,将其重设为modified create()
*方法。
*/
公共静态void blank(Long managerId)引发异常{
ObjectType=ObjectType.get(getControllerClass());
notFoundIfNull(类型);
构造函数=type.entityClass.getDeclaredConstructor();
constructor.setAccessible(true);
模型对象=(模型)构造函数.newInstance();
试一试{
呈现(类型、对象、managerId);
}捕获(TemplateNotFounde异常){
呈现(“CRUD/blank.html”、类型、对象、managerId);
}
}
/** 
*修改了create()方法。
*/
公共静态void create(Long managerId)引发异常{
ObjectType=ObjectType.get(getControllerClass());
notFoundIfNull(类型);
构造函数=type.entityClass.getDeclaredConstructor();
constructor.setAccessible(true);
模型对象=(模型)构造函数.newInstance();
Binder.bindBean(params.getRootParamNode(),“object”,object);
有效(对象);
if(validation.hasErrors()){
renderArgs.put(“error”、play.i18n.Messages.get(“crud.hasErrors”);
试一试{
呈现(request.controller.replace(“.”,“/”+“/blank.html”),类型,对象);
}捕获(TemplateNotFounde异常){
呈现(“CRUD/blank.html”,类型,对象);
}
}
对象。_save();
/*自定义部分{{{*/
Manager=Manager.findById(managerId);
manager.actions.add(对象);
success(play.i18n.Messages.get(“crud.created”,type.modelName));
if(params.get(“\u save”)!=null){
Managers.show(managerId);
}
if(params.get(“\u saveandaddother”)!=null){
Actions.blank(managerId);
}
Actions.show(object.\u key(),managerId);
/* }}} */
}
/** 
*modified show()方法。
*/
公共静态void show(字符串id,managerId)引发异常{
// ...
}
//其他修改的方法…如果需要。
// ...
}
Actions/blank.html
#{扩展'CRUD/layout.html'/}
#{set title:messages.get('crud.blank.title',type.modelName)/}
&{'crud.blank.title',type.modelName}
*{
*这里我们将managerId重设为修改的操作。create()。
}*
#{表单操作:@create(managerId),enctype:'multipart/form data'}
#{crud.form/}

#{/form}
我在你家做过类似的事情

#{extends 'CRUD/layout.html' /}
#{set title:messages.get('crud.show.title', type.modelName) /}

<div id="crudShow" class="${type.name}">

  <h2 id="crudShowTitle">&{'crud.show.title', type.modelName}</h2>

  <div class="objectForm">
  #{form action:@save(object._key()), enctype:'multipart/form-data'}

    *{
    * You must hide default output of the crud.form for the "actions" field.
    * Just specify all fields of the Manager except the "actions".
   }*

    #{crud.form fields:['someField1', 'someField2', 'someField4'] /}

    *{
    * Now you can draw some list that contains all manager`s actions.
   }*

    <ul>
      #{list items:'object.actions', as:'action'}
       <li>
        <a href="@{Actions.show(action.id)}">${action}</a>
       </li>
      #{/list}
    </ul>

    *{
    * For adding a new action you need a something like link below.
    * There we try to retieve Manager`s id in the Action`s blank
    * method. Than we could try to modify blank() and intercept id for
    * our purposes.
   }*
    <a href="@{Actions.blank(object.id)}">Add new action</a>

    <p class="crudButtons">
      <input type="submit" name="_save" value="&{'crud.save', type.modelName}" />
      <input type="submit" name="_saveAndContinue" value="&{'crud.saveAndContinue', type.modelName}" />
    </p>
  #{/form}
  </div>

  #{form @delete(object._key())}
    <p class="crudDelete">
      <input type="submit" value="&{'crud.delete', type.modelName}" />
    </p>
  #{/form}

</div>
public class Actions extends CRUD {

    // ...

    /** 
     * Modifed blank() method, that will retieve managerId to
     * Actions/blank.html, that will retieve it into modifed create()
     * method.
     */
    public static void blank(Long managerId) throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Model object = (Model) constructor.newInstance();
        try {
            render(type, object, managerId);
        } catch (TemplateNotFoundException e) {
            render("CRUD/blank.html", type, object, managerId);
        }
    }

    /** 
     * Modifed create() method.
     */
    public static void create(Long managerId) throws Exception {
        ObjectType type = ObjectType.get(getControllerClass());
        notFoundIfNull(type);
        Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
        constructor.setAccessible(true);
        Model object = (Model) constructor.newInstance();
        Binder.bindBean(params.getRootParamNode(), "object", object);
        validation.valid(object);
        if (validation.hasErrors()) {
            renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors"));
            try {
                render(request.controller.replace(".", "/") + "/blank.html", type, object);
            } catch (TemplateNotFoundException e) {
                render("CRUD/blank.html", type, object);
            }
        }
        object._save();

        /* Custom part {{{ */
        Manager manager = Manager.findById(managerId);
        manager.actions.add(object);

        flash.success(play.i18n.Messages.get("crud.created", type.modelName));
        if (params.get("_save") != null) {
            Managers.show(managerId);
        }
        if (params.get("_saveAndAddAnother") != null) {
            Actions.blank(managerId);
        }
        Actions.show(object._key(), managerId);
        /* }}} */
    }

    /** 
     * Modifed show() method.
     */
    public static void show(String id, managerId) throws Exception {
        // ...
    }

    // Other modifed methods... if need.

    // ...

}
#{extends 'CRUD/layout.html' /}
#{set title:messages.get('crud.blank.title', type.modelName) /}

<div id="crudBlank" class="${type.name}">

    <h2 id="crudBlankTitle">&{'crud.blank.title', type.modelName}</h2>

    <div class="objectForm">

    *{
    * Here we retieve managerId to modifed Actions.create().
   }*

    #{form action:@create(managerId), enctype:'multipart/form-data'}
        #{crud.form /}
        <p class="crudButtons">
            <input type="submit" name="_save" value="&{'crud.save', type.modelName}" />
            <input type="submit" name="_saveAndContinue" value="&{'crud.saveAndContinue', type.modelName}" />
            <input type="submit" name="_saveAndAddAnother" value="&{'crud.saveAndAddAnother', type.modelName}" />
        </p>
    #{/form}
    </div>

</div>