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 Cookies

Please Help Getting "500 Internal Error" when I use if else condition in index.hbs

<html lang="en">
<!doctype html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Students</title>
</head>
<body>
    {{#if username }}
        <h1>Welcome {{ username }}!</h1>
    {{ else }}
        <h1>Welcome Students!</h1>
        <form action="/sign-in" method="post">
            <input type="type" placeholder="What's your user name?" name="username">
            <button>Sign in</button>
        </form>
    {{/if}}
</body>
</html>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Signed in</title>
</head>
<body>
<p>Welcome {{username}}!</p>
</body>
</html>
package com.teamtreehouse;

import spark.ResponseTransformer;

import static spark.Spark.*;
import spark.ModelAndView;
import spark.template.mustache.MustacheTemplateEngine;

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


/**
 * Created by ashokgudivada on 4/30/17.
 */
public class Main {
    public static void main(String[] args){
        port(8001);
        get("/hello", (req, res) -> "Hello World");

        get("/", (req, res) -> {
            Map<String, String> model = new HashMap<>();
            model.put("username", req.cookie("username"));
            return new ModelAndView(model, "index.hbs");
        }, new MustacheTemplateEngine());

        post("/sign-in", (request, response) -> {
            Map<String, String> model = new HashMap<>();
            String username = request.queryParams("username");
            response.cookie("username",username);
            model.put("username", username);
            return new ModelAndView(model, "sign-in.hbs");
        }, new MustacheTemplateEngine());
    }


}

1 Answer

Fahad Mutair
Fahad Mutair
10,359 Points

hi ASHOK Gudivada , in the examples he was using HandlebarsTemplateEngine() not MustacheTemplateEngine() so you have to change it to HandlebarsTemplateEngine() and don't forget to update your dependencies to include compile 'com.sparkjava:spark-template-handlebars:2.5.5'