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 Spring Basics Modeling, Storing, and Presenting Data Using @PathVariable to Create a Dynamic Detail Page

vikas Rai
vikas Rai
9,703 Points

Using @PathVariable to Create a Dynamic Detail Page

When I am using Path variable annotation I am getting this error There was an unexpected error (type=Internal Server Error, status=500). Exception evaluating SpringEL expression: "gif.name" (template: "gif-details" - line 13, col 12)

BELOW IS MY CODE

package com.teamtreehouse.giflib.controller;

import com.teamtreehouse.giflib.data.GifRepository; import com.teamtreehouse.giflib.model.Gif; import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;

@Controller //Due to this annotation spring detect this class as controller class public class GifController {

@Autowired
private GifRepository gifRepository;



@RequestMapping("/gif/{nam}")
public String gifDetails(@PathVariable String nam, ModelMap modelMap){
    Gif gif= gifRepository.findByName(nam);
    modelMap.put("gif", gif);
    return "gif-details";
}

}

vikas Rai
vikas Rai
9,703 Points

package com.teamtreehouse.giflib.model;

import java.time.LocalDate;

public class Gif {

private String name;
private LocalDate dateUploaded;
private String userName;
private Boolean favourite;

public Gif(String name, LocalDate dateUploaded, String userName, Boolean favourite) {
    this.name = name;
    this.dateUploaded = dateUploaded;
    this.userName = userName;
    this.favourite = favourite;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public LocalDate getDateUploaded() {
    return dateUploaded;
}

public void setDateUploaded(LocalDate dateUploaded) {
    this.dateUploaded = dateUploaded;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public Boolean isFavourite() {
    return favourite;
}

public void setFavourite(Boolean favourite) {
    this.favourite = favourite;
}

}

1 Answer

I don't see anything wrong with what you shared* You might look at your gif-details.html template.

*by the way, when sharing code, use the following format for Markdown.

Code: Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks.