Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

When I type in my user name and click the "Sign in" button, all it says is "Welcome !". No user name.

When I type in my user name and click the "Sign in" button, all it says is "Welcome !". The username is not displaying.

Here is my Main.java file:

package com.teamtreehouse.courses;

import java.util.HashMap;
import java.util.Map;

import spark.ModelAndView;
import spark.template.handlebars.HandlebarsTemplateEngine; 
import static spark.Spark.*;

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

        get("/", (req, res) -> {
            return 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(null, "sign-in.hbs");
        }, new HandlebarsTemplateEngine());
    }
}

Here is my index.hbs file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello Students</title>
</head>
<body>
    <h1>Welcome Students!</h1>
    <!-- To do: Add sign in -->
    <form action="/sign-in" method="post">
        <input type="text" placeholder="What's your user name?" name="username" >
        <button>Sign in</button>
    </form>
</body>
</html>

sign-in.hbs file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Signed In</title>
</head>
<body>
    <p>Welcome {{username}} !</p>
</body>
</html>

1 Answer

Never mind I figured it out. The problem was in the Main.java file. Instead of doing this:

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

I had this:

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