Java Intro to Java Web Development with Spark Diving into Web Development Accepting Requests

Tornike Shelia
Tornike Shelia
2,781 Points

The output result doesn't show String , instead it shows : spark.ModelAndView@508e5484

public class Main {
    public static void main(String[] args) {

        get("/", (req, res) -> new ModelAndView(null, "index.hbs"), new HandlebarsTemplateEngine());

        post("/sign-in",(req,res)->{
            Map<String, String> model = new HashMap<>();
            model.put("username" , req.queryParams("username"));
            return new ModelAndView(model,"sign-in.hbs");
        });new HandlebarsTemplateEngine();
    }
    }

sign-in.hbs :

    <body>
      <p>Welcome {{ username }}! </p>
    </body>

When I type anything in the input field it shows : spark.ModelAndView@508e5484 , the numbers are random everytime

Tracy Bowers
Tracy Bowers
4,547 Points

Your syntax is incorrect. I can see why you are bashing your head. Stuff like this is so easy to miss. You have: });new HandlebarsTemplateEngine();

you in fact need: , new HandlebarsTemplateEngine() );

Notice you need to create a lambda and create a new handekbarsTemplateEngine object which are both passed as parameters to the post method.

Your syntax is only passing the lambda because you have a ";" followed by calling for a new HandelbarsTemplateEngine object.

Try not to go cross-eyed. But yes this is really awkward in my opinion. The powers that be in the Java world are trying to make this better... hopefully