Anton Belev
Anton Belev

Reputation: 13523

Spring redirect to another page on button click

I'm new to the Spring framework. At the moment I've got two HTML pages and when a button is clicked on the first one I want to redirect to the second page.

At the moment I'm doing this in a really hacky way.

The first page is just a log in page and depending on the user logged I'm redirection to a specific page. On the login page I've got two hidden <a> like these

<a href="admin" class="button" id="btnAdmin" style="opacity: 0;"> Login as tutor </a>
<a href="student" class="button" id="btnStudent" style="opacity: 0;"> Login as student</a>

Then using JavaScript I'm triggering this <a> to be clicked when the Login button is clicked. That way I'm successfully redirecting to the admin.html or student.html pages. The problem is that once I'm redirected to these pages the page is not rendered correctly (the js is not loaded correctly) so I need to refresh the page to see the proper content of the page. I assume that the reason for is because I'm redirecting in such a hacky way. So my question is - having a button like this (ignore the JQuery Mobile)

<input type="button" id="btnLogin" class="btnLogin" value="Sigh in" data-theme="b"/>

how to redirect properly in the Spring framework?

PS I'm coming from ASP.NET background so I was thinking of some sort of button click event handler in the back end, and then doing something like Response.Redirect("newpage");

Upvotes: 1

Views: 5652

Answers (1)

Rafał Wrzeszcz
Rafał Wrzeszcz

Reputation: 2057

Not sure if I understood your question correctly, but basically handling links and buttons is pure client-side task and has nothing to do with any server-side implementations, no matter if it's Java+Spring, or ASP.NET.

But if you really want to know how to redirect in Spring, the easiest way is to just return redirect:path from your action:

@Controller
class MyController
{
    @RequestMapping(...)
    public String myAction()
    {
        /* your logic here */
        return 'redirect:student';
    }
}

Upvotes: 1

Related Questions