提交表单时,Play Framework POST路径不会运行

时间:2017-11-30 20:51:23

标签: java forms playframework routes

我以前从未遇到过这种情况,所以我正在寻求帮助。

我有一个简单的字段表单,它有一个提交按钮,不会启动它的POST路由。这段代码几天前正在运行,但现在它只是不断运行GET路由。没有错误,因为它永远不会进入POST路由及它正在调用的函数。

以下是路线:

GET    /createptp        controllers.Application.ptpCreate()
POST   /createptp        controllers.Application.ptpSave()

以下是表格:

@(ptpForm: Form[Application.PTPForm], lookups: java.util.List[Lookup], users: java.util.List[User])

@main(null) {

    <section id="ptpCreated">
        <form class="formoid-solid-dark"
            style="background-color: #FFFFFF; font-size: 14px; font-family: 'Trebuchet MS', 'Roboto', Arial, Helvetica, sans-serif; color: #34495E; max-width: 480px; min-width: 150px"
            method="get" action="">
            <div class="title">
                <h3>Success</h3>
            </div>
            <div class="element-number" title="This is the eight-digit Provider ID number that you used for this setting in the portal maintained by Xerox.  You may have had different Provider IDs for different services, settings, and/or waivers.  Cannot be blank - numbers only">
                <label class="title"><span class="required">* Legacy Provider Id...</span></label>
                <div class="item-cont"><input type="text" name="legacy_Provider_Id" class='allow_numeric' required="required" min="1" max="99999999" maxlength="8" placeholder=""/><span class="icon-place"></span></div>
            </div>
            <div class="submit">
                <input type="submit" value="Continue" />
            </div>
            <!-- This is needed for bottom shadow to appear... -->
            <div></div>
        </form>
    </section>

}

以下是控制器中的功能:

    public Result ptpCreate() {
            // Set some needed values...
            List<Lookup> lookups = Lookup.find.all();
            List<User> users = User.find.all();
            // Sort the lists...
            AppGlobals sortThis = new AppGlobals();
            Collections.sort(users, sortThis.new sortUsers());
            Collections.sort(lookups, sortThis.new sortLookups());
            return ok(adult1.render(form(PTPForm.class), lookups, users));
    }

public Result ptpSave() {
        Form<PTPForm> ptpEntry = form(PTPForm.class).bindFromRequest();

        if (ptpEntry.hasErrors()) {
            List<Lookup> lookups = Lookup.find.all();
            List<User> users = User.find.all();
            Logger.debug("Save ptp - errors");
            // Sort the lists...
            AppGlobals sortThis = new AppGlobals();
            Collections.sort(users, sortThis.new sortUsers());
            Collections.sort(lookups, sortThis.new sortLookups());
            return badRequest(adult1.render(ptpEntry, lookups, users));
        }

        String userkey = AccessMiddleware.getSessionUserKey();
        User user = null;
        user = User.findByUserKey(userkey);

        // Save the PTP record....
        PTP ptp = new PTP();
        PTPForm ptpForm = ptpEntry.get();

        ptp.setLegacy_Provider_Id(ptpForm.legacy_Provider_Id);

        ptp.save();

        return ok(ptpcreated.render(ptp.getPtpkey()));
    }

单击“继续”按钮后,它将返回ptpCreate()功能,甚至不会使用POST路径。

我执行了cleancompile没有运气。

我尝试将<form action>更改为action="@routes.Application.ptpSave()",但也没有运气。

我完全迷失了这个,因为没有错误可以追踪。

1 个答案:

答案 0 :(得分:1)

如@jrook所述,您需要确保PhoneBook phoneBook标记使用POST方法。如您的问题所示,您的<form><form>

如果您查看路线,method="get"会转到GET /createptpcontrollers.Application.ptpCreate()会转到POST /createptp。它们都具有相同的URL,因此用于发送请求的方法非常重要。

当您使用controllers.Application.ptpSave()时,您获得了正确的网址,但仍然发送了GET请求,因此它将转到action="@controllers.routes.Application.ptpSave()"而不是controllers.Application.ptpCreate()

修复方法是使用controllers.Application.ptpSave()路由到method="POST"

另一种方法是使用表单模板助手来呈现给定反向路径的正确方法和操作。例如。 controllers.Application.ptpSave()。这将呈现@helper.form(action = controllers.routes.Application.ptpSave()) { ... }。请参阅:https://www.playframework.com/documentation/2.6.x/JavaFormHelpers#Creating-a-%3Cform%3E-tag

将来调试这样的东西会使用两种技术:

  1. 使用 curl 手动向<form method="GET" action="/createptp">...</form>发送POST请求以检查其是否有效。

  2. 使用浏览器的开发者工具测试您在提交表单时发送的请求类型。

  3. 我最近创建了一个Play Framework问题来改进此领域的文档:https://github.com/playframework/playframework/issues/8004。如果您对此有任何想法,请随时发表评论。

相关问题