【问题标题】:Get selected value from selector (@Select) in controller. Java Play Framework从控制器中的选择器 (@Select) 中获取选定的值。 Java Play 框架
【发布时间】:2013-11-08 11:52:07
【问题描述】:

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

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

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

感谢您的帮助!

编辑: 在控制台中打印错误,说“error.required”。任何人?该表单似乎不包含任何信息对象。找到this simular question/answer..那么为什么这不起作用?

【问题讨论】:

  • 您的模型中可能还有其他字段或字段名称不匹配...也许显示您的 Infoobject 模型代码
  • 你能展示一下 Infoobject 模型吗?

标签: java model-view-controller playframework playframework-2.0


【解决方案1】:

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

<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。

【讨论】:

    【解决方案2】:

    在@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());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2012-05-02
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多