从控制器中的选择器(@Select)获取所选值。Java播放框架
我正在尝试在控制器中获取选定值,但无法使其工作 在我看来,我有一个表格:从控制器中的选择器(@Select)获取所选值。Java播放框架,java,model-view-controller,playframework,playframework-2.0,Java,Model View Controller,Playframework,Playframework 2.0,我正在尝试在控制器中获取选定值,但无法使其工作 在我看来,我有一个表格: @(infoObjectForm: Form[Infoobject],nav: String = "") @form(routes.Admin.admin_createMapInstance()) { <fieldset> @select( infoObjectForm("infoobjectId"), opti
@(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>
}
我试着在我的视图中使用两个@inputText字段,这样我就可以创建一个信息对象,这很有效。但现在我不想创建现有信息对象的映射实例
谢谢你的帮助
编辑:在控制台中打印错误,显示“error.required”。任何人表单似乎不包含任何Infoobject。找到了类似的问题/答案。那么为什么这不起作用呢?这个解决方案起作用,但我认为这不是最好的方法
<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>
@挑选(
infoObjectForm(“infoobjectId.id”),
选项(Infoobject.all_值),
'id->“信息对象\u字段”,
“_label->”Infoobject“,“_default->”-选择Infoobject--”,
“\u showConstraints->false
)
或
函数createMap(){
var ioId=document.getElementById('infoobjects_field')。值;
如果(ioId){
var nextUrl=“/CreateMapInstance?ioId=“+ioId;
window.location=nextUrl;
}
}
我将执行GET并只发送url中的ID,而不是在帖子中发送整个infoobject。在@select标记put 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());
}
}
静态{
play.data.format.Formatters.register(Infoobject.class,newclassformatter());
}
公共静态类类格式化程序扩展了SimpleFormatter{
@凌驾
公共信息对象解析(字符串文本、区域设置){
如果(text==null | | text.trim().length()==0)
返回null;
返回Infoobject.find.byId(Long.parseLong(text.trim());
}
@凌驾
公共字符串打印(信息对象值、区域设置){
返回(value==null | | value.id==null?“:value.id.toString());
}
}
您的模型中可能有其他字段,或者字段名称不匹配。。。可能会显示您的Infoobject
model代码您可以显示Infoobject模型吗?
<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>
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());
}
}