0

I have server and I should make request on button pressed also I have to call this method and when it is works I should parse json but my doesn't see controller method only main method is available

How to call

 <input type="submit" onclick="@routes.Login.resp()" value="LOGIN" >

because it is not worrking Cannot resolve symbol

GET     /login                           controllers.Login.main()

My controller:

package controllers;

import play.libs.F;
import play.libs.WS;
import play.mvc.Controller;
import play.mvc.Result;


public class Login extends Controller {



    public static Result main() {
        return ok(views.html.login.render());
    }

    public static F.Promise<Result> resp() {
        String feedUrl="http://validate.jsontest.com/?json=%7B%22key%22:%22value%22%7D";
        final F.Promise<Result> resultPromise = WS.url(feedUrl).get().flatMap(
                new F.Function<WS.Response, F.Promise<Result>>() {
                    public F.Promise<Result> apply(WS.Response response) {
                        return WS.url(response.asJson().findPath("empty").asText()).get().map(
                                new F.Function<WS.Response, Result>() {
                                    public Result apply(WS.Response response) {
                                        return ok("size" + response.asJson().findPath("count").asInt());
                                    }
                                }
                        );
                    }
                }
        );
        return resultPromise;
    }
}

view:

<!--
Author: W3layouts
Author URL: http://w3layouts.com
License: Creative Commons Attribution 3.0 Unported
License URL: http://creativecommons.org/licenses/by/3.0/
-->
<!DOCTYPE html>
<html>
    <head>
        <title>LOGIN</title>
        <meta charset="utf-8">
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/stylelogin.css")">

        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
            <!--webfonts-->
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:600italic,400,300,600,700' rel='stylesheet' type='text/css'>
            <!--//webfonts-->
    </head>
    <body>
            <!-----start-main---->
        <div class="main">
            <div class="login-form">
                <h1>Member Login</h1>
                <div class="head">
                    <img src="@routes.Assets.at("images/user.png")" alt=""/>

                </div>
                <form>
                    <input type="text" class="text" value="USERNAME" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'USERNAME';}" >
                    <input type="password" value="Password" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Password';}">
                    <div class="submit">
                        <input type="submit" onclick="@routes.Login.main()" value="LOGIN" >
                    </div>

                </form>
            </div>
                <!--//End-login-form-->
                <!-----start-copyright---->

                <!-----//end-copyright---->
        </div>
            <!-----//end-main---->

    </body>
</html>

I am not sure if I also parse json properly,how to make proper GET,POST requests and parse it

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sasha
  • 644
  • 8
  • 22

1 Answers1

1

As far as I know with the onclick attribute you always call a function in your JavaScript. If you want to specify an URL you need to put it into your form tag with an action attribute like <form action="@routes.Login.main()">.

The default for the HTML form tag is to send a GET. If you want to send a POST you have to specify it via an additional method="post" like <form action="@routes.Login.main()" method="post">. But then you have to change your routing too: POST /login controllers.Login.main(). If you want to post login data I'd strongly recommend to use POST because with GET your data including the password turns up in the query string of your URL.

Additionally your @routes.Login.main() method just returns the login view return ok(views.html.login.render());. Instead it should evaluate the form data you are sending.

Community
  • 1
  • 1
Kris
  • 4,595
  • 7
  • 32
  • 50
  • i am absolutely newbie,can you show how to make that? – Sasha Jul 17 '15 at 17:39
  • In https://www.playframework.com/documentation/2.2.6/JavaGuide4 they explain how to do a simple authentication with Play. It's for version 2.2.6 but should still work for the current Play. – Kris Jul 17 '15 at 18:28
  • I want to login with other webservice – Sasha Jul 18 '15 at 05:43
  • like http://stackoverflow.com/questions/31489384/play-framework-2-2-form-http-get – Sasha Jul 18 '15 at 08:44