1

I am developing a web application using the Spring framework and Thymeleaf.

I have created a drop down menu, but I want something else on the page to appear when a certain option in the drop down menu is selected. By selected I mean when the option in the menu is clicked on and nothing more. I do not mean complete form submission.

I have read through the docs but found no solution.

Any ideas?

Thanks

Kingamere
  • 9,496
  • 23
  • 71
  • 110
  • As I understand you want to do it in the client side. So why don't you try jQuery? [This](http://stackoverflow.com/questions/7019096/jquery-dropdown-hide-show-div-based-on-value) might help you. – nmy Aug 25 '15 at 04:40
  • Oh so I need to use javascript? Spring doesn't provide any feature like this? – Kingamere Aug 25 '15 at 14:03

1 Answers1

0

You ca use JS using th:inline like this, explanation is on comment

JS code

<script th:inline="javascript">
    //Declare the URL of RequestMapping to use
    //Do no change format

    /*[+
     var urlToload = [[@{/path/spring}]];
     var anotherUrlToUse = [[@{/another/url?param=}]];
     +]*/

    //Handle you jquery event             
    $('body').on('click', '.dropdown-menu', function (e) {
        var t = $(this);
        //Sending your ajax request
        $.ajax({
            url: urlToload,
            method: 'POST',
            data: {
                optionSelected: t.val()
            }
        })
        /**
         * Execute when your request is done
         * @param {type} data : the page result of your Request                 * 
         */
        .done(function (data) {
            $("#receiver").html(data);
        })
        //Execute on fail
        .fail(function (xhr, ajaxOptions, thrownError) {
            console.log("ERROR");
            //Use anotherUrlToUse with GET METHOD to redirect
            window.location.replace(anotherUrlToUse+404);
        });
    });
</script>

Controller code

@RequestMapping(value = "/path/spring", method = RequestMethod.POST)
public String controllerMethod(Model model, @RequestParam("optionSelected") String optionSelected) {
    /**
     * Do your job
     * Your code
     */
    return "page/result";
}
Aroniaina
  • 1,252
  • 13
  • 31