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 Using the MVC Architecture Wrappin' It Up

Ulugbek Aripov
Ulugbek Aripov
4,461 Points

I finished with favorites. Can you please help with 2 lines on search?

What is wrong here? Should I use @RequestMapping ?

@RequestMapping("/search?q={keyword}")
public String searchDetails(@RequestParam String keyword, ModelMap modelMap) {

1 Answer

Boban Talevski
Boban Talevski
24,793 Points

Your @RequestMapping annotation should just have the "vanilla" URI so to speak as a value. No need for q equals something, and yeah, I was initially trying something similar :).

The thing is, the q request parameter is going to be automatically picked up by the Spring framework, since it's a request parameter and is passed in the usual way in the HTTP request. The important thing is that the parameter name is q, and that's about it. The name q is defined in the input tag in the search form and that's all we need to match in our controller method.

So, mine looks like this:

    @RequestMapping("/search")
    public String searchResults(@RequestParam("q") String searchTerm, ModelMap modelMap) {
        List<Gif> gifsFound = gifRepository.searchByName(searchTerm);
        modelMap.put("gifs", gifsFound);

        return "home";
    }

The action in my search form tag is simply "/search" as well.