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 Create an Index Page That Presents a List of Data

Question about @RequestMapping

in Class GifController we did this

@RequestMapping("/gif/{name}")
public String gifDetails(@PathVariable String name , ModelMap modelMap){

            Gif  gif = gifRepository.findByName(name);

        modelMap.put("gif",gif);
        return  "gif-details";
    } 

but I want to set I default value if the user enter the URL without the name localhost:8080/gif any idea how to do this

thanks in advance

1 Answer

There's several ways to do this. You could simply create a new @RequestMapping("/gif") method for the more general case where no name value is given. You could make the method handle both cases explicitly like this @RequestMapping(value = {"/gif", "/gif/{name}"}), and inside the method, check if name == null, then do something based on that.

Take a look at the explanation given at this link https://www.baeldung.com/spring-optional-path-variables