0

我试图在我的控制器中获得一个选定的值,但无法让它工作。

在我看来,我有一个表格:

@(infoObjectForm: Form[Infoobject],nav: String = "")

@form(routes.Admin.admin_createMapInstance()) {
        <fieldset>
        @select(
                infoObjectForm("infoobjectId"), 
                options(Infoobject.all_values), 
                'id -> "infoobjects_field",
                '_label -> "Infoobject", '_default -> "--Select Infoobject--",
                '_showConstraints -> false, 'value -> infoObjectForm
            )
        </fieldset>

        <div class="actions">
            <input type="submit" value="Create this Map Instance" class="btn primary"> or 
            <a href="@routes.Admin.admin_MapInstance()" class="btn">Cancel</a> 
        </div>
    }

这个下拉列表显示了我所有的信息对象。当我选择一个并点击提交按钮时,我想在控制器中使用选定的信息对象(及其属性)。

Form<Infoobject> mapForm = form(Infoobject.class).bindFromRequest();
        if(mapForm.hasErrors()) {
            flash("error", "MapForm contains an error");
            return badRequest(createMapForm.render(mapForm, ""));
        }
Infoobject infoobject = mapForm.get();
String desig = infoobject.getDesignation();
String desc = infoobject.getDescription();
...

mapForm 有错误。你们知道为什么吗?

信息对象模型:

@Entity
public class Infoobject extends Model {
@Id
    @SequenceGenerator(name="infoobject_seq", sequenceName="infoobject_id_seq", allocationSize=1000)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="infoobject_seq")
    public Long infoobjectId;

    @Required
    public String designation;
    @Required
    public String description;
        ...getters/setters

我尝试在我的视图中使用两个 @inputText 字段,所以我可以创建一个 info 对象,它可以工作。但现在我不想创建现有信息对象的地图实例。

感谢你的帮助!

编辑:在控制台中打印错误,说“error.required”。任何人?该表单似乎不包含任何信息对象。找到了这个类似的问题/答案..那么为什么这不起作用?

4

2 回答 2

0

该解决方案有效,但我认为这不是最好的方法。

<form>
        @select(
                infoObjectForm("infoobjectId.id"), 
                options(Infoobject.all_values), 
                'id -> "infoobjects_field",
                '_label -> "Infoobject", '_default -> "--Select Infoobject--",
                '_showConstraints -> false
            )
        <div class="actions">
            <input type="button" value="Create this Map Instance" class="btn primary" onclick="JavaScript:createMap()"> or 
            <a href="@routes.Admin.admin_MapInstance()" class="btn">Cancel</a> 
        </div>
    </form>
    <script>
        function createMap() {
            var ioId = document.getElementById('infoobjects_field').value;
            if(ioId) {
                var nextUrl = "/CreateMapInstance?ioId="+ioId;
                window.location = nextUrl;
            }
        }
    </script>

我将执行 GET 并仅在 url 中发送 ID,而不是在 POST 中发送整个 infoobject。

于 2013-11-11T10:25:59.947 回答
0

在@select 标记中放入 infoObjectForm("infoobjectId")

在模型类 Infoobject 中添加:

static {
    play.data.format.Formatters.register(Infoobject.class, new ClassFormatter());
}

public static class ClassFormatter extends SimpleFormatter<Infoobject> {

    @Override
    public Infoobject parse(String text, Locale locale) {
        if (text == null || text.trim().length() == 0)
            return null;
        return Infoobject.find.byId(Long.parseLong(text.trim()));
    }

    @Override
    public String print(Infoobject value, Locale locale) {
        return (value == null || value.id == null ? "" : value.id.toString());
    }
}
于 2013-11-22T14:03:33.583 回答