有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从控制器中的选择器(@Select)获取所选值。Java播放框架

我正在尝试在控制器中获取选定值,但无法使其工作

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

@(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>
    }

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

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();
...

映射表单有错误。你们知道为什么吗

信息对象模型:

@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字段,这样我就可以创建一个信息对象,这很有效。但现在我不想创建现有信息对象的映射实例

谢谢你的帮助

编辑:在控制台中打印错误,显示“error.required”。任何人表单似乎不包含任何Infoobject。找到this个相同的问题/答案。。那么为什么这不起作用呢


共 (2) 个答案

  1. # 1 楼答案

    在@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());
        }
    }
    
  2. # 2 楼答案

    这个解决方案可行,但我认为这不是最好的方法

    <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,而不是在帖子中发送整个infoobject