Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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